Class: ChainOptions::OptionSet
- Inherits:
-
Object
- Object
- ChainOptions::OptionSet
- Defined in:
- lib/chain_options/option_set.rb
Instance Attribute Summary collapse
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
Class Method Summary collapse
-
.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`.
-
.warn_incompatible_options(option_name, *options) ⇒ Object
Warns of incompatible options for a chain_option.
Instance Method Summary collapse
-
#add_option(name, parameters) ⇒ Object
Checks the given option-parameters for incompatibilities and registers a new option.
-
#current_value(name) ⇒ Object
Returns the current_value of an option.
-
#handle_option_call(option_name, *args, &block) ⇒ Object
Handles a call of #option_name.
-
#initialize(instance, chain_options = {}, values = {}) ⇒ OptionSet
constructor
A new instance of OptionSet.
-
#new_value(name, *args, &block) ⇒ Object
Builds a new value for the given chain option.
-
#option(name) ⇒ Object
Returns an option registered under ‘name`.
Constructor Details
#initialize(instance, chain_options = {}, values = {}) ⇒ OptionSet
Returns a new instance of OptionSet.
33 34 35 36 37 38 39 |
# File 'lib/chain_options/option_set.rb', line 33 def initialize(instance, = {}, values = {}) @instance = instance @values = values @chain_options = .inject({}) do |, (name, config)| .merge(name => config.merge(instance_method_hash(config))) end end |
Instance Attribute Details
#instance ⇒ Object (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 (name, "invalid: :default", "incremental: true") if invalid.to_s == "default" (name, "incremental: true", "filter:") if filter (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 (option_name, *) warn "The 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) .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 |