Class: UsesAttributes
- Inherits:
-
Object
- Object
- UsesAttributes
- 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
Instance Method Summary collapse
-
#finish ⇒ Object
Return a hash after verifying everything was set correctly.
-
#initialize(has_attributes) ⇒ UsesAttributes
constructor
A new instance of UsesAttributes.
-
#metaclass ⇒ Object
Oh this old thing…
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..send(:attr_accessor, attr) # Define a constrained setter self..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
#finish ⇒ Object
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 |
#metaclass ⇒ Object
Oh this old thing…
7 8 9 10 11 |
# File 'lib/hosties/Reification.rb', line 7 def class << self self end end |