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.
35 36 37 38 39 40 41 |
# File 'lib/chain_options/option_set.rb', line 35 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.
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 (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.
12 13 14 |
# File 'lib/chain_options/option_set.rb', line 12 def (option_name, *) STDERR.puts "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.
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) .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 |