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 = {})
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
|