active_repository 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjliYTU5ODNhMzMyZThjNWI4MGRjYmJkMTI2ZmRmYzgyOTFkZGQ4OA==
4
+ ZDhkNGUzNGNjOTYzNzkwNWUzMTU4NzhkZmY0Yjc1NGZiOWZmYjlkNQ==
5
5
  data.tar.gz: !binary |-
6
- NWJjYzEwNmVjN2NlYjVkNTM0ZTI1NjczYTZhOTA2MGU2Y2JhMjc1YQ==
6
+ MWI4YjQ2ZjU0NTM5NTQ5ZjY3ZTM5MTMyZDhlYThmZDg3MmFhMDJkOA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NWFlMDExYTJmOGFmYzY1NzAxZmFjMDJiZDIwYmI5ZWNhMzhjMjI2NzkwM2Ey
10
- NmVmODhlMzljMzAzYmU4YmJjNzZhNzAzYmVjOWZlMzhmYmIyNmZmOTk0NGNl
11
- OTE2NDQxOGUzMzY4ZjM5MjZiYWM0MDEyMjk3ZmIwYTg5MmJjNDQ=
9
+ NTU0NzhkNzU4ZjdhOTU0ODFkYmYwOTg4NzVjZDhlYjRmYWEwMmZjNjg5NGFl
10
+ YTE1ZTQyNTAzYTE0NTJiYTNlYjU2ZDRjNTI5ODNlYTQ5NTU1YmRjZDc2NTA4
11
+ MjA0ODg5MTMzYjkxNzU1MDZmNjRiNDI5ZDkyMjQ0ZTJiZTRjNWY=
12
12
  data.tar.gz: !binary |-
13
- YWZkYTZjMGU2ODQ4OGNhNWJjMGZlMTY0ZWJkYTY0YTk0Y2EwNjFjN2JhOTFm
14
- ODI3NzdjNDliMmQ4N2NkZjVhZmExNzU4ODdlNTEwZTNmYTdmZmVlZDk2MTY2
15
- ZTIyNDNmNjVlMjQzMjliNjE4NzJlZWIyOTYwZmJkODRjOGUxNWQ=
13
+ YjkzYjY4OGRmZjc0MjQzN2M2NmVjODE3YWQzNTk5NTdhZDhhMWM2ZGI3NDUy
14
+ MjQwYjZjZmRiMDc2ODYzOWNiMDlhYmVlNTYyMzUwZWMyYzAzYTlmYTkwZWJk
15
+ M2UyYjIxZGM0M2IzZTdjMDBmYTJmNWQxN2ExMzIxMTNmMzM5N2Q=
@@ -4,6 +4,11 @@ class DefaultAdapter
4
4
  klass.get_model_class.all
5
5
  end
6
6
 
7
+ def delete(klass, id)
8
+ object = klass.get_model_class.find_by_id(id)
9
+ object.delete if object
10
+ end
11
+
7
12
  def delete_all(klass)
8
13
  klass.get_model_class.delete_all
9
14
  end
@@ -25,6 +25,10 @@ class PersistenceAdapter
25
25
  get_adapter(klass).create(klass, attributes)
26
26
  end
27
27
 
28
+ def delete(klass, id)
29
+ get_adapter(klass).delete(klass, id)
30
+ end
31
+
28
32
  def delete_all(klass)
29
33
  get_adapter(klass).delete_all(klass)
30
34
  end
@@ -183,8 +183,10 @@ module ActiveRepository
183
183
  self.id = nil if self.id.nil?
184
184
  super
185
185
  else
186
+ object.attributes = self.attributes
186
187
  object.save(true)
187
188
  end
189
+
188
190
  true
189
191
  else
190
192
  self.persist
@@ -1,3 +1,3 @@
1
1
  module ActiveRepository
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -23,16 +23,23 @@ module ActiveHash
23
23
  record_id = record.id.to_s
24
24
  record_hash = record.hash
25
25
 
26
- if self.all.map(&:hash).include?(record_hash)
27
- record_index.delete(record_id)
28
- self.all.delete(record)
29
- end
26
+ remove(record)
30
27
 
31
28
  if record_index[record_id].nil? || !self.all.map(&:hash).include?(record_hash)
32
29
  insert_record(record)
33
30
  end
34
31
  end
35
32
 
33
+ def self.remove(record)
34
+ record_id = record.id.to_s
35
+ record_hash = record.hash
36
+
37
+ if self.all.map(&:hash).include?(record_hash)
38
+ record_index.delete(record_id)
39
+ self.all.delete(record)
40
+ end
41
+ end
42
+
36
43
  def self.where(query)
37
44
  if query.is_a?(String)
38
45
  return ActiveHash::SQLQueryExecutor.execute(self, query)
@@ -69,6 +76,14 @@ module ActiveHash
69
76
  end
70
77
  end
71
78
 
79
+ def delete
80
+ record = self.class.find_by_id(self.id)
81
+
82
+ self.class.remove(self)
83
+
84
+ self.class.find_by_id(self.id).nil?
85
+ end
86
+
72
87
  def to_param
73
88
  id.present? ? id.to_s : nil
74
89
  end
@@ -51,6 +51,16 @@ module ActiveRepository
51
51
  end
52
52
  end
53
53
 
54
+ # Deletes self from the repository.
55
+ def delete
56
+ klass = self.class
57
+ if klass.get_model_class == klass
58
+ super
59
+ else
60
+ PersistenceAdapter.delete(klass, self.id)
61
+ end
62
+ end
63
+
54
64
  # Updates #key attribute with #value value.
55
65
  def update_attribute(key, value)
56
66
  ret = self.valid?
@@ -631,6 +631,22 @@ shared_examples ".transaction" do
631
631
  end
632
632
  end
633
633
 
634
+ shared_examples "#delete" do
635
+ before do
636
+ Country.delete_all
637
+ end
638
+
639
+ it "removes a record" do
640
+ country = Country.create
641
+
642
+ Country.size.should == 1
643
+
644
+ country.delete
645
+
646
+ Country.size.should == 0
647
+ end
648
+ end
649
+
634
650
  shared_examples ".delete_all" do
635
651
  before do
636
652
  Country.delete_all
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.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caio Torres
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-28 00:00:00.000000000 Z
11
+ date: 2013-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_hash