Class: Unitwise::Expression::Matcher

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

Overview

Matcher is responsible for building up Parslet alternatives of atoms and prefixes to be used by Unitwise::Expression::Parser.

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.



22
23
24
25
# File 'lib/unitwise/expression/matcher.rb', line 22

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

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



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

def collection
  @collection
end

#modeObject (readonly)

Returns the value of attribute mode.



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

def mode
  @mode
end

Class Method Details

.atom(mode) ⇒ Object



7
8
9
# File 'lib/unitwise/expression/matcher.rb', line 7

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

.metric_atom(mode) ⇒ Object



11
12
13
# File 'lib/unitwise/expression/matcher.rb', line 11

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

.prefix(mode) ⇒ Object



15
16
17
# File 'lib/unitwise/expression/matcher.rb', line 15

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

Instance Method Details

#alternativeObject



37
38
39
# File 'lib/unitwise/expression/matcher.rb', line 37

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

#matchersObject



33
34
35
# File 'lib/unitwise/expression/matcher.rb', line 33

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

#stringsObject



27
28
29
30
31
# File 'lib/unitwise/expression/matcher.rb', line 27

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