Sha256: 8e6a7e77d9c4baecc217e9eaecc2cdcddad40820af16b11bc94ee9605832352c

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'core_ext/string/demodulize'

module Space
  class Action
    class Handler
      ALIASES = {
        ''  => 'scope',
        '-' => 'unscope',
        'r' => 'refresh'
      }
      attr_reader :project

      def initialize(project)
        @project = project
      end

      def run(line)
        scope, type = parse(line)
        action = action_for(scope, type)
        action.run
        Events.flush
      end

      private

        def action_for(scope, type)
          const = const_for(type)
          repos = repos_for(scope)
          args  = [project, repos]
          args << type if const == Action::Execute
          const.new(*args)
        end

        def parse(line)
          Parser.new(project.repos.names).parse(line)
        end

        def const_for(type)
          Action.const_get(const_name(type))
        rescue NameError
          Action::Execute
        end

        def const_name(type)
          type = (type || '').strip
          type = ALIASES[type] if ALIASES.key?(type)
          type.capitalize
        end

        def repos_for(scope)
          scope ? project.repos.select_by_names_or_numbers(scope) : project.repos.scope.self_and_deps
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
space-0.0.6 lib/space/action/handler.rb