Class: Ragol::Matcher
- Inherits:
-
Object
- Object
- Ragol::Matcher
- Includes:
- Logue::Loggable
- Defined in:
- lib/ragol/matcher.rb
Instance Attribute Summary collapse
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
Instance Method Summary collapse
- #find_match(opt) ⇒ Object
-
#initialize(tags) ⇒ Matcher
constructor
A new instance of Matcher.
- #match?(opt) ⇒ Boolean
- #score(opt) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(tags) ⇒ Matcher
Returns a new instance of Matcher.
12 13 14 |
# File 'lib/ragol/matcher.rb', line 12 def initialize @elements = [ ].flatten.compact end |
Instance Attribute Details
#elements ⇒ Object (readonly)
Returns the value of attribute elements.
10 11 12 |
# File 'lib/ragol/matcher.rb', line 10 def elements @elements end |
Instance Method Details
#find_match(opt) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ragol/matcher.rb', line 16 def find_match opt tag = opt.split('=', 2)[0] || opt @elements.each do |elmt| if elmt.kind_of?(Regexp) if md = elmt.match(tag) return [ :regexp, md ] end elsif tag.length > elmt.length next elsif elmt.index(tag) == 0 score = tag.length == elmt.length ? 1.0 : tag.length * 0.01 return [ :string, score ] end end nil end |
#match?(opt) ⇒ Boolean
34 35 36 37 |
# File 'lib/ragol/matcher.rb', line 34 def match? opt type, val = find_match(opt) type && val end |
#score(opt) ⇒ Object
39 40 41 42 |
# File 'lib/ragol/matcher.rb', line 39 def score opt type, val = find_match(opt) type && (type == :regexp ? 1.0 : val) end |
#to_s ⇒ Object
44 45 46 |
# File 'lib/ragol/matcher.rb', line 44 def to_s @elements.join(', ') end |