Class: BranchIOCLI::Configuration::OptionWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/branch_io_cli/configuration/option_wrapper.rb

Overview

Proxy class for use with Command.new.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, options, add_defaults = true) ⇒ OptionWrapper

Returns a new instance of OptionWrapper.



9
10
11
12
13
14
15
16
17
# File 'lib/branch_io_cli/configuration/option_wrapper.rb', line 9

def initialize(hash, options, add_defaults = true)
  hash ||= {}

  @hash = hash
  @options = options
  @add_defaults = add_defaults

  build_option_hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/branch_io_cli/configuration/option_wrapper.rb', line 19

def method_missing(method_sym, *arguments, &block)
  option = @option_hash[method_sym]
  return super unless option

  value = hash[method_sym]
  return value unless add_defaults && value.nil?

  default_value = option.env_value
  default_value = option.default_value if default_value.nil?
  default_value
end

Instance Attribute Details

#add_defaultsObject (readonly)

Returns the value of attribute add_defaults.



7
8
9
# File 'lib/branch_io_cli/configuration/option_wrapper.rb', line 7

def add_defaults
  @add_defaults
end

#hashObject (readonly)

Returns the value of attribute hash.



5
6
7
# File 'lib/branch_io_cli/configuration/option_wrapper.rb', line 5

def hash
  @hash
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/branch_io_cli/configuration/option_wrapper.rb', line 6

def options
  @options
end

Instance Method Details

#build_option_hashObject



31
32
33
# File 'lib/branch_io_cli/configuration/option_wrapper.rb', line 31

def build_option_hash
  @option_hash = options.inject({}) { |hash, o| hash.merge o.name => o }
end