Class: CFoundry::V1::Model
- Inherits:
-
Object
- Object
- CFoundry::V1::Model
show all
- Extended by:
- ModelMagic
- Defined in:
- lib/cfoundry/v1/model.rb
Constant Summary
collapse
- @@objects =
{}
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from ModelMagic
attribute, define_client_methods, on_base_client, on_client, read_locations, read_only_attributes, write_locations
Constructor Details
#initialize(guid, client, manifest = nil) ⇒ Model
Returns a new instance of Model.
46
47
48
49
50
51
|
# File 'lib/cfoundry/v1/model.rb', line 46
def initialize(guid, client, manifest = nil)
@guid = guid
@client = client
@manifest = manifest
@changes = {}
end
|
Class Attribute Details
.base_object_name ⇒ Object
22
23
24
|
# File 'lib/cfoundry/v1/model.rb', line 22
def base_object_name
@base_object_name ||= object_name
end
|
.object_name ⇒ Object
15
16
17
18
19
20
|
# File 'lib/cfoundry/v1/model.rb', line 15
def object_name
@object_name ||=
name.split("::").last.gsub(
/([a-z])([A-Z])/,
'\1_\2').downcase.to_sym
end
|
Instance Attribute Details
#changes ⇒ Object
Returns the value of attribute changes.
44
45
46
|
# File 'lib/cfoundry/v1/model.rb', line 44
def changes
@changes
end
|
#guid ⇒ Object
Returns the value of attribute guid.
44
45
46
|
# File 'lib/cfoundry/v1/model.rb', line 44
def guid
@guid
end
|
Class Method Details
.inherited(klass) ⇒ Object
38
39
40
41
|
# File 'lib/cfoundry/v1/model.rb', line 38
def inherited(klass)
@@objects[klass.object_name] = klass
super
end
|
.objects ⇒ Object
34
35
36
|
# File 'lib/cfoundry/v1/model.rb', line 34
def objects
@@objects
end
|
.plural_base_object_name ⇒ Object
30
31
32
|
# File 'lib/cfoundry/v1/model.rb', line 30
def plural_base_object_name
"#{base_object_name}s"
end
|
.plural_object_name ⇒ Object
26
27
28
|
# File 'lib/cfoundry/v1/model.rb', line 26
def plural_object_name
"#{object_name}s"
end
|
Instance Method Details
#base_object_name ⇒ Object
69
70
71
|
# File 'lib/cfoundry/v1/model.rb', line 69
def base_object_name
@base_object_name ||= self.class.base_object_name
end
|
#changed? ⇒ Boolean
57
58
59
|
# File 'lib/cfoundry/v1/model.rb', line 57
def changed?
!@changes.empty?
end
|
#create! ⇒ Object
82
83
84
85
86
87
88
|
# File 'lib/cfoundry/v1/model.rb', line 82
def create!
@manifest = @client.base.send(:"create_#{base_object_name}", write_manifest)
@guid = read_manifest[guid_name]
true
end
|
#delete! ⇒ Object
96
97
98
99
100
101
102
|
# File 'lib/cfoundry/v1/model.rb', line 96
def delete!
@client.base.send(:"delete_#{base_object_name}", @guid)
@guid = nil
true
end
|
#eql?(other) ⇒ Boolean
Also known as:
==
169
170
171
|
# File 'lib/cfoundry/v1/model.rb', line 169
def eql?(other)
other.is_a?(self.class) && @guid == other.guid
end
|
#exists? ⇒ Boolean
104
105
106
107
108
109
110
|
# File 'lib/cfoundry/v1/model.rb', line 104
def exists?
invalidate!
manifest
true
rescue CFoundry::NotFound
false
end
|
#find_path(hash, path) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/cfoundry/v1/model.rb', line 132
def find_path(hash, path)
return [false, nil] unless hash
first, *rest = path
return [false, nil] unless hash.key?(first)
here = hash[first]
if rest.empty?
[true, here]
else
find_path(here, rest)
end
end
|
#guid_name ⇒ Object
73
74
75
|
# File 'lib/cfoundry/v1/model.rb', line 73
def guid_name
self.class.guid_name
end
|
#hash ⇒ Object
174
175
176
|
# File 'lib/cfoundry/v1/model.rb', line 174
def hash
@guid.hash
end
|
#inspect ⇒ Object
61
62
63
|
# File 'lib/cfoundry/v1/model.rb', line 61
def inspect
"\#<#{self.class.name} '#@guid'>"
end
|
#invalidate! ⇒ Object
77
78
79
80
|
# File 'lib/cfoundry/v1/model.rb', line 77
def invalidate!
@manifest = nil
@changes = {}
end
|
#manifest ⇒ Object
53
54
55
|
# File 'lib/cfoundry/v1/model.rb', line 53
def manifest
@manifest ||= @client.base.send(base_object_name, @guid)
end
|
#object_name ⇒ Object
65
66
67
|
# File 'lib/cfoundry/v1/model.rb', line 65
def object_name
@object_name ||= self.class.object_name
end
|
#put(what, where, path) ⇒ Object
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/cfoundry/v1/model.rb', line 158
def put(what, where, path)
if path.size == 1
where[path.last] = what
elsif name = path.first
where[name] ||= {}
put(what, where[name], path[1..-1])
end
nil
end
|
#read_manifest ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/cfoundry/v1/model.rb', line 112
def read_manifest
read = {}
self.class.read_locations.each do |name, where|
found, val = find_path(manifest, where)
read[name] = val if found
end
self.class.write_locations.each do |name, where|
next if self.class.read_only_attributes.include? name
found, val = find_path(manifest, where)
read[name] = val if found
end
read[guid_name] = @guid
read
end
|
#update! ⇒ Object
90
91
92
93
94
|
# File 'lib/cfoundry/v1/model.rb', line 90
def update!
@client.base.send(:"update_#{base_object_name}", @guid, write_manifest)
true
end
|
#write_manifest(body = read_manifest, onto = {}) ⇒ Object
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/cfoundry/v1/model.rb', line 147
def write_manifest(body = read_manifest, onto = {})
onto[guid_name] = @guid if guid_name
self.class.write_locations.each do |name, where|
next if self.class.read_only_attributes.include? name
put(body[name], onto, where) if body.key?(name)
end
onto
end
|