Sha256: 0bbd61679358183b927b74f2c7ae1743532c89ca4d744baf54d98f1a5e5c123a

Contents?: true

Size: 1.08 KB

Versions: 6

Compression:

Stored size: 1.08 KB

Contents

module Unitwise
  module Expression
    # Matcher is responsible for building up Parslet alternatives of atoms and
    # prefixes to be used by Unitwise::Expression::Parser.
    class Matcher
      class << self
        def atom(mode)
          @atom ||= {}
          @atom[mode] ||= new(Atom.all, mode).alternative
        end

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

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

      attr_reader :collection, :mode

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

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

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

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

    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
unitwise-2.0.0 lib/unitwise/expression/matcher.rb
unitwise-1.1.0 lib/unitwise/expression/matcher.rb
unitwise-193-1.0.4 lib/unitwise/expression/matcher.rb
unitwise-1.0.4 lib/unitwise/expression/matcher.rb
unitwise-1.0.3 lib/unitwise/expression/matcher.rb
unitwise-1.0.2 lib/unitwise/expression/matcher.rb