Class: Cuboid::Options
- Includes:
- Singleton
- Defined in:
- lib/cuboid/options.rb
Overview
Defined Under Namespace
Classes: Error
Constant Summary collapse
- TO_RPC_IGNORE =
Set.new([ :instance, :rpc, :agent, :queue, :paths, :snapshot, :report, :output, :system ])
- TO_HASH_IGNORE =
Set.new([ :instance ])
Instance Attribute Summary collapse
-
#application ⇒ Object
Returns the value of attribute application.
-
#authorized_by ⇒ String
E-mail address of the person that authorized the run.
Class Method Summary collapse
- .attr_accessor(*vars) ⇒ Object
- .attr_accessors ⇒ Object
-
.group_classes ⇒ Hash<Symbol,OptionGroup>
Option group classes by name.
- .method_missing(sym, *args, &block) ⇒ Object
-
.register_group(group) ⇒ Object
Should be called by Cuboid::OptionGroup.inherited.
- .respond_to?(*args) ⇒ Boolean
Instance Method Summary collapse
- #attr_accessors ⇒ Object
- #dup ⇒ Object
-
#hash_to_rpc_data(hash) ⇒ Hash
‘hash` in #to_rpc_data format.
- #hash_to_save_data(hash) ⇒ Object
-
#initialize ⇒ Options
constructor
A new instance of Options.
-
#load(filepath) ⇒ Cuboid::Options
Loads a file created by #save.
-
#reset ⇒ Options
Restores everything to their default values.
-
#rpc_data_to_hash(hash) ⇒ Hash
‘hash` in #to_hash format.
- #save(file) ⇒ Object
-
#to_hash ⇒ Hash
(also: #to_h)
‘self` converted to a Hash.
-
#to_rpc_data ⇒ Hash
‘self` converted to a Hash suitable for RPC transmission.
- #to_rpc_data_without_defaults ⇒ Object
- #to_save_data ⇒ Object
- #to_save_data_without_defaults ⇒ Object
-
#update(options) ⇒ Options
(also: #set)
Configures options via a Hash object.
-
#validate ⇒ Hash
Hash of errors with the name of the invalid options/groups as the keys.
Constructor Details
#initialize ⇒ Options
Returns a new instance of Options.
94 95 96 |
# File 'lib/cuboid/options.rb', line 94 def initialize reset end |
Instance Attribute Details
#application ⇒ Object
Returns the value of attribute application.
92 93 94 |
# File 'lib/cuboid/options.rb', line 92 def application @application end |
#authorized_by ⇒ String
Returns E-mail address of the person that authorized the run.
90 91 92 |
# File 'lib/cuboid/options.rb', line 90 def @authorized_by end |
Class Method Details
.attr_accessor(*vars) ⇒ Object
14 15 16 17 18 |
# File 'lib/cuboid/options.rb', line 14 def self.attr_accessor(*vars) @attr_accessors ||= [] @attr_accessors |= vars super( *vars ) end |
.attr_accessors ⇒ Object
20 21 22 |
# File 'lib/cuboid/options.rb', line 20 def self.attr_accessors @attr_accessors end |
.group_classes ⇒ Hash<Symbol,OptionGroup>
Returns Option group classes by name.
56 57 58 |
# File 'lib/cuboid/options.rb', line 56 def group_classes @group_classes ||= {} end |
.method_missing(sym, *args, &block) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/cuboid/options.rb', line 38 def method_missing( sym, *args, &block ) if instance.respond_to?( sym ) instance.send( sym, *args, &block ) else super( sym, *args, &block ) end end |
.register_group(group) ⇒ Object
Should be called by Cuboid::OptionGroup.inherited.
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cuboid/options.rb', line 62 def register_group( group ) name = Utilities.caller_name # Prepare an attribute reader for this group... attr_reader name # ... and initialize it. instance_variable_set "@#{name}".to_sym, group.new group_classes[name.to_sym] = group end |
.respond_to?(*args) ⇒ Boolean
46 47 48 |
# File 'lib/cuboid/options.rb', line 46 def respond_to?( *args ) super || instance.respond_to?( *args ) end |
Instance Method Details
#attr_accessors ⇒ Object
24 25 26 |
# File 'lib/cuboid/options.rb', line 24 def attr_accessors self.class.attr_accessors end |
#dup ⇒ Object
239 240 241 |
# File 'lib/cuboid/options.rb', line 239 def dup self.class.allocate.reset.update( self.to_h ) end |
#hash_to_rpc_data(hash) ⇒ Hash
Returns ‘hash` in #to_rpc_data format.
231 232 233 |
# File 'lib/cuboid/options.rb', line 231 def hash_to_rpc_data( hash ) self.class.allocate.reset.update( hash ).to_rpc_data end |
#hash_to_save_data(hash) ⇒ Object
235 236 237 |
# File 'lib/cuboid/options.rb', line 235 def hash_to_save_data( hash ) self.class.allocate.reset.update( hash ).to_save_data end |
#load(filepath) ⇒ Cuboid::Options
Loads a file created by #save.
175 176 177 |
# File 'lib/cuboid/options.rb', line 175 def load( filepath ) update( YAML.load_file( filepath ) ) end |
#reset ⇒ Options
Restores everything to their default values.
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/cuboid/options.rb', line 101 def reset # nil everything out. instance_variables.each { |var| instance_variable_set( var.to_s, nil ) } # Set fresh option groups. group_classes.each do |name, klass| instance_variable_set "@#{name}".to_sym, klass.new end @authorized_by = nil self end |
#rpc_data_to_hash(hash) ⇒ Hash
Returns ‘hash` in #to_hash format.
221 222 223 224 |
# File 'lib/cuboid/options.rb', line 221 def rpc_data_to_hash( hash ) self.class.allocate.reset.update( hash ).to_hash. reject { |k| TO_RPC_IGNORE.include? k } end |
#save(file) ⇒ Object
154 155 156 157 158 159 |
# File 'lib/cuboid/options.rb', line 154 def save( file ) File.open( file, 'w' ) do |f| f.write to_save_data f.path end end |
#to_hash ⇒ Hash Also known as: to_h
Returns ‘self` converted to a Hash.
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/cuboid/options.rb', line 201 def to_hash hash = {} instance_variables.each do |var| val = instance_variable_get( var ) var = normalize_name( var ) next if TO_HASH_IGNORE.include?( var ) hash[var] = (val.is_a? OptionGroup) ? val.to_h : val end hash.deep_clone end |
#to_rpc_data ⇒ Hash
Returns ‘self` converted to a Hash suitable for RPC transmission.
181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/cuboid/options.rb', line 181 def to_rpc_data hash = {} instance_variables.each do |var| val = instance_variable_get( var ) var = normalize_name( var ) next if TO_RPC_IGNORE.include?( var ) hash[var.to_s] = (val.is_a? OptionGroup) ? val.to_rpc_data : val end hash.deep_clone end |
#to_rpc_data_without_defaults ⇒ Object
194 195 196 197 |
# File 'lib/cuboid/options.rb', line 194 def to_rpc_data_without_defaults defaults = self.class.allocate.reset.to_rpc_data to_rpc_data.reject { |k, v| defaults[k] == v } end |
#to_save_data ⇒ Object
161 162 163 |
# File 'lib/cuboid/options.rb', line 161 def to_save_data to_rpc_data.to_yaml end |
#to_save_data_without_defaults ⇒ Object
165 166 167 |
# File 'lib/cuboid/options.rb', line 165 def to_save_data_without_defaults to_rpc_data_without_defaults.to_yaml end |
#update(options) ⇒ Options Also known as: set
Configures options via a Hash object.
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/cuboid/options.rb', line 127 def update( ) .each do |k, v| k = k.to_sym if group_classes.include? k send( k ).update v else send( "#{k.to_s}=", v ) end end self end |
#validate ⇒ Hash
Returns Hash of errors with the name of the invalid options/groups as the keys.
143 144 145 146 147 148 149 150 |
# File 'lib/cuboid/options.rb', line 143 def validate errors = {} group_classes.keys.each do |name| next if (group_errors = send(name).validate).empty? errors[name] = group_errors end errors end |