Class: Terraspace::App::CallableOption
- Inherits:
-
Object
- Object
- Terraspace::App::CallableOption
- Includes:
- Util::Logging
- Defined in:
- lib/terraspace/app/callable_option.rb,
lib/terraspace/app/callable_option/concern.rb
Defined Under Namespace
Modules: Concern
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ CallableOption
constructor
A new instance of CallableOption.
-
#object ⇒ Object
Returns either an Array or nil.
Methods included from Util::Logging
Constructor Details
#initialize(options = {}) ⇒ CallableOption
Returns a new instance of CallableOption.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/terraspace/app/callable_option.rb', line 16 def initialize(={}) @options = # Example: # config_name: config.allow.envs # config_value: ["dev"] # args: [@stack_name] # passed to object.call @config_name = [:config_name] @config_value = [:config_value] @passed_args = [:passed_args] end |
Instance Method Details
#object ⇒ Object
Returns either an Array or nil
28 29 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 |
# File 'lib/terraspace/app/callable_option.rb', line 28 def object case @config_value when nil return nil when Array 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) unless result.is_a?(Array) || result.is_a?(NilClass) = "ERROR: The #{@config_name} needs to return an Array or nil" logger.info .color(:yellow) logger.info <<~EOL The #{@config_name} when assigned a class, object, or proc must implement the call method and return an Array or nil. The current return value is a #{result.class} EOL raise end end result end |