Class: Attentive::Match
- Inherits:
-
Object
- Object
- Attentive::Match
- Defined in:
- lib/attentive/match.rb
Instance Attribute Summary collapse
-
#listener ⇒ Object
readonly
Returns the value of attribute listener.
-
#match_end ⇒ Object
readonly
Returns the value of attribute match_end.
-
#match_start ⇒ Object
readonly
Returns the value of attribute match_start.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#phrase ⇒ Object
readonly
Returns the value of attribute phrase.
Instance Method Summary collapse
- #[](variable_name) ⇒ Object
-
#initialize(phrase, attributes = {}) ⇒ Match
constructor
A new instance of Match.
- #inspect ⇒ Object
- #matched?(variable_name) ⇒ Boolean
- #replace_with(tokens) ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
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
#listener ⇒ Object (readonly)
Returns the value of attribute listener.
3 4 5 |
# File 'lib/attentive/match.rb', line 3 def listener @listener end |
#match_end ⇒ Object (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_start ⇒ Object (readonly)
Returns the value of attribute match_start.
3 4 5 |
# File 'lib/attentive/match.rb', line 3 def match_start @match_start end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
3 4 5 |
# File 'lib/attentive/match.rb', line 3 def @message end |
#phrase ⇒ Object (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, "#{$!.} in #{inspect}" end |
#inspect ⇒ Object
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
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) [match_start...match_end] = tokens match_start + tokens.length end |
#to_h ⇒ Object
28 29 30 |
# File 'lib/attentive/match.rb', line 28 def to_h @match_data end |
#to_s ⇒ Object
24 25 26 |
# File 'lib/attentive/match.rb', line 24 def to_s @phrase end |