Class: ProcessExecuter::Options::Base
- Inherits:
-
Object
- Object
- ProcessExecuter::Options::Base
- Defined in:
- lib/process_executer/options/base.rb
Overview
Defines, validates, and holds a set of option values
Options are defined by subclasses by overriding the define_options method.
Direct Known Subclasses
Instance Method Summary collapse
-
#allowed_options ⇒ Hash<Symbol, ProcessExecuter::Options::OptionDefinition>
All the allowed options as a hash whose keys are the option names.
-
#each_with_object(obj) {|key_value, obj| ... } ⇒ Object
Iterate over each option with an object.
-
#initialize(**options_hash) ⇒ Base
constructor
Create a new Options object.
-
#inspect ⇒ String
A string representation of the options.
-
#merge(*other_options_hashes) ⇒ self.class
Returns a new options object formed by merging self with each of other_hashes.
-
#merge!(*other_options_hashes) ⇒ self
Merge the given options into the current options object.
-
#to_h ⇒ Hash
A hash representation of the options.
-
#to_s ⇒ String
A string representation of the object that includes the options.
Constructor Details
#initialize(**options_hash) ⇒ Base
Create a new Options object
Normally you would use a subclass instead of instantiating this class directly.
63 64 65 66 67 68 69 |
# File 'lib/process_executer/options/base.rb', line 63 def initialize(**) @options_hash = .transform_values(&:default).merge() @errors = [] define_accessor_methods end |
Instance Method Details
#allowed_options ⇒ Hash<Symbol, ProcessExecuter::Options::OptionDefinition>
All the allowed options as a hash whose keys are the option names
The returned hash what is returned from define_options but with the option
names as keys. The values are instances of OptionDefinition.
The returned hash is frozen and cannot be modified.
88 89 90 91 92 93 |
# File 'lib/process_executer/options/base.rb', line 88 def @allowed_options ||= .to_h do |option| [option.name, option] end.freeze end |
#each_with_object(obj) {|key_value, obj| ... } ⇒ Object
Iterate over each option with an object
146 147 148 |
# File 'lib/process_executer/options/base.rb', line 146 def each_with_object(obj, &) .each_with_object(obj, &) end |
#inspect ⇒ String
A string representation of the options
115 116 117 |
# File 'lib/process_executer/options/base.rb', line 115 def inspect .inspect end |
#merge(*other_options_hashes) ⇒ self.class
Returns a new options object formed by merging self with each of other_hashes
213 214 215 216 |
# File 'lib/process_executer/options/base.rb', line 213 def merge(*) = .reduce(, :merge) self.class.new(**) end |
#merge!(*other_options_hashes) ⇒ self
Merge the given options into the current options object
Subsequent hashes' values overwrite earlier ones for the same key.
The merged options are checked the same way the constructor checks its
options: unknown options and invalid option values raise a
ProcessExecuter::ArgumentError. In that case, the current options
object is left unchanged.
183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/process_executer/options/base.rb', line 183 def merge!(*) = @options_hash @options_hash = .dup.merge!(*) @errors = [] self rescue ProcessExecuter::ArgumentError @options_hash = @errors = [] raise end |
#to_h ⇒ Hash
A hash representation of the options
127 128 129 |
# File 'lib/process_executer/options/base.rb', line 127 def to_h .dup end |
#to_s ⇒ String
A string representation of the object that includes the options
103 104 105 |
# File 'lib/process_executer/options/base.rb', line 103 def to_s "#{super.to_s[0..-2]} #{inspect}>" end |