active_repository 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +29 -6
- data/lib/active_repository/associations.rb +2 -2
- data/lib/active_repository/base.rb +38 -9
- data/lib/active_repository/finders.rb +5 -3
- data/lib/active_repository/version.rb +1 -1
- data/lib/active_repository/write_support.rb +9 -3
- data/lib/active_repository/writers.rb +32 -23
- data/spec/support/shared_examples.rb +4 -11
- metadata +2 -2
data/README.md
CHANGED
@@ -46,20 +46,43 @@ Or install it yourself as:
|
|
46
46
|
|
47
47
|
## Usage
|
48
48
|
|
49
|
-
|
49
|
+
To use it you should inherit ActiveRepository::Base:
|
50
50
|
|
51
|
-
class User <
|
52
|
-
|
53
|
-
|
51
|
+
class User < ActiveRepository::Base
|
52
|
+
end
|
53
|
+
|
54
|
+
ActiveRepository::Base has two class attributes to help it identify where it is going to persist data
|
55
|
+
|
56
|
+
###model_class
|
57
|
+
|
58
|
+
This attribute is used to identify the class responsible for persisting data, it should be the ActiveRecord model or the Mongoid Document.
|
59
|
+
|
60
|
+
###save_in_memory
|
61
|
+
|
62
|
+
This attribute is used to persist data directly into memory. When set to true, it ignores the model_class attribute value and save in the memory, if set to false it user model_class to persist data.
|
63
|
+
|
64
|
+
P.S.: Just be careful, the set_save_in_memory method should always be called after set_model_class method.
|
54
65
|
|
66
|
+
class User < ActiveRepository::Base
|
55
67
|
# Defines the class responsible for persisting data
|
56
|
-
set_model_class(
|
68
|
+
set_model_class(UserModel)
|
57
69
|
|
58
70
|
# Set this to true in order to ignore model_class attribute and persist in memory
|
59
71
|
set_save_in_memory(true)
|
60
72
|
end
|
61
73
|
|
62
|
-
Then
|
74
|
+
Then, you have only to set the fields it is going to use:
|
75
|
+
|
76
|
+
class User < ActiveRepository::Base
|
77
|
+
# Defines the fields of the class
|
78
|
+
fields :name, :email, :birthdate
|
79
|
+
|
80
|
+
set_model_class(UserModel)
|
81
|
+
|
82
|
+
set_save_in_memory(true)
|
83
|
+
end
|
84
|
+
|
85
|
+
Now you are all set and ready to go. It is just using ActiveRepository as if it was your ActiveRecord model or Mongoid Document.
|
63
86
|
|
64
87
|
## Contributing
|
65
88
|
|
@@ -80,7 +80,6 @@ module ActiveRepository
|
|
80
80
|
|
81
81
|
# Defines "belongs to" type relation between ActiveRepository objects
|
82
82
|
def belongs_to(association_id, options = {})
|
83
|
-
|
84
83
|
options = {
|
85
84
|
:class_name => association_id.to_s.classify,
|
86
85
|
:foreign_key => association_id.to_s.foreign_key
|
@@ -100,7 +99,8 @@ module ActiveRepository
|
|
100
99
|
end
|
101
100
|
|
102
101
|
define_method("#{association_id}=") do |new_value|
|
103
|
-
attributes
|
102
|
+
attributes.delete(association_id.to_sym)
|
103
|
+
send("#{options[:foreign_key]}=", (new_value.try(:id) ? new_value.id : new_value))
|
104
104
|
end
|
105
105
|
|
106
106
|
end
|
@@ -5,6 +5,18 @@ require 'active_repository/sql_query_executor'
|
|
5
5
|
require 'active_repository/finders'
|
6
6
|
require 'active_repository/writers'
|
7
7
|
|
8
|
+
begin
|
9
|
+
klass = Module.const_get(Mongoid::Document)
|
10
|
+
unless klass.is_a?(Class)
|
11
|
+
raise "Not defined"
|
12
|
+
end
|
13
|
+
rescue
|
14
|
+
module Mongoid
|
15
|
+
module Document
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
8
20
|
module ActiveRepository
|
9
21
|
|
10
22
|
# Base class for ActiveRepository gem.
|
@@ -54,6 +66,8 @@ module ActiveRepository
|
|
54
66
|
|
55
67
|
fields :created_at, :updated_at
|
56
68
|
|
69
|
+
self.save_in_memory = true if self.save_in_memory == nil
|
70
|
+
|
57
71
|
# Returns all persisted objects
|
58
72
|
def self.all
|
59
73
|
self == get_model_class ? super : get_model_class.all.map { |object| serialize!(object.attributes) }
|
@@ -84,7 +98,7 @@ module ActiveRepository
|
|
84
98
|
|
85
99
|
# Returns the Class responsible for persisting the objects
|
86
100
|
def self.get_model_class
|
87
|
-
return self if self.
|
101
|
+
return self if self.model_class.nil? || self.save_in_memory?
|
88
102
|
save_in_memory? ? self : self.model_class
|
89
103
|
end
|
90
104
|
|
@@ -107,6 +121,8 @@ module ActiveRepository
|
|
107
121
|
def self.set_model_class(value)
|
108
122
|
self.model_class = value if model_class.nil?
|
109
123
|
|
124
|
+
self.set_save_in_memory(self.model_class == self)
|
125
|
+
|
110
126
|
field_names.each do |field_name|
|
111
127
|
define_custom_find_by_field(field_name)
|
112
128
|
define_custom_find_all_by_field(field_name)
|
@@ -116,7 +132,7 @@ module ActiveRepository
|
|
116
132
|
# Sets the class attribute save_in_memory, set it to true to ignore model_class attribute
|
117
133
|
# and persist objects in memory
|
118
134
|
def self.set_save_in_memory(value)
|
119
|
-
self.save_in_memory = value
|
135
|
+
self.save_in_memory = value
|
120
136
|
end
|
121
137
|
|
122
138
|
# Searches persisted objects that matches the criterias in the parameters.
|
@@ -146,18 +162,31 @@ module ActiveRepository
|
|
146
162
|
# is saved in memory.
|
147
163
|
def persist
|
148
164
|
if self.valid?
|
149
|
-
save_in_memory? ? save : self.convert
|
165
|
+
save_in_memory? ? save : self.convert.present?
|
150
166
|
end
|
151
167
|
end
|
152
168
|
|
153
169
|
# Gathers the persisted object from database and updates self with it's attributes.
|
154
170
|
def reload
|
155
|
-
|
171
|
+
object = self.class.get_model_class.find(self.id)
|
172
|
+
|
173
|
+
serialize! (self.class.get_model_class.find(self.id) || self).attributes
|
156
174
|
end
|
157
175
|
|
158
|
-
|
159
|
-
|
160
|
-
|
176
|
+
def save(force=false)
|
177
|
+
if self.class == self.class.get_model_class
|
178
|
+
object = self.class.get_model_class.find(self.id)
|
179
|
+
|
180
|
+
if force || self.id.nil?
|
181
|
+
self.id = nil if self.id.nil?
|
182
|
+
super
|
183
|
+
else
|
184
|
+
object.save(true)
|
185
|
+
end
|
186
|
+
true
|
187
|
+
else
|
188
|
+
self.persist
|
189
|
+
end
|
161
190
|
end
|
162
191
|
|
163
192
|
# Updates attributes from self with the attributes from the parameters
|
@@ -166,7 +195,7 @@ module ActiveRepository
|
|
166
195
|
self.attributes = attributes
|
167
196
|
end
|
168
197
|
|
169
|
-
self
|
198
|
+
self.dup
|
170
199
|
end
|
171
200
|
|
172
201
|
protected
|
@@ -175,7 +204,7 @@ module ActiveRepository
|
|
175
204
|
def convert(attribute="id")
|
176
205
|
klass = self.class.get_model_class
|
177
206
|
object = klass.where(attribute.to_sym => self.send(attribute)).first
|
178
|
-
|
207
|
+
|
179
208
|
object ||= self.class.get_model_class.new
|
180
209
|
|
181
210
|
attributes = self.attributes
|
@@ -67,13 +67,15 @@ module ActiveRepository #:nodoc:
|
|
67
67
|
|
68
68
|
# Searches first object that matches #field_name field with the #args value(s)
|
69
69
|
def find_by_field(field_name, args)
|
70
|
-
self.find_all_by_field(field_name, args).first
|
70
|
+
self.find_all_by_field(field_name, args).first.dup
|
71
71
|
end
|
72
72
|
|
73
73
|
# Searches for an object that has id with #id value, if none is found returns nil
|
74
74
|
def find_by_id(id)
|
75
75
|
if self == get_model_class
|
76
|
-
super(id)
|
76
|
+
object = super(id)
|
77
|
+
|
78
|
+
object.nil? ? nil : object.dup
|
77
79
|
else
|
78
80
|
object = nil
|
79
81
|
|
@@ -101,7 +103,7 @@ module ActiveRepository #:nodoc:
|
|
101
103
|
# Returns the object in the position specified in #position
|
102
104
|
def get(position)
|
103
105
|
object = get_model_class.send(position)
|
104
|
-
serialize!
|
106
|
+
object.present? ? serialize!(object.attributes) : nil
|
105
107
|
end
|
106
108
|
end
|
107
109
|
end
|
@@ -57,10 +57,16 @@ module ActiveHash
|
|
57
57
|
end
|
58
58
|
|
59
59
|
def save(*args)
|
60
|
-
|
60
|
+
if self.valid?
|
61
|
+
record = self.class.find_by_id(self.id)
|
61
62
|
|
62
|
-
|
63
|
-
|
63
|
+
self.class.insert(self) if record.nil? && record != self
|
64
|
+
|
65
|
+
self.id = self.class.last.id if self.id.nil?
|
66
|
+
true
|
67
|
+
else
|
68
|
+
false
|
69
|
+
end
|
64
70
|
end
|
65
71
|
|
66
72
|
def to_param
|
@@ -1,20 +1,20 @@
|
|
1
|
-
# Module containing writing
|
1
|
+
# Module containing methods responsible for writing attributes of the ActiveRepository::Base class
|
2
2
|
module ActiveRepository
|
3
3
|
module Writers
|
4
4
|
# Creates an object and persists it.
|
5
5
|
def create(attributes={})
|
6
6
|
object = get_model_class.new(attributes)
|
7
7
|
|
8
|
-
object.id = nil
|
8
|
+
object.id = nil unless exists?(object.id)
|
9
9
|
|
10
10
|
if get_model_class == self
|
11
11
|
object.save
|
12
12
|
else
|
13
13
|
repository = serialize!(object.attributes)
|
14
|
-
repository.valid? ?
|
14
|
+
object = repository.valid? ? get_model_class.create(attributes) : repository
|
15
15
|
end
|
16
16
|
|
17
|
-
serialize!(object.attributes)
|
17
|
+
object.valid? ? serialize!(object.reload.attributes) : object
|
18
18
|
end
|
19
19
|
|
20
20
|
# Searches for an object that matches the attributes on the parameter, if none is found
|
@@ -22,7 +22,7 @@ module ActiveRepository
|
|
22
22
|
def find_or_create(attributes)
|
23
23
|
object = get_model_class.where(attributes).first
|
24
24
|
|
25
|
-
object =
|
25
|
+
object = get_model_class.create(attributes) if object.nil?
|
26
26
|
|
27
27
|
serialize!(object.attributes)
|
28
28
|
end
|
@@ -38,37 +38,46 @@ module ActiveRepository
|
|
38
38
|
|
39
39
|
# Updates #key attribute with #value value.
|
40
40
|
def update_attribute(key, value)
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
object = self.class.get_model_class.find(self.id)
|
41
|
+
ret = self.valid?
|
42
|
+
|
43
|
+
if ret
|
44
|
+
object = (self.id.nil? ? self.class.get_model_class.new : self.class.get_model_class.find(self.id))
|
45
|
+
|
46
|
+
if self.class == self.class.get_model_class
|
47
|
+
self.send("#{key}=", value)
|
48
|
+
|
49
|
+
ret = save
|
50
|
+
else
|
51
|
+
key = (key.to_s == 'id' ? '_id' : key.to_s) if mongoid?
|
45
52
|
|
46
|
-
|
47
|
-
super(key,value)
|
48
|
-
key = key.to_s == 'id' ? '_id' : key.to_s
|
53
|
+
ret = object.update_attribute(key,value)
|
49
54
|
end
|
50
55
|
|
51
|
-
|
52
|
-
object.save
|
56
|
+
reload
|
53
57
|
end
|
54
58
|
|
55
|
-
|
59
|
+
ret
|
56
60
|
end
|
57
61
|
|
58
62
|
# Updates attributes in self with the attributes in the parameter
|
59
63
|
def update_attributes(attributes)
|
60
|
-
|
61
|
-
|
62
|
-
|
64
|
+
ret = true
|
65
|
+
klass = self.class
|
66
|
+
model_class = self.class.get_model_class
|
67
|
+
|
68
|
+
if klass == model_class
|
69
|
+
attributes.each do |key, value|
|
70
|
+
self.send("#{key}=", value) unless key == :id
|
71
|
+
end
|
72
|
+
save
|
63
73
|
else
|
64
|
-
object = self.
|
65
|
-
end
|
74
|
+
object = self.id.nil? ? model_class.new : model_class.find(self.id)
|
66
75
|
|
67
|
-
|
68
|
-
object.update_attribute("#{k.to_s}", v) unless k == :id
|
76
|
+
ret = object.update_attributes(attributes)
|
69
77
|
end
|
70
78
|
|
71
|
-
|
79
|
+
reload
|
80
|
+
ret
|
72
81
|
end
|
73
82
|
end
|
74
83
|
end
|
@@ -63,7 +63,7 @@ shared_examples ".where" do
|
|
63
63
|
end
|
64
64
|
|
65
65
|
it "if id exists, use auto increment id" do
|
66
|
-
country = Country.create(:
|
66
|
+
country = Country.create(:name => "Russia", :language => 'Russian')
|
67
67
|
|
68
68
|
country.id.should_not == 1
|
69
69
|
end
|
@@ -472,7 +472,7 @@ shared_examples "#save" do
|
|
472
472
|
|
473
473
|
it "adds the new object to the data collection" do
|
474
474
|
Country.all.should be_empty
|
475
|
-
country = Country.new :
|
475
|
+
country = Country.new :name => "foo", :monarch => nil, :language => nil
|
476
476
|
country.persist.should be_true
|
477
477
|
country.reload
|
478
478
|
|
@@ -526,17 +526,10 @@ shared_examples ".create" do
|
|
526
526
|
it "adds an auto-incrementing id if the id is nil" do
|
527
527
|
country1 = Country.new :name => "foo"
|
528
528
|
country1.save
|
529
|
-
country1.id.should == 1
|
530
529
|
|
531
|
-
country2 = Country.new :name => "bar"
|
530
|
+
country2 = Country.new :name => "bar"
|
532
531
|
country2.save
|
533
|
-
country2.id.should ==
|
534
|
-
end
|
535
|
-
|
536
|
-
it "does not add auto-incrementing id if the id is present" do
|
537
|
-
country1 = Country.new :id => 456, :name => "foo"
|
538
|
-
country1.save
|
539
|
-
country1.id.should == 456
|
532
|
+
country2.id.should == country1.id + 1
|
540
533
|
end
|
541
534
|
|
542
535
|
it "adds the new object to the data collection" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_repository
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-02-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: active_hash
|