Sha256: d8ef93395afdabaab4faba77ede59c6c884002ebfa9a53286535122465c5dc62

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

# Class represents a lono option that is possibly callable. Examples:
#
#    config.allow.envs
#    config.allow.regions
#    config.deny.envs
#    config.deny.regions
#
# Abstraction is definitely obtuse. Using it to get rid of duplication.
#
class Lono::App
  class CallableOption
    include Lono::Utils::Logging

    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

    # Returns either an Array or nil
    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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lono-8.0.0.pre.rc6 lib/lono/app/callable_option.rb
lono-8.0.0.pre.rc5 lib/lono/app/callable_option.rb
lono-8.0.0.pre.rc4 lib/lono/app/callable_option.rb
lono-8.0.0.pre.rc3 lib/lono/app/callable_option.rb