Class: Attentive::Match

Inherits:
Object
  • Object
show all
Defined in:
lib/attentive/match.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(phrase, attributes = {}) ⇒ Match

Returns a new instance of Match.



5
6
7
8
9
10
11
12
# File 'lib/attentive/match.rb', line 5

def initialize(phrase, attributes={})
  @phrase = phrase.to_s
  @match_data = attributes.fetch(:match_data, {})
  @match_start = attributes.fetch(:match_start)
  @match_end = attributes.fetch(:match_end)
  @message = attributes.fetch(:message)
  @listener = attributes[:listener]
end

Instance Attribute Details

#listenerObject (readonly)

Returns the value of attribute listener.



3
4
5
# File 'lib/attentive/match.rb', line 3

def listener
  @listener
end

#match_endObject (readonly)

Returns the value of attribute match_end.



3
4
5
# File 'lib/attentive/match.rb', line 3

def match_end
  @match_end
end

#match_startObject (readonly)

Returns the value of attribute match_start.



3
4
5
# File 'lib/attentive/match.rb', line 3

def match_start
  @match_start
end

#messageObject (readonly)

Returns the value of attribute message.



3
4
5
# File 'lib/attentive/match.rb', line 3

def message
  @message
end

#phraseObject (readonly)

Returns the value of attribute phrase.



3
4
5
# File 'lib/attentive/match.rb', line 3

def phrase
  @phrase
end

Instance Method Details

#[](variable_name) ⇒ Object



18
19
20
21
22
# File 'lib/attentive/match.rb', line 18

def [](variable_name)
  @match_data.fetch variable_name.to_s
rescue KeyError
  raise KeyError, "#{$!.message} in #{inspect}"
end

#inspectObject



37
38
39
# File 'lib/attentive/match.rb', line 37

def inspect
  "#<#{self.class.name} #{@match_data.inspect} #{phrase.inspect}>"
end

#matched?(variable_name) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/attentive/match.rb', line 14

def matched?(variable_name)
  !@match_data[variable_name.to_s].nil?
end

#replace_with(tokens) ⇒ Object



32
33
34
35
# File 'lib/attentive/match.rb', line 32

def replace_with(tokens)
  message[match_start...match_end] = tokens
  match_start + tokens.length
end

#to_hObject



28
29
30
# File 'lib/attentive/match.rb', line 28

def to_h
  @match_data
end

#to_sObject



24
25
26
# File 'lib/attentive/match.rb', line 24

def to_s
  @phrase
end