Class: OCCI::Core::Entity

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/occi/core/entity.rb

Direct Known Subclasses

Link, Resource

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity = nil, default = nil) ⇒ Entity

Returns a new instance of Entity



53
54
55
56
# File 'lib/occi/core/entity.rb', line 53

def initialize(entity=nil, default = nil)
  super(entity, default)
  self.check
end

Class Method Details

.check(attributes, definitions) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/occi/core/entity.rb', line 90

def self.check(attributes, definitions)
  attributes = OCCI::Core::Attributes.new(attributes)
  definitions.each_key do |key|
    properties = definitions[key]
    value = attributes[key] ||= properties[:default]
    if properties.include?(:type)
      raise "required attribute #{key} not found" if value.nil? && properties.required
      next if value.nil? && !properties.required
      case properties.type
        when 'string'
          raise "attribute #{key} with value #{value} from class #{value.class.name} does not match attribute property type #{properties.type}" unless value.kind_of?(String)
          raise "attribute #{key} with length #{value.length} not in range #{properties.minimum}-#{properties.maximum}" unless (properties.minimum..properties.maximum) === value.length if properties.minimum && properties.maximum
        when 'number'
          raise "attribute #{key} value #{value} from class #{value.class.name} does not match attribute property type #{properties.type}" unless value.kind_of?(Numeric)
          raise "attribute #{key} with value #{value} not in range #{properties.minimum}-#{properties.maximum}" unless (properties.minimum..properties.maximum) === value if properties.minimum && properties.maximum
        when 'boolean'
          raise "attribute #{key} value #{value} from class #{value.class.name} does not match attribute property type #{properties.type}" unless !!value == value
      end
      raise "attribute #{key} with value #{value} does not match pattern #{properties.pattern}" if value.to_s.scan(Regexp.new(properties.pattern)).empty? if properties.pattern
      attributes[key] = value
    else
      attributes[key] = check(value, definitions[key])
    end
  end
  attributes.delete_if { |k, v| v.nil? } # remove empty attributes
  return attributes
end

.registerObject

Define appropriate kind



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/occi/core/entity.rb', line 35

def self.register
  data = Hashie::Mash.new
  data[:term] = "entity"
  data[:scheme] = "http://schemas.ogf.org/occi/core#"
  data[:title] = "Entity"
  data.attributes!.occi!.core!.id!.type = "string"
  data.attributes!.occi!.core!.id!.pattern = "[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}"
  data.attributes!.occi!.core!.id!.required = false
  data.attributes!.occi!.core!.id!.mutable = false
  data.attributes!.occi!.core!.title!.type = "string"
  data.attributes!.occi!.core!.title!.pattern = ".*"
  data.attributes!.occi!.core!.title!.required = false
  data.attributes!.occi!.core!.title!.mutable = true

  kind = OCCI::Core::Kind.new(data)
  OCCI::Registry.register(kind)
end

Instance Method Details

#checkObject



84
85
86
87
88
# File 'lib/occi/core/entity.rb', line 84

def check
  definitions = OCCI::Registry.get_by_id(self.kind).attributes if self.kind
  self.mixins.each { |mixin| definitions.merge!(OCCI::Registry.get_by_id(mixin).attributes) if OCCI::Registry.get_by_id(mixin).attributes } if self.mixins
  self.attributes = Entity.check(self.attributes, definitions) if definitions
end

#convert_value(val, duping = false) ⇒ Object

:nodoc:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/occi/core/entity.rb', line 118

def convert_value(val, duping=false) #:nodoc:
  case val
    when self.class
      val.dup
    when ::Hash
      val = val.dup if duping
      self.class.subkey_class.new.merge(val) unless val.kind_of?(Hashie::Mash)
      val
    when Array
      val.collect { |e| convert_value(e) }
    else
      val
  end
end

#idObject



58
59
60
# File 'lib/occi/core/entity.rb', line 58

def id
  return self[:id]
end

#id=(id) ⇒ Object



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

def id=(id)
  self[:id] = id
  self.attributes!.occi!.core!.id = id
end

#locationObject



76
77
78
# File 'lib/occi/core/entity.rb', line 76

def location
  '/' + OCCI::Registry.get_by_id(self.kind).term + '/' + self.id if self.kind
end

#titleObject



67
68
69
# File 'lib/occi/core/entity.rb', line 67

def title
  return self[:title]
end

#title=(title) ⇒ Object



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

def title=(title)
  self[:title] = title
  self.attributes!.occi!.core!.title = title
end

#type_identifierObject



80
81
82
# File 'lib/occi/core/entity.rb', line 80

def type_identifier
  OCCI::Registry.get_by_id(self.kind).type_identifier
end