Class: Occi::Core::Attribute

Inherits:
Struct
  • Object
show all
Includes:
Yell::Loggable
Defined in:
lib/occi/core/attribute.rb

Overview

Represents an attribute commonly used in instances based on `Entity` or `ActionInstance`. In most cases, instances of this class will carry the attribute `value` and the `attribute_definition` used for validation purposes. Attributes without `value` will default to the value specified by `attribute_definition.default`, if present. Attributes without `attribute_definition` are considered invalid.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attribute_definitionAttributeDefinition

definition of this attribute instance

Returns:


14
15
16
# File 'lib/occi/core/attribute.rb', line 14

def attribute_definition
  @attribute_definition
end

#valueObject

value of this attribute instance

Returns:

  • (Object)

    the current value of value


14
15
16
# File 'lib/occi/core/attribute.rb', line 14

def value
  @value
end

Instance Method Details

#defaultObject, NilClass

Gracefully sets `value` for this instance from the default value specified in `attribute_definition`. Only `value` set to `nil` will be replaced, other values will be kept. In case `nil` is the default value, it will be set and reported as a new value. An attempt to change the value will be made only if there is no current value (instance with a `value` but no `attribute_definition` will pass this method without raising an error).

Returns:

  • (Object)

    new value, if changed

  • (NilClass)

    if nothing changed

Raises:


100
101
102
# File 'lib/occi/core/attribute.rb', line 100

def default
  value? ? nil : default!
end

#default!Object

Sets `value` for this instance from the default value specified in `attribute_definition`. This method will OVERWRITE any previous `value` present in this instance. See `#default` for the graceful version.

Returns:

  • (Object)

    new value

Raises:


111
112
113
114
115
116
117
# File 'lib/occi/core/attribute.rb', line 111

def default!
  unless definition?
    raise Occi::Core::Errors::AttributeDefinitionError,
          'There is no definition for this attribute'
  end
  self.value = attribute_definition.default
end

#definition?TrueClass, FalseClass Also known as: attribute_definition?

Checks whether this instance has `attribute_definition` assigned.

Returns:


61
62
63
# File 'lib/occi/core/attribute.rb', line 61

def definition?
  !attribute_definition.nil?
end

#empty?TrueClass, FalseClass

Checks whether this instance is missing both `value` and `attribute_definition`.

Returns:


86
87
88
# File 'lib/occi/core/attribute.rb', line 86

def empty?
  !(definition? || value?)
end

#full?TrueClass, FalseClass

Checks whether this instance has both `value` and `attribute_definition` assigned. For details, see `#definition?` and `#value?`.

Returns:

  • (TrueClass, FalseClass)

    flag indicating the presence of `attribute_definition` and `value`


79
80
81
# File 'lib/occi/core/attribute.rb', line 79

def full?
  definition? && value?
end

#optionally_valueless?TrueClass, FalseClass

Reports whether attribute value is absent (`nil`) but the attribute is optional and this fact can be safely ignored. Helps with rendering decisions.

Returns:


131
132
133
# File 'lib/occi/core/attribute.rb', line 131

def optionally_valueless?
  !value? && definition? && attribute_definition.optional?
end

#reset!NilClass

Resets the value of this attribute instance to `nil`.

Returns:

  • (NilClass)

    always `nil`


122
123
124
# File 'lib/occi/core/attribute.rb', line 122

def reset!
  self.value = nil
end

#valid!Object

Checks whether the `value` assigned to this instance does not violate restrictions defined in `attribute_definition`. Attributes without `attribute_definition` are considered invalid. Attributes without `value` may be considered valid depending on the content on `attribute_definition`. This method will raise an `Occi::Core::Errors::AttributeValidationError` error on failure.

Raises:


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/occi/core/attribute.rb', line 45

def valid!
  unless definition?
    raise Occi::Core::Errors::AttributeValidationError,
          'Attribute is missing a definition'
  end
  unless attribute_definition.respond_to?(:valid!)
    raise Occi::Core::Errors::AttributeDefinitionError,
          'Attribute definition is not capable of validation'
  end

  attribute_definition.valid! value
end

#valid?TrueClass, FalseClass

Checks whether the `value` assigned to this instance does not violate restrictions defined in `attribute_definition`. Attributes without `attribute_definition` are considered invalid. Attributes without `value` may be considered valid depending on the content on `attribute_definition`.

Returns:


24
25
26
27
28
29
30
31
32
33
# File 'lib/occi/core/attribute.rb', line 24

def valid?
  begin
    valid!
  rescue Occi::Core::Errors::AttributeValidationError, Occi::Core::Errors::AttributeDefinitionError => ex
    logger.warn "Attribute invalid: #{ex.message}"
    return false
  end

  true
end

#value?TrueClass, FalseClass

Checks whether this instance has `value` assigned. Attributes in which `nil` is an acceptable value will still be considered valueless, although the validation may pass, see `#valid?` or `#valid!`.

Returns:


71
72
73
# File 'lib/occi/core/attribute.rb', line 71

def value?
  !value.nil?
end