Class: ConvenientService::Config::Commands::NormalizeOptions
- Inherits:
-
Support::Command
- Object
- Support::Command
- ConvenientService::Config::Commands::NormalizeOptions
- Defined in:
- lib/convenient_service/config/commands/normalize_options.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #call ⇒ Hash
- #initialize(options:) ⇒ void constructor
- #normalize(value) ⇒ ConvenientService::Config::Entities::Option+
- #normalize_array(array) ⇒ Array<ConvenientService::Config::Entities::Option>
- #normalize_hash(hash) ⇒ ConvenientService::Config::Entities::Option+
- #normalize_option(option) ⇒ ConvenientService::Config::Entities::Option
- #normalize_options(options) ⇒ Array<ConvenientService::Config::Entities::Option>
- #normalize_set(set) ⇒ Array<ConvenientService::Config::Entities::Option>
- #normalize_symbol(symbol) ⇒ ConvenientService::Config::Entities::Option
- #raise_option_can_not_be_normalized(option) ⇒ Object
Methods inherited from Support::Command
Constructor Details
#initialize(options:) ⇒ void
22 23 24 |
# File 'lib/convenient_service/config/commands/normalize_options.rb', line 22 def initialize(options:) @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/convenient_service/config/commands/normalize_options.rb', line 16 def @options end |
Instance Method Details
#call ⇒ Hash
29 30 31 32 33 |
# File 'lib/convenient_service/config/commands/normalize_options.rb', line 29 def call Utils::Array.wrap(.to_a) .flat_map { |option| normalize(option) } .reduce({}) { |memo, option| memo.merge(option.name => option) } # index_by end |
#normalize(value) ⇒ ConvenientService::Config::Entities::Option+
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 (value) else raise_option_can_not_be_normalized(value) end end |
#normalize_array(array) ⇒ Array<ConvenientService::Config::Entities::Option>
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 (value) else raise_option_can_not_be_normalized(array) end end end |
#normalize_hash(hash) ⇒ ConvenientService::Config::Entities::Option+
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 () .to_a end |
#normalize_set(set) ⇒ Array<ConvenientService::Config::Entities::Option>
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 (value) else raise_option_can_not_be_normalized(set) end end end |
#normalize_symbol(symbol) ⇒ ConvenientService::Config::Entities::Option
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 |