active_repository 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/lib/active_repository/base.rb +21 -7
- data/lib/active_repository/uniqueness.rb +2 -1
- data/lib/active_repository/version.rb +1 -1
- data/lib/active_repository/write_support.rb +8 -0
- data/spec/support/shared_examples.rb +6 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4066b2f88bcb0854427c407b27da4ae0a17a5b92
|
4
|
+
data.tar.gz: a39ab7fe4b4dd35ccd0af5fcbf1a8e3c9ab67376
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d47c8c5572ce3b52e1d0c4c4dbe7871777a5805995008251152cd03b658a227dfe8931f4562124a995170fa6129879db7c8c7d922d68d944c3e4411b1e99d6ef
|
7
|
+
data.tar.gz: 7626c6fc598178aad7104ac083f0e174bf7e274e25c64a43e273e197652d0d765faa8ec0d060953ee12871b329e70de28311b38bcdf90924bb84ac8fdae91ad1
|
data/Gemfile
CHANGED
@@ -50,7 +50,8 @@ module ActiveRepository
|
|
50
50
|
include ActiveRepository::Associations
|
51
51
|
include ActiveRepository::Writers::InstanceMethods
|
52
52
|
|
53
|
-
class_attribute :model_class, :instance_writer => false
|
53
|
+
class_attribute :model_class, :before_save_methods, :after_save_methods, :instance_writer => false
|
54
|
+
class_attribute :before_create_methods, :after_create_methods, :instance_writer => false
|
54
55
|
class_attribute :save_in_memory, :postfix, :instance_writer => true
|
55
56
|
|
56
57
|
after_validation :set_timestamps
|
@@ -60,6 +61,14 @@ module ActiveRepository
|
|
60
61
|
(repository? ? super : PersistenceAdapter.all(self).map { |object| serialize!(object.attributes) })
|
61
62
|
end
|
62
63
|
|
64
|
+
def self.before_save(*methods, options)
|
65
|
+
self.before_save_methods = ((before_save_methods || []) + methods).flatten
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.after_save(*methods, options)
|
69
|
+
self.after_save_methods = ((after_save_methods || []) + methods).flatten
|
70
|
+
end
|
71
|
+
|
63
72
|
# Constantize class name
|
64
73
|
def self.constantize
|
65
74
|
self.to_s.constantize
|
@@ -194,21 +203,26 @@ module ActiveRepository
|
|
194
203
|
end
|
195
204
|
|
196
205
|
def save(force=false)
|
206
|
+
(before_save_methods || []).each { |method| self.send(method) }
|
207
|
+
result = true
|
208
|
+
|
197
209
|
if self.class == persistence_class
|
198
210
|
object = persistence_class.where(id: self.id).first_or_initialize
|
199
211
|
|
200
|
-
if force || self.id.nil?
|
212
|
+
result = if force || self.id.nil?
|
201
213
|
self.id = nil if self.id.nil?
|
202
214
|
super
|
203
215
|
elsif self.valid?
|
204
|
-
object.attributes = self.attributes
|
216
|
+
object.attributes = self.attributes.select{ |key, value| self.class.serialized_attributes.include?(key.to_s) }
|
205
217
|
object.save(true)
|
206
218
|
end
|
207
|
-
|
208
|
-
self.valid?
|
209
219
|
else
|
210
|
-
self.persist
|
220
|
+
result = self.persist
|
211
221
|
end
|
222
|
+
|
223
|
+
(after_save_methods || []).each { |method| self.send(method) }
|
224
|
+
|
225
|
+
result
|
212
226
|
end
|
213
227
|
|
214
228
|
# Updates attributes from self with the attributes from the parameters
|
@@ -232,7 +246,7 @@ module ActiveRepository
|
|
232
246
|
|
233
247
|
object ||= persistence_class.new
|
234
248
|
|
235
|
-
attributes = self.attributes
|
249
|
+
attributes = self.attributes.select{ |key, value| self.class.serialized_attributes.include?(key.to_s) }
|
236
250
|
|
237
251
|
attributes.delete(:id)
|
238
252
|
|
@@ -11,7 +11,8 @@ module ActiveModel
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def validate_each(record, attribute, value)
|
14
|
-
|
14
|
+
query = (record.id ? "id <> #{record.id}" : 'id is not null')
|
15
|
+
duplicate = record.class.where(query).all.select do |object|
|
15
16
|
object.id != record.id && object.send(attribute) == record.send(attribute)
|
16
17
|
end
|
17
18
|
|
@@ -19,6 +19,14 @@ end
|
|
19
19
|
|
20
20
|
module ActiveHash
|
21
21
|
class Base
|
22
|
+
def initialize(attributes = {})
|
23
|
+
attributes = attributes.symbolize_keys
|
24
|
+
@attributes = attributes
|
25
|
+
attributes.dup.each do |key, value|
|
26
|
+
send "#{key}=", value
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
22
30
|
def self.insert(record)
|
23
31
|
record_id = record.id.to_s
|
24
32
|
record_hash = record.hash
|
@@ -88,6 +88,12 @@ shared_examples ".where" do
|
|
88
88
|
results.last.name.should == "UK"
|
89
89
|
end
|
90
90
|
|
91
|
+
it "finds elements which id's are not null" do
|
92
|
+
results = Country.where('id is not "null"').all
|
93
|
+
|
94
|
+
results.size.should == 5
|
95
|
+
end
|
96
|
+
|
91
97
|
it "filters the records from an AR-like conditions hash" do
|
92
98
|
first_id = Country.first.id
|
93
99
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_repository
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Caio Torres
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03
|
11
|
+
date: 2014-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_hash
|