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.



23
24
25
26
27
# File 'lib/objects/registry/command/base.rb', line 23

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



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

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)


10
11
12
# File 'lib/objects/registry/command/base.rb', line 10

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

.new_from_raw_command(data) ⇒ Object



4
5
6
7
8
# File 'lib/objects/registry/command/base.rb', line 4

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



14
15
16
17
18
19
20
# File 'lib/objects/registry/command/base.rb', line 14

def valid_types
  {
    external: ::Bcome::Registry::Command::External,
    internal: ::Bcome::Registry::Command::Internal,
    shortcut: ::Bcome::Registry::Command::Shortcut
  }
end

Instance Method Details

#defaultsObject



29
30
31
# File 'lib/objects/registry/command/base.rb', line 29

def defaults
  @data[:defaults]
end

#execute(*_params) ⇒ Object



45
46
47
# File 'lib/objects/registry/command/base.rb', line 45

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

#expected_keysObject



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

def expected_keys
  %i[console_command group description]
end

#process_arguments(arguments) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/objects/registry/command/base.rb', line 33

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



57
58
59
60
61
# File 'lib/objects/registry/command/base.rb', line 57

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



63
64
65
# File 'lib/objects/registry/command/base.rb', line 63

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