Class: Terraspace::App::CallableOption

Inherits:
Object
  • Object
show all
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

Methods included from Util::Logging

#logger

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={})
  @options = options
  # Example:
  # config_name:  config.allow.envs
  # config_value: ["dev"]
  # args:         [@stack_name] # passed to object.call
  @config_name = options[:config_name]
  @config_value = options[:config_value]
  @passed_args = options[:passed_args]
end

Instance Method Details

#objectObject

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)
      message = "ERROR: The #{@config_name} needs to return an Array or nil"
      logger.info message.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 message
    end
  end
  result
end