Class: HasAttributes
- Inherits:
-
Object
- Object
- HasAttributes
- Defined in:
- lib/hosties/Definitions.rb
Overview
Superclass for the host and environment requirement types. This class handles the plumbing of tracking, constraining, and eventually reifying attributes.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#constraints ⇒ Object
Returns the value of attribute constraints.
Instance Method Summary collapse
-
#have_attributes(attr, *more) ⇒ Object
(also: #have_attribute)
Specify symbols that will later be reified into attributes.
-
#initialize ⇒ HasAttributes
constructor
A new instance of HasAttributes.
-
#valid?(name, value) ⇒ Boolean
Check if a given name-value pair is valid given the constraints.
-
#where(name) ⇒ Object
Helpful method to define constraints.
Constructor Details
#initialize ⇒ HasAttributes
Returns a new instance of HasAttributes.
29 30 31 32 |
# File 'lib/hosties/Definitions.rb', line 29 def initialize @constraints = {} @attributes = [] end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
28 29 30 |
# File 'lib/hosties/Definitions.rb', line 28 def attributes @attributes end |
#constraints ⇒ Object
Returns the value of attribute constraints.
27 28 29 |
# File 'lib/hosties/Definitions.rb', line 27 def constraints @constraints end |
Instance Method Details
#have_attributes(attr, *more) ⇒ Object Also known as: have_attribute
Specify symbols that will later be reified into attributes
35 36 37 |
# File 'lib/hosties/Definitions.rb', line 35 def have_attributes(attr, *more) @attributes += (more << attr) end |
#valid?(name, value) ⇒ Boolean
Check if a given name-value pair is valid given the constraints
49 50 51 52 53 |
# File 'lib/hosties/Definitions.rb', line 49 def valid?(name, value) if @constraints.include? name then constraints[name].possible_vals.include? value else true end end |
#where(name) ⇒ Object
Helpful method to define constraints
42 43 44 45 46 |
# File 'lib/hosties/Definitions.rb', line 42 def where(name) # Must define the attributes before constraining them raise ArgumentError, "Unknown attribute: #{name}" unless @attributes.include? name @constraints[name] = AttributeConstraint.new(name) end |