Class: Unitwise::Expression::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/unitwise/expression/matcher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, mode = :primary_code) ⇒ Matcher

Returns a new instance of Matcher.



20
21
22
23
# File 'lib/unitwise/expression/matcher.rb', line 20

def initialize(collection, mode=:primary_code)
  @collection = collection
  @mode = mode
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



18
19
20
# File 'lib/unitwise/expression/matcher.rb', line 18

def collection
  @collection
end

#modeObject (readonly)

Returns the value of attribute mode.



18
19
20
# File 'lib/unitwise/expression/matcher.rb', line 18

def mode
  @mode
end

Class Method Details

.atom(mode) ⇒ Object



5
6
7
# File 'lib/unitwise/expression/matcher.rb', line 5

def atom(mode)
  new(Atom.all, mode).alternative
end

.metric_atom(mode) ⇒ Object



9
10
11
# File 'lib/unitwise/expression/matcher.rb', line 9

def metric_atom(mode)
  new(Atom.all.select(&:metric?), mode).alternative
end

.prefix(mode) ⇒ Object



13
14
15
# File 'lib/unitwise/expression/matcher.rb', line 13

def prefix(mode)
  new(Prefix.all, mode).alternative
end

Instance Method Details

#alternativeObject



35
36
37
# File 'lib/unitwise/expression/matcher.rb', line 35

def alternative
  @alternative ||= Parslet::Atoms::Alternative.new(*matchers)
end

#matchersObject



31
32
33
# File 'lib/unitwise/expression/matcher.rb', line 31

def matchers
  @matchers ||= strings.map {|s| Parslet::Atoms::Str.new(s) }
end

#stringsObject



25
26
27
28
29
# File 'lib/unitwise/expression/matcher.rb', line 25

def strings
  @stings ||= collection.map(&mode).flatten.compact.sort do |x,y|
    y.length <=> x.length
  end
end