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.
@attr type [Class] Ruby class of the desired value @attr required [TrueClass, FalseClass] required flag @attr mutable [TrueClass, FalseClass] mutable flag @attr default [Object] default value, optional @attr description [String, NilClass] attribute description, optional @attr pattern [Regexp, NilClass] value pattern, only for type `String`
@author Boris Parak <parak@cesnet.cz>
Constructs an instance with sensible defaults. The default definition is for a String-based optional attribute without a pattern.
@example
AttributeDefinition.new type: Integer, required: true AttributeDefinition.new
@param args [Hash] arguments with definition information @option args [Class] :type (String) class of the desired value @option args [TrueClass, FalseClass] :required (false) required flag @option args [TrueClass, FalseClass] :mutable (true) mutable flag @option args [Object] :default (nil) default value @option args [String, NilClass] :description (nil) attribute description @option args [Regexp, NilClass] :pattern (nil) value pattern
# 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
Indicates the presence of a default value.
@example
attr_def.default # => nil attr_def.default? # => false
@return [TrueClass, FalseClass] default value indicator
# File lib/occi/core/attribute_definition.rb, line 129 def default? !default.nil? end
Changes the value of `mutable` to `false`, in case `mutable` is `nil` or `true`.
@example
attr_def.mutable? # => true attr_def.immutable! attr_def.mutable? # => false
# File lib/occi/core/attribute_definition.rb, line 118 def immutable! self.mutable = false end
Shorthand for getting the negated value of `mutable`.
@example
attr_def.mutable? # => true attr_def.immutable? # => false
@return [TrueClass, FalseClass] negated value of `mutable`
# File lib/occi/core/attribute_definition.rb, line 107 def immutable? !mutable? end
Changes the value of `mutable` to `true`, in case `mutable` is `nil` or `false`.
@example
attr_def.mutable? # => false attr_def.mutable! attr_def.mutable? # => true
# File lib/occi/core/attribute_definition.rb, line 96 def mutable! self.mutable = true end
Changes the value of `required` to `false`, in case `required` is `nil` or `true`.
@example
attr_def.required? # => true attr_def.optional! attr_def.required? # => false
# File lib/occi/core/attribute_definition.rb, line 85 def optional! self.required = false end
Shorthand for getting the negated value of `required`.
@example
attr_def.required? # => false attr_def.optional? # => true
@return [TrueClass, FalseClass] negated value of `required`
# File lib/occi/core/attribute_definition.rb, line 74 def optional? !required? end
Indicates the presence of a pattern for value.
@example
attr_def.pattern # => /.*/ attr_def.pattern? # => true
@return [TrueClass, FalseClass] pattern indicator
# File lib/occi/core/attribute_definition.rb, line 140 def pattern? !pattern.nil? end
Changes the value of `required` to `true`, in case `required` is `nil` or `false`.
@example
attr_def.required? # => false attr_def.required! attr_def.required? # => true
# File lib/occi/core/attribute_definition.rb, line 63 def required! self.required = true end
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.
@example
attr_def.type # => String attr_def.value! 0.5 # => Occi::Core::Errors::AttributeValidationError
@param value [Object] candidate value
# File lib/occi/core/attribute_definition.rb, line 174 def valid!(value) valid_type! valid_value! value end
Indicates whether the given value is an acceptable value for an attribute with this definition.
@example
attr_def.type # => String attr_def.value? 5.0 # => false
@param value [Object] candidate value @return [TrueClass, FalseClass] validation result
# 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