Module: Fog::Attributes::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#_load(marshalled) ⇒ Object



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

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

#aliasesObject



9
10
11
# File 'lib/fog/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
100
101
# File 'lib/fog/attributes.rb', line 17

def attribute(name, options = {})
  # FIXME: handle legacy where options would have been one or more aliases
  if !options.is_a?(Hash)
    options = {:aliases => options}
  end
  class_eval <<-EOS, __FILE__, __LINE__
    attr_reader :#{name}
  EOS
  case options[:type]
  when :boolean
    class_eval <<-EOS, __FILE__, __LINE__
      def #{name}=(new_#{name})
        @#{name} = case new_#{name}
        when 'true'
          true
        when 'false'
          false
        end
      end
    EOS
  when :float
    class_eval <<-EOS, __FILE__, __LINE__
      def #{name}=(new_#{name})
        @#{name} = new_#{name}.to_f
      end
    EOS
  when :integer
    class_eval <<-EOS, __FILE__, __LINE__
      def #{name}=(new_#{name})
        @#{name} = new_#{name}.to_i
      end
    EOS
  when :string
    class_eval <<-EOS, __FILE__, __LINE__
      def #{name}=(new_#{name})
        @#{name} = new_#{name}.to_s
      end
    EOS
  when :time
    class_eval <<-EOS, __FILE__, __LINE__
      def #{name}=(new_#{name})
        if !new_#{name}.is_a?(Time)
          @#{name} = Time.parse(new_#{name})
        else
          @#{name} = new_#{name}
        end
      end
    EOS
  when :array
    class_eval <<-EOS, __FILE__, __LINE__
    def #{name}=(new_#{name})
      @#{name} = if new_#{name}.is_a?(Array)
        new_#{name}
      else
        @#{name} = [ new_#{name} ]
      end
    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[:#{squash}]
              @#{name} = new_data[:#{squash}]
            else
              @#{name} = [ new_data ]
            end
          else
            @#{name} = new_data
          end
        end
      EOS
    else
      class_eval <<-EOS, __FILE__, __LINE__
        attr_writer :#{name}
      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/attributes.rb', line 13

def attributes
  @attributes ||= []
end

#identity(name, other_names = []) ⇒ Object



103
104
105
106
# File 'lib/fog/attributes.rb', line 103

def identity(name, other_names = [])
  @identity = name
  self.attribute(name, {:aliases => other_names})
end

#ignore_attributes(*args) ⇒ Object



108
109
110
# File 'lib/fog/attributes.rb', line 108

def ignore_attributes(*args)
  @ignored_attributes = args
end

#ignored_attributesObject



112
113
114
# File 'lib/fog/attributes.rb', line 112

def ignored_attributes
  @ignored_attributes ||= []
end