Class: HasAttributes

Inherits:
Object
  • Object
show all
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

EnvironmentRequirement, HostRequirement

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHasAttributes

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

#attributesObject

Returns the value of attribute attributes.



28
29
30
# File 'lib/hosties/Definitions.rb', line 28

def attributes
  @attributes
end

#constraintsObject

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

Returns:

  • (Boolean)


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

Raises:

  • (ArgumentError)


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