Class: Fog::CloudSigma::CloudsigmaModel

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/cloudsigma/nested_model.rb

Instance Attribute Summary

Attributes inherited from Model

#collection, #service

Class Method Summary collapse

Methods inherited from Model

#initialize, #inspect, #reload, #symbolize_keys, #to_json, #wait_for

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Fog::Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

This class inherits a constructor from Fog::Model

Class Method Details

.model_attribute(name, model, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fog/cloudsigma/nested_model.rb', line 28

def model_attribute(name, model, options={})
  attributes_key = options[:aliases] || name
  class_eval <<-EOS, __FILE__, __LINE__
    def #{name}
      #{name}_attrs = attributes[:#{attributes_key}]
      if #{name}_attrs
        refreshed_#{name} = #{name}_attrs ?  #{model}.new(#{name}_attrs) : nil
        attributes[:#{attributes_key}] = refreshed_#{name}.attributes

        refreshed_#{name}
      else
        nil
      end
    end
    def #{name}=(new_#{name})
      if new_#{name}
        attributes[:#{attributes_key}] = new_#{name}.kind_of?(Hash) ? new_#{name} : new_#{name}.attributes
      else
        nil
      end
    end
  EOS

  @attributes ||= []
  @attributes |= [name]
  for new_alias in [*options[:aliases]]
    aliases[new_alias] = name
  end
end

.model_attribute_array(name, model, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fog/cloudsigma/nested_model.rb', line 5

def model_attribute_array(name, model, options={})
  attributes_key = options[:aliases] || name
  class_eval <<-EOS, __FILE__, __LINE__
    def #{name}
      #{name}_attrs = attributes[:#{attributes_key}] || []
      refreshed_#{name} = #{name}_attrs.map { |x| #{model}.new(x) }
      attributes[:#{attributes_key}] = refreshed_#{name}.map { |x| x.attributes }

      refreshed_#{name}
    end
    def #{name}=(new_#{name})
      new_#{name} ||= []
      attributes[:#{attributes_key}] = new_#{name}.map { |x| x.kind_of?(Hash) ? x : x.attributes}
    end
  EOS

  @attributes ||= []
  @attributes |= [name]
  for new_alias in [*options[:aliases]]
    aliases[new_alias] = name
  end
end