Sha256: 18eb85eb634d572780a0f51101a9b8486a72d93969606bff978af6d0ee5ea5cd

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 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)
        Events.sources.registered do
          action.run
        end
      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

2 entries across 2 versions & 1 rubygems

Version Path
space-0.0.8 lib/space/action/handler.rb
space-0.0.7 lib/space/action/handler.rb