class Occi::Core::Properties

Constants

PROPERTY_KEYS
SUPPORTED_TYPES

Types supported in properties, and their mapping to Ruby Classes

Attributes

default[RW]
description[RW]
mutable[RW]
mutable?[RW]
pattern[RW]
required[RW]
required?[RW]
type[R]

Public Class Methods

contains_props?(hash) click to toggle source
# File lib/occi/core/properties.rb, line 86
def self.contains_props?(hash)
  # Not a hash == doesn't contain Properties
  return false unless hash.kind_of? Hash
  hash = normalize_props(hash)

  # Are there any Property keys?
  return false if hash.empty?

  # Do all Property keys point to simple values?
  complx_keys = hash.keys.select { |k| hash[k].kind_of?(Hash) }
  return false unless complx_keys.empty?

  true
end
new(source_hash = {}) click to toggle source

@param source_hash [Hash]

# File lib/occi/core/properties.rb, line 21
def initialize(source_hash = {})
  raise ArgumentError, 'Source_hash must be initialized from a hash-like structure!' unless source_hash.kind_of?(Hash)
  raise ArgumentError, 'Source_hash must not be a Hashie::Mash instance!' if source_hash.kind_of?(Hashie::Mash)
  source_hash = Occi::Core::Properties.normalize_props(source_hash)

  self.type = source_hash[:type] || 'string'
  self.required = source_hash[:required].nil? ? false : source_hash[:required]
  self.mutable = source_hash[:mutable].nil? ? true : source_hash[:mutable]
  self.pattern = source_hash[:pattern] || '.*'
  self.description = source_hash[:description]
  self.default = source_hash[:default]
end
normalize_props(hash) click to toggle source
# File lib/occi/core/properties.rb, line 75
def self.normalize_props(hash)
  props = {}

  PROPERTY_KEYS.each do |key|
    found = hash.keys.select { |k| k.to_s.downcase.to_sym == key }.first
    props[key] = hash[found] if found
  end

  props
end

Private Class Methods

supported_type_names() click to toggle source
# File lib/occi/core/properties.rb, line 103
def self.supported_type_names()
  SUPPORTED_TYPES.keys.join(', ')
end

Public Instance Methods

as_json(options={}) click to toggle source
# File lib/occi/core/properties.rb, line 57
def as_json(options={})
  hash = {}

  hash["default"] = self.default if self.default
  hash["type"] = self.type if self.type
  hash["required"] = self.required unless self.required.nil?
  hash["mutable"] = self.mutable unless self.mutable.nil?
  hash["pattern"] = self.pattern if self.pattern
  hash["description"] = self.description if self.description

  hash
end
check_value_for_type(value, key_name = nil) click to toggle source

@param value [Object] Object whose class will be checked against definition

# File lib/occi/core/properties.rb, line 43
def check_value_for_type(value, key_name = nil)
  raise Occi::Errors::AttributeTypeError,
        "Attribute value #{value} for #{key_name.inspect} is class #{value.class.name}. " \
        "It does not match attribute property type #{@type}" unless SUPPORTED_TYPES[@type].any? { |klasse| value.kind_of?(klasse) }
end
empty?() click to toggle source

@return [Bool] Indicating whether this set of properties is “empty”, i.e. no attributes are set

# File lib/occi/core/properties.rb, line 71
def empty?
  as_json.empty?
end
to_hash() click to toggle source
# File lib/occi/core/properties.rb, line 49
def to_hash
  as_json
end
to_json(*a) click to toggle source
# File lib/occi/core/properties.rb, line 53
def to_json(*a)
  as_json(*a).to_json(*a)
end
type=(type) click to toggle source

@param type [String] Requested attribute type

# File lib/occi/core/properties.rb, line 35
def type=(type)
  raise Occi::Errors::AttributePropertyTypeError,
        "Type \"#{type}\" unsupported in properties. Supported " \
        "types are: #{Properties.supported_type_names}." unless SUPPORTED_TYPES.key?(type)
  @type = type
end