Class: UsesAttributes

Inherits:
Object
  • Object
show all
Defined in:
lib/hosties/Reification.rb

Overview

Fancy words, fancy words. Provide some classes to turn a declarative host definition into something more useful in code, applying rules from the definition files to ensure we only get valid stuff.

Direct Known Subclasses

EnvironmentBuilder, HostBuilder

Instance Method Summary collapse

Constructor Details

#initialize(has_attributes) ⇒ UsesAttributes

Returns a new instance of UsesAttributes.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hosties/Reification.rb', line 12

def initialize(has_attributes)
  @attributes = has_attributes.attributes
  # Magic.
  has_attributes.attributes.each do |attr|
    # Add in the attribute
    self.metaclass.send(:attr_accessor, attr)
    # Define a constrained setter
    self.metaclass.send(:define_method, attr) do |val|
      raise ArgumentError, "Invalid value" unless has_attributes.valid?(attr, val)
      instance_variable_set "@#{attr}", val
    end
  end
end

Instance Method Details

#finishObject

Return a hash after verifying everything was set correctly



27
28
29
30
31
32
33
34
35
36
# File 'lib/hosties/Reification.rb', line 27

def finish
  retval = {}
  # Ensure all required attributes have been set
  @attributes.each do |attr|
    val = instance_variable_get "@#{attr}"
    raise ArgumentError, "Missing attribute #{attr}" if val.nil?
    retval[attr] = val
  end
  retval
end

#metaclassObject

Oh this old thing…



7
8
9
10
11
# File 'lib/hosties/Reification.rb', line 7

def metaclass 
  class << self
    self
  end
end