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 |
# File 'lib/process_executer/options/base.rb', line 63 def initialize(**) @options_hash = .transform_values(&:default).merge() define_accessor_methods validate! 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.
86 87 88 89 90 91 |
# File 'lib/process_executer/options/base.rb', line 86 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
144 145 146 |
# File 'lib/process_executer/options/base.rb', line 144 def each_with_object(obj, &) .each_with_object(obj, &) end |
#inspect ⇒ String
A string representation of the options
113 114 115 |
# File 'lib/process_executer/options/base.rb', line 113 def inspect .inspect end |
#merge(*other_options_hashes) ⇒ self.class
Returns a new options object formed by merging self with each of other_hashes
208 209 210 211 |
# File 'lib/process_executer/options/base.rb', line 208 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.
181 182 183 184 185 186 187 188 189 |
# File 'lib/process_executer/options/base.rb', line 181 def merge!(*) = @options_hash @options_hash = .dup.merge!(*) validate! self rescue ProcessExecuter::ArgumentError @options_hash = raise end |
#to_h ⇒ Hash
A hash representation of the options
125 126 127 |
# File 'lib/process_executer/options/base.rb', line 125 def to_h .dup end |
#to_s ⇒ String
A string representation of the object that includes the options
101 102 103 |
# File 'lib/process_executer/options/base.rb', line 101 def to_s "#{super.to_s[0..-2]} #{inspect}>" end |