Class: Rill

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/Olib/pattern_matching/rill.rb

Constant Summary collapse

PROMPT =
/<prompt/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start: nil, close: PROMPT, mode: :xml, timeout: 5) ⇒ Rill

Returns a new instance of Rill.



15
16
17
18
19
20
21
# File 'lib/Olib/pattern_matching/rill.rb', line 15

def initialize(start: nil, close: PROMPT, mode: :xml, timeout: 5)
  fail "Rill.new() requires :start argument" if start.nil?
  @mode    = mode
  @close   = Outcome.new(close)
  @start   = Outcome.new(start)
  @timeout = timeout
end

Instance Attribute Details

#closeObject (readonly)

Returns the value of attribute close.



13
14
15
# File 'lib/Olib/pattern_matching/rill.rb', line 13

def close
  @close
end

#modeObject (readonly)

Returns the value of attribute mode.



13
14
15
# File 'lib/Olib/pattern_matching/rill.rb', line 13

def mode
  @mode
end

#startObject (readonly)

Returns the value of attribute start.



13
14
15
# File 'lib/Olib/pattern_matching/rill.rb', line 13

def start
  @start
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



13
14
15
# File 'lib/Olib/pattern_matching/rill.rb', line 13

def timeout
  @timeout
end

Class Method Details

.union(*patterns) ⇒ Object



6
7
8
# File 'lib/Olib/pattern_matching/rill.rb', line 6

def self.union(*patterns)
  patterns.join("|")
end

Instance Method Details

#capture(obj, command_template) ⇒ Object



23
24
25
26
# File 'lib/Olib/pattern_matching/rill.rb', line 23

def capture(obj, command_template)
  return capture_xml(obj, command_template) if mode.eql?(:xml)
  fail "non-XML mode not implemented yet"
end

#capture_xml(obj, command_template) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/Olib/pattern_matching/rill.rb', line 28

def capture_xml(obj, command_template)
  begin_pattern = @start.prepare(obj)
  end_pattern   = @close.prepare(obj)
  command       = Outcome.prepare(command_template, obj)
  state         = :start
  lines         = []
  matches       = {}
  XML.cmd(command, pattern: begin_pattern, timeout: @timeout) do |line|
    state = :open  if line.match(begin_pattern)
    lines  << line if state.eql?(:open)
    if (result = (line.match(begin_pattern) or line.match(end_pattern)))
      matches.merge!(result.to_h)
    end

    if (line.match(end_pattern) and state.eql?(:open))
      return [:ok, matches, lines]
    end
  end
end

#each(&block) ⇒ Object



48
49
50
# File 'lib/Olib/pattern_matching/rill.rb', line 48

def each(&block)
  @lines.each(&block)
end