Class: Occi::Core::AttributeProperties

Inherits:
Hashie::Mash
  • Object
show all
Includes:
Helpers::Inspect
Defined in:
lib/occi/core/attribute_properties.rb

Direct Known Subclasses

Attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Inspect

#inspect

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &blk) ⇒ Object


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/occi/core/attribute_properties.rb', line 79

def method_missing(method_name, *args, &blk)
  return self.[](method_name, &blk) if key?(method_name)
  match = method_name.to_s.match(/(.*?)([?=!_]?)$/)
  case match[2]
    when "="
      case args.first
        when Occi::Core::AttributeProperty
          self[match[1]] = args.first
        when Hash
          self[match[1]] = Occi::Core::AttributeProperty.new(args.first)
        else
          if self[match[1]].kind_of? Occi::Core::AttributeProperty
            self[match[1]]._value = args.first
          else
            self[match[1]] = Occi::Core::AttributeProperty.new(:value => args.first)
          end
      end
    when "?"
      !!self[match[1]]
    when "!"
      initializing_reader(match[1])
    when "_"
      underbang_reader(match[1])
    else
      default(method_name, *args, &blk)
  end
end

Class Method Details

.parse(has) ⇒ Occi::Core::AttributeProperties

Returns parsed AttributeProperties

Parameters:

  • attributes (Hash)

Returns:


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/occi/core/attribute_properties.rb', line 152

def self.parse(has)
  attributes ||= Occi::Core::AttributeProperties.new
  hash.each_pair do |key, value|
    if [:Type, :Required, :Mutable, :Default, :Description, :Pattern, :type, :required, :mutable, :default, :description, :pattern].any? { |k| value.key?(k) and not value[k].kind_of? Hash }
      value[:type] ||= value[:Type] ||= "string"
      value[:required] ||= value[:Required] ||= false
      value[:mutable] ||= value[:Mutable] ||= false
      value[:default] = value[:Default] if value[:Default]
      value[:description] = value[:Description] if value[:Description]
      value[:pattern] ||= value[:Pattern] ||= ".*"
      value.delete :Type
      value.delete :Required
      value.delete :Mutable
      value.delete :Default
      value.delete :Description
      value.delete :Pattern
      attributes[key] = value
    else
      attributes[key] = self.parse attributes[key]
    end
  end
  attributes
end

.split(attributes) ⇒ Occi::Core::AttributeProperties

Parameters:

  • attributes (Hash)

    key value pair of full attribute names with their corresponding values

Returns:


178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/occi/core/attribute_properties.rb', line 178

def self.split(attributes)
  attribute = AttributeProperties.new
  attributes.each do |name, value|
    Occi::Log.debug "AttributeProperty named #{name}"
    key, _, rest = name.partition('.')
    if rest.empty?
      attribute[key] = value
    else
      attribute.merge! AttributeProperties.new(key => self.split(rest => value))
    end
  end
  return attribute
end

Instance Method Details

#[](key) ⇒ Object


7
8
9
10
11
12
13
14
15
16
# File 'lib/occi/core/attribute_properties.rb', line 7

def [](key)
  if key.to_s.include? '.'
    key, string = key.to_s.split('.', 2)
    attributes = super(key)
    raise "AttributeProperty with key #{key} not found" unless attributes
    attributes[string]
  else
    super(key)
  end
end

#[]=(key, value) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/occi/core/attribute_properties.rb', line 18

def []=(key, value)
  if key.to_s.include? '.'
    key, string = key.to_s.split('.', 2)
    case self[key]
      when Occi::Core::AttributeProperties
        attributes = self[key]
      else
        attributes = Occi::Core::AttributeProperties.new
    end
    attributes[string] = value
    super(key, attributes)
  else
    case value
      when Occi::Core::AttributeProperties
        super(key, value)
      when Occi::Core::AttributeProperty
        super(key, value)
      when Hash
        super(key, Occi::Core::AttributeProperty.new(value))
      else
        case self[key]
          when Occi::Core::AttributeProperty
            self[key]._value = value
          else
            super(key, Occi::Core::AttributeProperty.new(:value => value))
        end
    end
  end
end

#combineArray

TODO: convert combine into names (returning only an array of attribtue names) and move combine to Attributes

Returns:

  • (Array)

    key value pair of full attribute names with their corresponding values


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/occi/core/attribute_properties.rb', line 67

def combine
  hash = {}
  self.each_key do |key|
    if self[key].kind_of? Occi::Core::AttributeProperties
      self[key].combine.each_pair { |k, v| hash[key + '.' + k] = v }
    else
      hash[key] = self[key]
    end
  end
  hash
end

#convert_value(val, duping = false) ⇒ Object

:nodoc:


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/occi/core/attribute_properties.rb', line 107

def convert_value(val, duping=false) #:nodoc:
  case val
    when self.class
      val.dup
    when Hash
      duping ? val.dup : val
    when ::Hash
      val = val.dup if duping
      self.class.new(val)
    when Array
      val.collect { |e| convert_value(e) }
    when Occi::Core::AttributeProperty
      val.clone
    else
      val
  end
end

#remove(attributes) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/occi/core/attribute_properties.rb', line 48

def remove(attributes)
  attributes.keys.each do |key|
    puts key
    if self.keys.include? key
      puts 'includes'
      if self[key].class == Occi::Core::AttributeProperties
        puts self[key]
        self[key].remove attributes[key]
      else
        self.delete(key)
      end
    end
  end
  self
end