class Bcome::Registry::Command::Base

Public Class Methods

is_valid_type?(type) click to toggle source
# File lib/objects/registry/command/base.rb, line 13
def is_valid_type?(type)
  valid_types.keys.include?(type.to_sym)
end
new(data) click to toggle source
# File lib/objects/registry/command/base.rb, line 26
def initialize(data)
  @data = data
  @data[:defaults] = {} unless @data[:defaults]
  validate
end
new_from_raw_command(data) click to toggle source
# File lib/objects/registry/command/base.rb, line 6
def new_from_raw_command(data)
  raise Bcome::Exception::InvalidContextCommand, "#{data.inspect} is missing key type" unless data[:type]
  raise Bcome::Exception::InvalidContextCommand, "#{data.inspect} has invalid type '#{data[:type]}'" unless is_valid_type?(data[:type])

  valid_types[data[:type].to_sym].new(data)
end
valid_types() click to toggle source
# File lib/objects/registry/command/base.rb, line 17
def valid_types
  {
    external: ::Bcome::Registry::Command::External,
    internal: ::Bcome::Registry::Command::Internal,
    shortcut: ::Bcome::Registry::Command::Shortcut
  }
end

Public Instance Methods

defaults() click to toggle source
# File lib/objects/registry/command/base.rb, line 32
def defaults
  @data[:defaults]
end
execute(*_params) click to toggle source
# File lib/objects/registry/command/base.rb, line 48
def execute(*_params)
  raise 'Should be overriden'
end
expected_keys() click to toggle source
# File lib/objects/registry/command/base.rb, line 52
def expected_keys
  %i[console_command group description]
end
method_missing(method_sym, *arguments, &block) click to toggle source
Calls superclass method
# File lib/objects/registry/command/base.rb, line 56
def method_missing(method_sym, *arguments, &block)
  @data.key?(method_sym) ? @data[method_sym] : super
end
process_arguments(arguments) click to toggle source
# File lib/objects/registry/command/base.rb, line 36
def process_arguments(arguments)
  merged_arguments = {}

  if [Array, Hash].include?(arguments.class)
    processor_klass = arguments.is_a?(Array) ? ::Bcome::Registry::Arguments::CommandLine : ::Bcome::Registry::Arguments::Console
    merged_arguments = processor_klass.process(arguments, defaults)
  elsif defaults
    merged_arguments = defaults
  end
  merged_arguments
end
validate() click to toggle source
# File lib/objects/registry/command/base.rb, line 60
def validate
  expected_keys.each do |key|
    validation_error "#{@data.inspect} is missing key #{key}" unless @data.key?(key)
  end
end
validation_error(message) click to toggle source
# File lib/objects/registry/command/base.rb, line 66
def validation_error(message)
  raise Bcome::Exception::InvalidContextCommand, message
end