Module: Fog::Attributes::ClassMethods

Included in:
Collection, Model
Defined in:
lib/fog/core/attributes.rb

Instance Method Summary collapse

Instance Method Details

#_load(marshalled) ⇒ Object



5
6
7
# File 'lib/fog/core/attributes.rb', line 5

def _load(marshalled)
  new(Marshal.load(marshalled))
end

#aliasesObject



9
10
11
# File 'lib/fog/core/attributes.rb', line 9

def aliases
  @aliases ||= {}
end

#attribute(name, options = {}) ⇒ Object



17
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fog/core/attributes.rb', line 17

def attribute(name, options = {})
  class_eval <<-EOS, __FILE__, __LINE__
    def #{name}
      attributes[:#{name}]
    end
  EOS
  case options[:type]
  when :boolean
    class_eval <<-EOS, __FILE__, __LINE__
      def #{name}=(new_#{name})
        attributes[:#{name}] = case new_#{name}
        when true,'true'
          true
        when false,'false'
          false
        end
      end
    EOS
  when :float
    class_eval <<-EOS, __FILE__, __LINE__
      def #{name}=(new_#{name})
        attributes[:#{name}] = new_#{name}.to_f
      end
    EOS
  when :integer
    class_eval <<-EOS, __FILE__, __LINE__
      def #{name}=(new_#{name})
        attributes[:#{name}] = new_#{name}.to_i
      end
    EOS
  when :string
    class_eval <<-EOS, __FILE__, __LINE__
      def #{name}=(new_#{name})
        attributes[:#{name}] = new_#{name}.to_s
      end
    EOS
  when :time
    class_eval <<-EOS, __FILE__, __LINE__
      def #{name}=(new_#{name})
        attributes[:#{name}] = if new_#{name}.nil? || new_#{name} == "" || new_#{name}.is_a?(Time)
          new_#{name}
        else
          Time.parse(new_#{name})
        end
      end
    EOS
  when :array
    class_eval <<-EOS, __FILE__, __LINE__
    def #{name}=(new_#{name})
      attributes[:#{name}] = [*new_#{name}]
    end
    EOS
  else
    if squash = options[:squash]
      class_eval <<-EOS, __FILE__, __LINE__
        def #{name}=(new_data)
          if new_data.is_a?(Hash)
            if new_data.has_key?(:'#{squash}')
              attributes[:#{name}] = new_data[:'#{squash}']
            elsif new_data.has_key?("#{squash}")
              attributes[:#{name}] = new_data["#{squash}"]
            else
              attributes[:#{name}] = [ new_data ]
            end
          else
            attributes[:#{name}] = new_data
          end
        end
      EOS
    else
      class_eval <<-EOS, __FILE__, __LINE__
        def #{name}=(new_#{name})
          attributes[:#{name}] = new_#{name}
        end
      EOS
    end
  end
  @attributes ||= []
  @attributes |= [name]
  for new_alias in [*options[:aliases]]
    aliases[new_alias] = name
  end
end

#attributesObject



13
14
15
# File 'lib/fog/core/attributes.rb', line 13

def attributes
  @attributes ||= []
end

#identity(name, options = {}) ⇒ Object



101
102
103
104
# File 'lib/fog/core/attributes.rb', line 101

def identity(name, options = {})
  @identity = name
  self.attribute(name, options)
end

#ignore_attributes(*args) ⇒ Object



106
107
108
# File 'lib/fog/core/attributes.rb', line 106

def ignore_attributes(*args)
  @ignored_attributes = args.collect {|attr| attr.to_s }
end

#ignored_attributesObject



110
111
112
# File 'lib/fog/core/attributes.rb', line 110

def ignored_attributes
  @ignored_attributes ||= []
end