This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
/
resource.rb
280 lines (222 loc) · 7.41 KB
/
resource.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
require __DIR__ + 'support/inflector'
require __DIR__ + 'support/string'
require __DIR__ + 'property_set'
require __DIR__ + 'property'
require __DIR__ + 'repository'
require __DIR__ + 'hook'
require __DIR__ + 'associations/relationship'
require __DIR__ + 'associations/association_set'
require __DIR__ + 'associations/belongs_to'
require __DIR__ + 'associations/has_one'
require __DIR__ + 'associations/has_many'
require __DIR__ + 'associations/has_and_belongs_to_many'
module DataMapper
module Resource
# +----------------------
# Resource module methods
def self.included(target)
target.send(:extend, ClassMethods)
target.send(:extend, DataMapper::Hook::ClassMethods)
target.send(:include, DataMapper::Hook)
target.instance_variable_set("@resource_names", Hash.new { |h,k| h[k] = repository(k).adapter.resource_naming_convention.call(target.name) })
target.instance_variable_set("@properties", Hash.new { |h,k| h[k] = (k == :default ? PropertySet.new : h[:default].dup) })
# Associations:
target.send(:extend, DataMapper::Associations::BelongsTo)
target.send(:extend, DataMapper::Associations::HasOne)
target.send(:extend, DataMapper::Associations::HasMany)
target.send(:extend, DataMapper::Associations::HasAndBelongsToMany)
end
def self.dependencies
@dependencies = DependencyQueue.new
def self.dependencies
@dependencies
end
@dependencies
end
# +---------------
# Instance methods
def repository
@loaded_set ? @loaded_set.repository : self.class.repository
end
def key
key = []
self.class.key(repository.name).map do |property|
value = instance_variable_get(property.instance_variable_name)
key << value if !value.nil?
end
key
end
def loaded_set
@loaded_set
end
def loaded_set=(value)
@loaded_set = value
end
def readonly!
@readonly = true
end
def readonly?
@readonly == true
end
def save
repository.save(self)
end
def destroy
repository.destroy(self)
end
def attribute_loaded?(name)
instance_variables.include?(name.to_s.ensure_starts_with('@'))
end
def dirty_attributes
@dirty_attributes || @dirty_attributes = Hash.new
end
def dirty?
!@dirty_attributes.blank?
end
def attribute_dirty?(name)
raise ArgumentError.new("#{name.inspect} should be a Symbol") unless name.is_a?(Symbol)
dirty_attributes.include?(name)
end
def attribute_get(name)
unless attribute_loaded?(name)
lazy_load!(name)
end
instance_variable_get(name.to_s.ensure_starts_with('@'))
end
def attribute_set(name, value)
dirty_attributes[name] = instance_variable_set(name.to_s.ensure_starts_with('@'), value)
end
def lazy_load!(*names)
unless new_record? || @loaded_set.nil?
@loaded_set.reload!(:fields => names)
else
names.each { |name| instance_variable_set(name.to_s.ensure_starts_with('@'), nil) }
end
end
def initialize(details = nil) # :nodoc:
validate_resource!
def initialize(details = nil)
if details
initialize_with_attributes(details)
end
end
initialize(details)
end
def initialize_with_attributes(details) # :nodoc:
case details
when Hash then self.attributes = details
when Resource, Struct then self.private_attributes = details.attributes
# else raise ArgumentError.new("details should be a Hash, Resource or Struct\n\t#{details.inspect}")
end
end
def validate_resource! # :nodoc:
if self.class.properties(self.class.default_repository_name).empty?
raise IncompleteResourceError.new("Resources must have at least one property to be initialized.")
end
end
def self.===(other)
other.is_a?(Module) && other.ancestors.include?(Resource)
end
# Returns <tt>true</tt> if this model hasn't been saved to the
# database, <tt>false</tt> otherwise.
def new_record?
@new_record.nil? || @new_record
end
def attributes
pairs = {}
self.class.properties(repository.name).each do |property|
if property.reader_visibility == :public
pairs[property.name] = send(property.getter)
end
end
pairs
end
# Mass-assign mapped fields.
def attributes=(values_hash)
values_hash.each_pair do |k,v|
setter = k.to_s.sub(/\?$/, '').ensure_ends_with('=')
# We check #public_methods and not Class#public_method_defined? to
# account for singleton methods.
if public_methods.include?(setter)
send(setter, v)
end
end
end
private
def private_attributes
pairs = {}
self.class.properties(repository.name).each do |property|
pairs[property.name] = send(property.getter)
end
pairs
end
def private_attributes=(values_hash)
values_hash.each_pair do |k,v|
setter = k.to_s.sub(/\?$/, '').ensure_ends_with('=')
if respond_to?(setter) || private_methods.include?(setter)
send(setter, v)
end
end
end
public
module ClassMethods
def repository(name = default_repository_name)
DataMapper::repository(name)
end
def default_repository_name
:default
end
def resource_name(repository_name)
@resource_names[repository_name]
end
def resource_names
@resource_names
end
def property(name, type, options = {})
property = Property.new(self, name, type, options)
properties(repository.name) << property
# Add property to the other mappings as well if this is for the default repository.
if repository.name == default_repository_name
@properties.each_pair do |repository_name, properties|
next if repository_name == default_repository_name
properties << property
end
end
property
end
def properties(repository_name)
@properties[repository_name]
end
def key(repository_name)
@properties[repository_name].key
end
def inheritance_property(repository_name)
@properties[repository_name].detect { |property| property.type == Class }
end
def get(key)
repository.get(self, key.is_a?(Array) ? key : [key])
end
def [](key)
get(key) || raise(ObjectNotFoundError, "Could not find #{self.name} with key: #{key.inspect}")
end
def all(options = {})
repository(options[:repository] || default_repository_name).all(self, options)
end
def first(options = {})
repository(options[:repository] || default_repository_name).first(self, options)
end
def create(values)
instance = new(values)
[instance, instance.save]
end
# TODO SPEC
def copy(source, destination, options = {})
repository(destination) do
repository(source).all(self, options).each do |instance|
self.create(instance)
end
end
end
end
end
end