Class: Bcome::Registry::Command::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/objects/registry/command/base.rb

Direct Known Subclasses

External, Internal, Shortcut

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Base

Returns a new instance of Base.



26
27
28
29
30
# File 'lib/objects/registry/command/base.rb', line 26

def initialize(data)
  @data = data
  @data[:defaults] = {} unless @data[:defaults]
  validate
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



56
57
58
# 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

Class Method Details

.is_valid_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/objects/registry/command/base.rb', line 13

def is_valid_type?(type)
  valid_types.keys.include?(type.to_sym)
end

.new_from_raw_command(data) ⇒ Object



6
7
8
9
10
11
# 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_typesObject



17
18
19
20
21
22
23
# 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

Instance Method Details

#defaultsObject



32
33
34
# File 'lib/objects/registry/command/base.rb', line 32

def defaults
  @data[:defaults]
end

#execute(*_params) ⇒ Object



48
49
50
# File 'lib/objects/registry/command/base.rb', line 48

def execute(*_params)
  raise 'Should be overriden'
end

#expected_keysObject



52
53
54
# File 'lib/objects/registry/command/base.rb', line 52

def expected_keys
  %i[console_command group description]
end

#process_arguments(arguments) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# 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

#validateObject



60
61
62
63
64
# 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) ⇒ Object



66
67
68
# File 'lib/objects/registry/command/base.rb', line 66

def validation_error(message)
  raise Bcome::Exception::InvalidContextCommand, message
end