Class: ChainOptions::OptionSet

Inherits:
Object
  • Object
show all
Defined in:
lib/chain_options/option_set.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, chain_options = {}, values = {}) ⇒ OptionSet

Returns a new instance of OptionSet.

Parameters:

  • instance (Object)

    the object that uses the chain_option set.

  • chain_options (Hash) (defaults to: {})

    a hash of ‘config_hash` to initialize options from a config hash.

  • values (Hash) (defaults to: {})

    a hash of ‘value` with initial values for the named options.



35
36
37
38
39
40
41
# File 'lib/chain_options/option_set.rb', line 35

def initialize(instance, chain_options = {}, values = {})
  @instance      = instance
  @values        = values
  @chain_options = chain_options.inject({}) do |options, (name, config)|
    options.merge(name => config.merge(instance_method_hash(config)))
  end
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



43
44
45
# File 'lib/chain_options/option_set.rb', line 43

def instance
  @instance
end

Class Method Details

.handle_warnings(name, incremental: false, invalid: :raise, filter: nil, transform: nil) ⇒ Object

Prints warnings for incompatible options which were used as arguments in ‘chain_option`



19
20
21
22
23
24
25
# File 'lib/chain_options/option_set.rb', line 19

def handle_warnings(name, incremental: false, invalid: :raise, filter: nil, transform: nil, **)
  if incremental
    warn_incompatible_options(name, 'invalid: :default', 'incremental: true') if invalid.to_s == 'default'
    warn_incompatible_options(name, 'incremental: true', 'filter:') if filter
    warn_incompatible_options(name, 'incremental: true', 'transform:') if transform
  end
end

.warn_incompatible_options(option_name, *options) ⇒ Object

Warns of incompatible options for a chain_option. This does not necessarily mean that an error will be raised.



12
13
14
# File 'lib/chain_options/option_set.rb', line 12

def warn_incompatible_options(option_name, *options)
  STDERR.puts "The options #{options.join(', ')} are incompatible for the chain_option #{option_name}."
end

Instance Method Details

#add_option(name, parameters) ⇒ Object

Checks the given option-parameters for incompatibilities and registers a

new option.


49
50
51
52
# File 'lib/chain_options/option_set.rb', line 49

def add_option(name, parameters)
  self.class.handle_warnings(name, **parameters.dup)
  chain_options.merge(name => parameters.merge(method_hash(parameters)))
end

#current_value(name) ⇒ Object

Returns the current_value of an option.



57
58
59
# File 'lib/chain_options/option_set.rb', line 57

def current_value(name)
  option(name).current_value
end

#handle_option_call(option_name, *args, &block) ⇒ Object

Handles a call of #option_name. Determines whether the call was meant to be a setter or a getter and acts accordingly.



83
84
85
86
87
88
89
90
# File 'lib/chain_options/option_set.rb', line 83

def handle_option_call(option_name, *args, &block)
  if getter?(option_name, *args, &block)
    current_value(option_name)
  else
    new_value = new_value(option_name, *args, &block)
    instance.class.new(@values.merge(option_name.to_sym => new_value))
  end
end

#new_value(name, *args, &block) ⇒ Object

Builds a new value for the given chain option. It automatically applies transformations and filters and validates the resulting value, raising an exception if the value is not valid.



74
75
76
# File 'lib/chain_options/option_set.rb', line 74

def new_value(name, *args, &block)
  option(name).new_value(*args, &block)
end

#option(name) ⇒ Object

Returns an option registered under ‘name`.



64
65
66
67
# File 'lib/chain_options/option_set.rb', line 64

def option(name)
  config = chain_options[name] || raise_no_option_error(name)
  Option.new(config).tap { |o| o.initial_value(values[name]) if values.key?(name) }
end