Class: DNApi::Struct
- Inherits:
-
Struct
- Object
- Struct
- DNApi::Struct
show all
- Extended by:
- Test::Ext, Forwardable
- Defined in:
- lib/dnapi/struct.rb
Direct Known Subclasses
App, Components::Addons, Components::Addons::Addon, Components::Apache, Components::Cloudkick, Components::EncryptedBackup, Components::Exim, Components::GodMonitor, Components::Nagios, Components::Newrelic, Components::Passenger3, Components::Rubygems, Components::Ssmtp, Components::Stunneled, Components::VersionedComponent, Components::Volume, Cron, DbStack, EbuildDep, Environment, GemDep, Instance, Monitoring, Recipe, SSLCert, Stack, VHost
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Test::Ext
default_fauxture_name, fixture, generate, generate_attributes, pick
Constructor Details
#initialize(attrs = {}) ⇒ Struct
Returns a new instance of Struct.
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/dnapi/struct.rb', line 90
def initialize(attrs = {})
raise(ArgumentError, "#{attrs.inspect} is not a hash") unless attrs.is_a?(Hash)
mattrs = attrs.to_mash
super(*members.map {|m| mattrs.delete(m) })
self::class._many.each do |assoc|
send(:"#{assoc}=", mattrs.delete(assoc) || [])
end
self::class._ones.each do |assoc|
send(:"#{assoc}=", mattrs.delete(assoc) || nil)
end
mattrs.each do |key, value|
send("#{key}=", value)
end
end
|
Instance Attribute Details
#parent ⇒ Object
Returns the value of attribute parent.
15
16
17
|
# File 'lib/dnapi/struct.rb', line 15
def parent
@parent
end
|
Class Method Details
._many ⇒ Object
11
|
# File 'lib/dnapi/struct.rb', line 11
def _many() @_many ||= [] end
|
._ones ⇒ Object
12
|
# File 'lib/dnapi/struct.rb', line 12
def _ones() @_ones ||= [] end
|
._umembers ⇒ Object
13
|
# File 'lib/dnapi/struct.rb', line 13
def _umembers() @_umembers ||= [] end
|
.belongs_to(name) ⇒ Object
80
81
82
83
|
# File 'lib/dnapi/struct.rb', line 80
def self.belongs_to(name)
alias_method name, :parent
alias_method "#{name}=", :parent=
end
|
.from(jexp) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/dnapi/struct.rb', line 30
def self.from(jexp)
_many.each do |assoc|
jexp[assoc.to_s] = (jexp[assoc.to_s] || []).map do |item|
if klass = map[assoc.to_sym]
klass.from(item)
end
end.compact
end
_ones.each do |assoc|
if jexp[assoc.to_s]
jexp[assoc.to_s] = begin
item = jexp[assoc.to_s]
if klass = map[assoc.to_sym]
klass.from(item)
end
end
end
end
new(jexp)
end
|
.many(*names) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/dnapi/struct.rb', line 52
def self.many(*names)
_many.push(*names)
attr_reader(*names)
names.each do |name|
class_eval <<-EOT, __FILE__, __LINE__ + 1
def #{name}=(values)
values.each do |value|
value.parent = self if value.is_a?(DNApi::Struct)
end
@#{name} = values
end
EOT
end
end
|
.one(*names) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/dnapi/struct.rb', line 67
def self.one(*names)
_ones.push(*names)
attr_reader(*names)
names.each do |name|
class_eval <<-EOT, __FILE__, __LINE__ + 1
def #{name}=(value)
value.parent = self if value.is_a?(DNApi::Struct)
@#{name} = value
end
EOT
end
end
|
.unserialized_member(*names) ⇒ Object
85
86
87
88
|
# File 'lib/dnapi/struct.rb', line 85
def self.unserialized_member(*names)
_umembers.push(*names)
attr_accessor(*names)
end
|
Instance Method Details
#==(other) ⇒ Object
134
135
136
137
138
|
# File 'lib/dnapi/struct.rb', line 134
def ==(other)
super && self.class._umembers.all? do |umember|
self.send(umember) == other.send(umember)
end
end
|
#_many ⇒ Object
109
110
111
|
# File 'lib/dnapi/struct.rb', line 109
def _many
self.class._many
end
|
#_many_values ⇒ Object
117
118
119
|
# File 'lib/dnapi/struct.rb', line 117
def _many_values
_many.map {|assoc| send(assoc)}
end
|
#_ones ⇒ Object
113
114
115
|
# File 'lib/dnapi/struct.rb', line 113
def _ones
self.class._ones
end
|
#_ones_values ⇒ Object
121
122
123
|
# File 'lib/dnapi/struct.rb', line 121
def _ones_values
_ones.map {|assoc| send(assoc)}
end
|
#inspect ⇒ Object
140
141
142
|
# File 'lib/dnapi/struct.rb', line 140
def inspect
super.gsub(/>$/, ' ' + umember_inspect + '>')
end
|
#to_hash ⇒ Object
125
126
127
128
129
130
131
132
|
# File 'lib/dnapi/struct.rb', line 125
def to_hash
data = members.zip(values)
association_data = _many.zip(_many_values) + _ones.zip(_ones_values)
hash = (data + association_data).inject({}) do |h,(k,v)|
h.update(k => v)
end hash
end
|
#umember_inspect ⇒ Object
144
145
146
|
# File 'lib/dnapi/struct.rb', line 144
def umember_inspect
self.class._umembers.map {|u| u.to_s + '=' + send(u).inspect } * ' '
end
|