Class: ConvenientService::Config::Commands::NormalizeOptions

Inherits:
Support::Command show all
Defined in:
lib/convenient_service/config/commands/normalize_options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Support::Command

[], call

Constructor Details

#initialize(options:) ⇒ void

Parameters:

  • options (Object)

    Can be any type.



22
23
24
# File 'lib/convenient_service/config/commands/normalize_options.rb', line 22

def initialize(options:)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/convenient_service/config/commands/normalize_options.rb', line 16

def options
  @options
end

Instance Method Details

#callHash

Returns:

  • (Hash)


29
30
31
32
33
# File 'lib/convenient_service/config/commands/normalize_options.rb', line 29

def call
  Utils::Array.wrap(options.to_a)
    .flat_map { |option| normalize(option) }
    .reduce({}) { |memo, option| memo.merge(option.name => option) } # index_by
end

#normalize(value) ⇒ ConvenientService::Config::Entities::Option+

Parameters:

  • value (Object)

    Can be any type.

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/convenient_service/config/commands/normalize_options.rb', line 39

def normalize(value)
  case value
  when ::Symbol then normalize_symbol(value)
  when ::Array then normalize_array(value)
  when ::Set then normalize_set(value)
  when ::Hash then normalize_hash(value)
  when Entities::Option then normalize_option(value)
  when Entities::Options then normalize_options(value)
  else
    raise_option_can_not_be_normalized(value)
  end
end

#normalize_array(array) ⇒ Array<ConvenientService::Config::Entities::Option>

Parameters:

  • array (Array<Object>)

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/convenient_service/config/commands/normalize_options.rb', line 64

def normalize_array(array)
  array.map do |value|
    case value
    when ::Symbol then normalize_symbol(value)
    when ::Hash then normalize_hash(value)
    when Entities::Option then normalize_option(value)
    when Entities::Options then normalize_options(value)
    else
      raise_option_can_not_be_normalized(array)
    end
  end
end

#normalize_hash(hash) ⇒ ConvenientService::Config::Entities::Option+

Parameters:

  • hash (Hash{Symbol => Object})

Returns:



98
99
100
101
102
# File 'lib/convenient_service/config/commands/normalize_options.rb', line 98

def normalize_hash(hash)
  return Entities::Option.new(**hash) if hash.has_key?(:name)

  hash.map { |name, enabled| Entities::Option.new(name: name, enabled: enabled) }
end

#normalize_option(option) ⇒ ConvenientService::Config::Entities::Option



108
109
110
# File 'lib/convenient_service/config/commands/normalize_options.rb', line 108

def normalize_option(option)
  option
end

#normalize_options(options) ⇒ Array<ConvenientService::Config::Entities::Option>



116
117
118
# File 'lib/convenient_service/config/commands/normalize_options.rb', line 116

def normalize_options(options)
  options.to_a
end

#normalize_set(set) ⇒ Array<ConvenientService::Config::Entities::Option>

Parameters:

  • set (Set<Object>)

Returns:



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/convenient_service/config/commands/normalize_options.rb', line 81

def normalize_set(set)
  set.map do |value|
    case value
    when ::Symbol then normalize_symbol(value)
    when ::Hash then normalize_hash(value)
    when Entities::Option then normalize_option(value)
    when Entities::Options then normalize_options(value)
    else
      raise_option_can_not_be_normalized(set)
    end
  end
end

#normalize_symbol(symbol) ⇒ ConvenientService::Config::Entities::Option

Parameters:

  • symbol (Symbol)

Returns:



56
57
58
# File 'lib/convenient_service/config/commands/normalize_options.rb', line 56

def normalize_symbol(symbol)
  Entities::Option.new(name: symbol, enabled: true)
end

#raise_option_can_not_be_normalized(option) ⇒ Object



124
125
126
# File 'lib/convenient_service/config/commands/normalize_options.rb', line 124

def raise_option_can_not_be_normalized(option)
  ::ConvenientService.raise Exceptions::OptionCanNotBeNormalized.new(option: option)
end