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.



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

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.



41
42
43
# File 'lib/chain_options/option_set.rb', line 41

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`



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

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.



10
11
12
# File 'lib/chain_options/option_set.rb', line 10

def warn_incompatible_options(option_name, *options)
  warn "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.


47
48
49
50
# File 'lib/chain_options/option_set.rb', line 47

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.



55
56
57
# File 'lib/chain_options/option_set.rb', line 55

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.



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

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.



72
73
74
# File 'lib/chain_options/option_set.rb', line 72

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

#option(name) ⇒ Object

Returns an option registered under ‘name`.



62
63
64
65
# File 'lib/chain_options/option_set.rb', line 62

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