Class: Ufo::Config::CallableOption
- Inherits:
-
Object
- Object
- Ufo::Config::CallableOption
- Includes:
- Utils::Logging
- Defined in:
- lib/ufo/config/callable_option.rb,
lib/ufo/config/callable_option/concern.rb
Defined Under Namespace
Modules: Concern
Instance Method Summary collapse
- #inferred_config_value ⇒ Object
-
#initialize(options = {}) ⇒ CallableOption
constructor
A new instance of CallableOption.
-
#object ⇒ Object
Returns either an Array or nil.
Methods included from Utils::Logging
Constructor Details
#initialize(options = {}) ⇒ CallableOption
Returns a new instance of CallableOption.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ufo/config/callable_option.rb', line 12 def initialize(={}) @options = # Example: # config_name: names.stack # config_value: Ufo.config.names.stack # args: [self] # passed to object.call @config_name = [:config_name] @config_value = [:config_value] || inferred_config_value @config_name = "config.#{@config_name}" unless @config_name.include?("config.") @passed_args = [:passed_args] end |
Instance Method Details
#inferred_config_value ⇒ Object
24 25 26 27 |
# File 'lib/ufo/config/callable_option.rb', line 24 def inferred_config_value args = @options[:config_name].split('.').map(&:to_sym) # @options before @config_name is adjust to have full config name Ufo.config.dig(*args) end |
#object ⇒ Object
Returns either an Array or nil
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ufo/config/callable_option.rb', line 30 def object case @config_value when nil return nil when Array, String return @config_value when -> (c) { c.respond_to?(:public_instance_methods) && c.public_instance_methods.include?(:call) } object= @config_value.new when -> (c) { c.respond_to?(:call) } object = @config_value else raise "Invalid option for #{@config_name}" end if object result = @passed_args.empty? ? object.call : object.call(*@passed_args) valid_classes = [Array, String, NilClass] valid_classes_help = valid_classes valid_classes_help[-1] = "or #{valid_classes_help[-1]}" valid_classes_help = valid_classes.join(', ') unless valid_classes.include?(result.class) = "ERROR: The #{@config_name} needs to return an #{valid_classes_help}" logger.info .color(:red) logger.info <<~EOL The #{@config_name} when assigned a class, object, or proc must implement The call method and return an #{valid_classes_help}. The current return value is a #{result.class} EOL exit 1 end end result end |