Class: Occi::Core::AttributeDefinition

Inherits:
Object
  • Object
show all
Extended by:
Helpers::YamlSummoner
Includes:
Helpers::ArgumentValidator, Yell::Loggable
Defined in:
lib/occi/core/attribute_definition.rb

Overview

Contains an attribute definition information. This definition does not carry the name of the attribute or its value. These should be associated with the definition by other means, e.g. in a hash `{ name: definition }` or by using instances of the `Occi::Core::Attribute` class.

Author:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::YamlSummoner

from_yaml, needs_dereferencing?

Constructor Details

#initialize(args = {}) ⇒ AttributeDefinition

Constructs an instance with sensible defaults. The default definition is for a String-based optional attribute without a pattern.

Examples:

AttributeDefinition.new type: Integer, required: true
AttributeDefinition.new

Parameters:

  • args (Hash) (defaults to: {})

    arguments with definition information

Options Hash (args):

  • :type (Class) — default: String

    class of the desired value

  • :required (TrueClass, FalseClass) — default: false

    required flag

  • :mutable (TrueClass, FalseClass) — default: true

    mutable flag

  • :default (Object) — default: nil

    default value

  • :description (String, NilClass) — default: nil

    attribute description

  • :pattern (Regexp, NilClass) — default: nil

    value pattern


41
42
43
44
45
46
47
48
49
50
# File 'lib/occi/core/attribute_definition.rb', line 41

def initialize(args = {})
  default_args! args

  @type = args.fetch(:type)
  @required = args.fetch(:required)
  @mutable = args.fetch(:mutable)
  @default = args.fetch(:default)
  @description = args.fetch(:description)
  @pattern = args.fetch(:pattern)
end

Instance Attribute Details

#defaultObject

default value, optional

Returns:

  • (Object)

    the current value of default


18
19
20
# File 'lib/occi/core/attribute_definition.rb', line 18

def default
  @default
end

#descriptionString, NilClass

attribute description, optional

Returns:

  • (String, NilClass)

    the current value of description


18
19
20
# File 'lib/occi/core/attribute_definition.rb', line 18

def description
  @description
end

#mutableTrueClass, FalseClass Also known as: mutable?

mutable flag

Returns:


18
19
20
# File 'lib/occi/core/attribute_definition.rb', line 18

def mutable
  @mutable
end

#patternRegexp, NilClass

value pattern, only for type `String`

Returns:

  • (Regexp, NilClass)

    the current value of pattern


18
19
20
# File 'lib/occi/core/attribute_definition.rb', line 18

def pattern
  @pattern
end

#requiredTrueClass, FalseClass Also known as: required?

required flag

Returns:


18
19
20
# File 'lib/occi/core/attribute_definition.rb', line 18

def required
  @required
end

#typeClass

Ruby class of the desired value

Returns:

  • (Class)

    the current value of type


18
19
20
# File 'lib/occi/core/attribute_definition.rb', line 18

def type
  @type
end

Class Method Details

.allowed_yaml_classesObject

:nodoc:


262
263
264
# File 'lib/occi/core/attribute_definition.rb', line 262

def allowed_yaml_classes
  [String, Regexp, URI, IPAddr, Integer, Float, Boolean, Array, Hash].freeze
end

Instance Method Details

#default?TrueClass, FalseClass

Indicates the presence of a default value.

Examples:

attr_def.default  # => nil
attr_def.default? # => false

Returns:


129
130
131
# File 'lib/occi/core/attribute_definition.rb', line 129

def default?
  !default.nil?
end

#immutable!Object

Changes the value of `mutable` to `false`, in case `mutable` is `nil` or `true`.

Examples:

attr_def.mutable?   # => true
attr_def.immutable!
attr_def.mutable?   # => false

118
119
120
# File 'lib/occi/core/attribute_definition.rb', line 118

def immutable!
  self.mutable = false
end

#immutable?TrueClass, FalseClass

Shorthand for getting the negated value of `mutable`.

Examples:

attr_def.mutable?   # => true
attr_def.immutable? # => false

Returns:


107
108
109
# File 'lib/occi/core/attribute_definition.rb', line 107

def immutable?
  !mutable?
end

#mutable!Object

Changes the value of `mutable` to `true`, in case `mutable` is `nil` or `false`.

Examples:

attr_def.mutable? # => false
attr_def.mutable!
attr_def.mutable? # => true

96
97
98
# File 'lib/occi/core/attribute_definition.rb', line 96

def mutable!
  self.mutable = true
end

#optional!Object

Changes the value of `required` to `false`, in case `required` is `nil` or `true`.

Examples:

attr_def.required? # => true
attr_def.optional!
attr_def.required? # => false

85
86
87
# File 'lib/occi/core/attribute_definition.rb', line 85

def optional!
  self.required = false
end

#optional?TrueClass, FalseClass

Shorthand for getting the negated value of `required`.

Examples:

attr_def.required? # => false
attr_def.optional? # => true

Returns:


74
75
76
# File 'lib/occi/core/attribute_definition.rb', line 74

def optional?
  !required?
end

#pattern?TrueClass, FalseClass

Indicates the presence of a pattern for value.

Examples:

attr_def.pattern  # => /.*/
attr_def.pattern? # => true

Returns:


140
141
142
# File 'lib/occi/core/attribute_definition.rb', line 140

def pattern?
  !pattern.nil?
end

#required!Object

Changes the value of `required` to `true`, in case `required` is `nil` or `false`.

Examples:

attr_def.required? # => false
attr_def.required!
attr_def.required? # => true

63
64
65
# File 'lib/occi/core/attribute_definition.rb', line 63

def required!
  self.required = true
end

#valid!(value) ⇒ Object

Indicates whether the given value is an acceptable value for an attribute with this definition. This method will raise an error if the given value is not acceptable.

Examples:

attr_def.type       # => String
attr_def.value! 0.5 # => Occi::Core::Errors::AttributeValidationError

Parameters:

  • value (Object)

    candidate value


174
175
176
177
# File 'lib/occi/core/attribute_definition.rb', line 174

def valid!(value)
  valid_type!
  valid_value! value
end

#valid?(value) ⇒ TrueClass, FalseClass

Indicates whether the given value is an acceptable value for an attribute with this definition.

Examples:

attr_def.type       # => String
attr_def.value? 5.0 # => false

Parameters:

  • value (Object)

    candidate value

Returns:


153
154
155
156
157
158
159
160
161
162
# File 'lib/occi/core/attribute_definition.rb', line 153

def valid?(value)
  begin
    valid! value
  rescue Occi::Core::Errors::AttributeValidationError => ex
    logger.debug "AttributeValidation: #{ex.message}"
    return false
  end

  true
end