Class: Occi::Core::Attributes

Inherits:
AttributeProperties show all
Defined in:
lib/occi/core/attributes.rb

Instance Method Summary collapse

Methods inherited from AttributeProperties

#[], #combine, parse, #remove, split

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


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/occi/core/attributes.rb', line 35

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::Attribute
          self[match[1]] = args.first
        when Hash
          self[match[1]] = Occi::Core::Attribute.new(args.first)
        else
          if self[match[1]].kind_of? Occi::Core::Attribute
            self[match[1]]._value = args.first
          else
            self[match[1]] = Occi::Core::Attribute.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

Instance Method Details

#[]=(key, value) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/occi/core/attributes.rb', line 5

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

#as_json(options = {}) ⇒ Hashie::Mash

Returns json representation

Parameters:

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

Returns:

  • (Hashie::Mash)

    json representation


89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/occi/core/attributes.rb', line 89

def as_json(options={})
  hash = {}
  self.each_pair do |key, value|
    case value
      when Occi::Core::Attribute
        hash[key] = value._value
      else
        hash[key] = value.as_json options
    end
  end
  hash
end

#convert_value(val, duping = false) ⇒ Object

:nodoc:


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/occi/core/attributes.rb', line 63

def convert_value(val, duping=false) #:nodoc:
  case val
    when Occi::Core::AttributeProperties
      Occi::Core::Attributes.new 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
      Occi::Core::Attribute.new val
    else
      val
  end
end

#to_json(*a) ⇒ Object


83
84
85
# File 'lib/occi/core/attributes.rb', line 83

def to_json(*a)
  as_json(*a).to_json(*a)
end