Class: Mystro::Cloud::Model
- Inherits:
-
Object
- Object
- Mystro::Cloud::Model
show all
- Defined in:
- lib/mystro/cloud/model.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hash = {}) ⇒ Model
Returns a new instance of Model.
8
9
10
11
|
# File 'lib/mystro/cloud/model.rb', line 8
def initialize(hash={})
@data = {}
load(hash)
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
6
7
8
|
# File 'lib/mystro/cloud/model.rb', line 6
def data
@data
end
|
Class Method Details
.alias_for(name) ⇒ Object
118
119
120
|
# File 'lib/mystro/cloud/model.rb', line 118
def alias_for(name)
@aliases[name]
end
|
.attribute_for(name) ⇒ Object
114
115
116
|
# File 'lib/mystro/cloud/model.rb', line 114
def attribute_for(name)
@attributes[name] || @attributes[name] || @aliases[name] && @attributes[@aliases[name]]
end
|
.class_for(name) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/mystro/cloud/model.rb', line 122
def class_for(name)
n = name.to_s.capitalize
begin
return "Mystro::Cloud::#{n}".constantize
rescue => e
begin
return n.constantize
rescue => e
raise "couldn't find class: #{n}: #{e.message}"
end
end
end
|
.normal_key(k) ⇒ Object
135
136
137
|
# File 'lib/mystro/cloud/model.rb', line 135
def normal_key(k)
k.to_s.downcase.to_sym
end
|
.validate!(data) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/mystro/cloud/model.rb', line 99
def validate!(data)
@attributes.each do |k, v|
type = v[:type]
type = type.is_a?(String) ? class_for(type) : type
value = data[k]
value = value.to_s if value.is_a?(Symbol)
raise "#{k} required but is nil or false" if v[:required] && !value
raise "#{k} required but empty" if v[:required] && type == Array && value.empty?
raise "incorrect type: #{k}: '#{value.inspect}' is not a '#{type}'" if value && !value.is_a?(type)
end
rescue => e
raise "#{e.message}: #{data.inspect}"
end
|
Instance Method Details
#[](key) ⇒ Object
74
75
76
|
# File 'lib/mystro/cloud/model.rb', line 74
def [](key)
send(key.to_sym)
end
|
#[]=(key, value) ⇒ Object
77
78
79
|
# File 'lib/mystro/cloud/model.rb', line 77
def []=(key, value)
send("#{key}=", value)
end
|
#alias_for(name) ⇒ Object
62
63
64
|
# File 'lib/mystro/cloud/model.rb', line 62
def alias_for(name)
self.class.alias_for(name)
end
|
#attribute_for(name) ⇒ Object
58
59
60
|
# File 'lib/mystro/cloud/model.rb', line 58
def attribute_for(name)
self.class.attribute_for(name)
end
|
#class_for(name) ⇒ Object
70
71
72
|
# File 'lib/mystro/cloud/model.rb', line 70
def class_for(name)
self.class.class_for(name)
end
|
#load(hash) ⇒ Object
13
14
15
16
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
|
# File 'lib/mystro/cloud/model.rb', line 13
def load(hash)
if hash.is_a?(Hash)
hash.each do |k, v|
k = normal_key(k)
k = alias_for(k) || k
attr = attribute_for(k)
raise "#{self.class.name}: attr not found for #{k.inspect}" unless attr
if attr[:child]
if attr[:type] == Array
next unless v v = [v] unless v.is_a?(Array)
@data[k] ||= []
v.each do |e|
if e.is_a?(Mystro::Cloud::Model)
@data[k] << e
else
c = class_for(attr[:of])
@data[k] << c.new(e)
end
end
else
c = class_for(attr[:type])
if v.class == c
@data[k] = v
else
@data[k] = c.new(v)
end
end
elsif attr[:type] == :boolean
@data[k] = v && (v == true || v == "true")
else
@data[k] = v
end
end
else
hash
end
end
|
#normal_key(k) ⇒ Object
66
67
68
|
# File 'lib/mystro/cloud/model.rb', line 66
def normal_key(k)
self.class.normal_key(k)
end
|
#to_hash ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/mystro/cloud/model.rb', line 81
def to_hash
out = {}
@data.each do |k, v|
if v.respond_to?(:to_hash)
out[k] = v.to_hash
elsif v.is_a?(Array)
out[k] = v.map {|e| e.respond_to?(:to_hash) ? e.to_hash : e}
else
out[k] = v
end
end
out
end
|
#validate! ⇒ Object
54
55
56
|
# File 'lib/mystro/cloud/model.rb', line 54
def validate!
self.class.validate!(@data)
end
|