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/rackspace-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
|