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



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

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

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection



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

def collection
  @collection
end

#modeObject (readonly)

Returns the value of attribute mode



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

def mode
  @mode
end

Class Method Details

.atom(mode) ⇒ Object



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

def atom(mode)
  @atom ||= {}
  @atom[mode] ||= new(Atom.all, mode).alternative
end

.metric_atom(mode) ⇒ Object



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

def metric_atom(mode)
  @metric_atom ||= {}
  @metric_atom[mode] ||=
    new(Atom.all.select(&:metric?), mode).alternative
end

.prefix(mode) ⇒ Object



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

def prefix(mode)
  @prefix ||= {}
  @prefix[mode] ||= new(Prefix.all, mode).alternative
end

Instance Method Details

#alternativeObject



41
42
43
# File 'lib/unitwise/expression/matcher.rb', line 41

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

#matchersObject



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

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

#stringsObject



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

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