Sha256: 69fc580b6d3c61a0522140b3ac68949c45ea9070846995b6e4debcc1b3399377

Contents?: true

Size: 763 Bytes

Versions: 2

Compression:

Stored size: 763 Bytes

Contents

module Space
  class Action
    class Parser
      attr_reader :names, :line

      def initialize(names)
        @names = names
      end

      def parse(line)
        @line = line
        scope = parse_scope
        command = parse_command
        [scope, command]
      end

      private

        def parse_scope
          scope = []
          pattern = /^(#{names.join('|')}|\d+)\s*/
          line.gsub!(pattern) { |repo| scope << resolve(repo.strip); '' } while line =~ pattern
          line.strip!
          scope unless scope.empty?
        end

        def parse_command
          line.strip
          line unless line.empty?
        end

        def resolve(repo)
          repo =~ /^\d+$/ ? names[repo.to_i - 1] : repo
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
space-0.0.3 lib/space/action/parser.rb
space-0.0.2 lib/space/action/parser.rb