Class: Cuboid::Options

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/cuboid/options.rb

Overview

See Also:

Author:

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptions

Returns a new instance of Options.



94
95
96
# File 'lib/cuboid/options.rb', line 94

def initialize
    reset
end

Instance Attribute Details

#applicationObject

Returns the value of attribute application.



92
93
94
# File 'lib/cuboid/options.rb', line 92

def application
  @application
end

#authorized_byString

Returns E-mail address of the person that authorized the run.

Returns:

  • (String)

    E-mail address of the person that authorized the run.

See Also:

  • HTTP::Client#headers


90
91
92
# File 'lib/cuboid/options.rb', line 90

def authorized_by
  @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_accessorsObject



20
21
22
# File 'lib/cuboid/options.rb', line 20

def self.attr_accessors
    @attr_accessors
end

.group_classesHash<Symbol,OptionGroup>

Returns Option group classes by name.

Returns:



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

Returns:

  • (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_accessorsObject



24
25
26
# File 'lib/cuboid/options.rb', line 24

def attr_accessors
    self.class.attr_accessors
end

#dupObject



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.

Parameters:

Returns:



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.

Parameters:

  • filepath (String)

    Path to the file created by #save.

Returns:



175
176
177
# File 'lib/cuboid/options.rb', line 175

def load( filepath )
    update( YAML.load_file( filepath ) )
end

#resetOptions

Restores everything to their default values.

Returns:



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.

Parameters:

Returns:



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

Parameters:

  • file (String)

    Saves ‘self` to `file` using YAML.



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_hashHash Also known as: to_h

Returns ‘self` converted to a Hash.

Returns:

  • (Hash)

    ‘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_dataHash

Returns ‘self` converted to a Hash suitable for RPC transmission.

Returns:

  • (Hash)

    ‘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_defaultsObject



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_dataObject



161
162
163
# File 'lib/cuboid/options.rb', line 161

def to_save_data
    to_rpc_data.to_yaml
end

#to_save_data_without_defaultsObject



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.

Parameters:

Returns:

See Also:



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cuboid/options.rb', line 127

def update( options )
    options.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

#validateHash

Returns Hash of errors with the name of the invalid options/groups as the keys.

Returns:

  • (Hash)

    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