active_repository 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ODc4ODk0M2JiZTk5ZGE4ODZiYzM4OGRmNzNiZmYxODVlZjk1Yzg2Yg==
5
+ data.tar.gz: !binary |-
6
+ NDM4YjY5YjIyY2JhZTUxOWVjNzdkY2ZkYWFkMWYzZmI3YjQ2NzNkMQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YWRmMTRiYzEwMjMwMjE2ZDg0YzJiZWE1MzEzZWI5MDViY2Q2ZDdjMDEyY2Rj
10
+ MjVmNTI0MWYwODNhNzM2NzkzMzIzZTZiYzllYjg2ODA5NDgyMjM0OTgxNTQx
11
+ N2U0NDI0MGRkZTk5OTNiOGZmYzYwNjY2MGNmMDhmZmY2MWI2YjQ=
12
+ data.tar.gz: !binary |-
13
+ YjYzN2E1ZTdlOTI2ODMxYWVlZjlkYmM3NzgwNGRmZWQ0ODJlNDA0NjgyNmFj
14
+ ZTM5MjRiODkzZTJmMWNhMmMzNjRiZjFkNjhjMmJkZDZlMDMxNDJhODQ5NThk
15
+ OWI1N2RhNWM5ODA1NzU2ODhlMGQ2YzdmMWM5MTM5YjJlMmJlYmI=
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ActiveRepository
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/efreesen/active_repository.png)](http://travis-ci.org/efreesen/active_repository)[![Dependency Status](https://gemnasium.com/efreesen/active_repository.png)](https://gemnasium.com/efreesen/active_repository) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/efreesen/active_repository)
3
+ [![Build Status](https://secure.travis-ci.org/efreesen/active_repository.png)](http://travis-ci.org/efreesen/active_repository)[![Dependency Status](https://gemnasium.com/efreesen/active_repository.png)](https://gemnasium.com/efreesen/active_repository) [![Code Climate](https://codeclimate.com/github/efreesen/active_repository.png)](https://codeclimate.com/github/efreesen/active_repository)
4
4
 
5
5
  ActiveRepository was designed so you can build your Business Models without depending on any ORM. It by default saves your data in memory using ActiveHash (https://github.com/zilkey/active_hash). Then when you decide which ORM you want to use you only have to connect ActiveRepository with it.
6
6
 
@@ -84,6 +84,8 @@ Then, you have only to set the fields it is going to use:
84
84
 
85
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.
86
86
 
87
+ You can check an example project here: https://github.com/efreesen/sports_betting_engine
88
+
87
89
  ## Contributing
88
90
 
89
91
  1. Fork it
@@ -35,6 +35,7 @@ Gem::Specification.new do |gem|
35
35
  gem.add_development_dependency(%q<rspec>, [">= 2.2.0"])
36
36
  gem.add_development_dependency(%q<activerecord>)
37
37
  gem.add_development_dependency(%q<mongoid>)
38
+ gem.add_development_dependency(%q<mongo_mapper>)
38
39
  gem.add_development_dependency('rake')
39
40
  gem.add_development_dependency(%q<sqlite3>) unless RUBY_PLATFORM == 'java'
40
41
  gem.add_development_dependency(%q<jdbc-sqlite3>) if RUBY_PLATFORM == 'java'
@@ -0,0 +1,20 @@
1
+ class ActiveHashAdapter
2
+ class << self
3
+ def all(klass)
4
+ klass.superclass.superclass.all
5
+ end
6
+
7
+ def delete_all
8
+ @klass.superclass.delete_all
9
+ end
10
+
11
+ def exists?(id)
12
+ @klass.find_by_id(id).present?
13
+ end
14
+
15
+ def where(*args)
16
+ query = ActiveHash::SQLQueryExecutor.args_to_query(args)
17
+ @klass.where(query)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,51 @@
1
+ class DefaultAdapter
2
+ class << self
3
+ def all(klass)
4
+ klass.get_model_class.all
5
+ end
6
+
7
+ def delete_all(klass)
8
+ klass.get_model_class.delete_all
9
+ end
10
+
11
+ def exists?(klass, id)
12
+ klass.get_model_class.exists?(id)
13
+ end
14
+
15
+ def find(klass, id)
16
+ klass.get_model_class.find(id)
17
+ end
18
+
19
+ def first(klass)
20
+ klass.get_model_class.first
21
+ end
22
+
23
+ def last(klass)
24
+ klass.get_model_class.last
25
+ end
26
+
27
+ def create(klass, attributes)
28
+ object = klass.get_model_class.create(attributes)
29
+ end
30
+
31
+ def update_attribute(klass, id, key, value)
32
+ object = id.nil? ? klass.get_model_class.new(key.to_sym => value) : klass.get_model_class.find(id)
33
+
34
+ ret = object.update_attribute(key, value)
35
+
36
+ [ret, object]
37
+ end
38
+
39
+ def update_attributes(klass, id, attributes)
40
+ object = id.nil? ? klass.get_model_class.new : klass.get_model_class.find(id)
41
+
42
+ ret = object.update_attributes(attributes)
43
+
44
+ [ret, object]
45
+ end
46
+
47
+ def where(klass, args)
48
+ klass.get_model_class.where(args)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,53 @@
1
+ require 'active_repository/adapters/default_adapter'
2
+
3
+ class MongoidAdapter < DefaultAdapter
4
+ class << self
5
+ # def all(klass)
6
+ # klass.all
7
+ # end
8
+
9
+ # def delete_all(klass)
10
+ # klass.delete_all
11
+ # end
12
+
13
+ def exists?(klass, id)
14
+ klass.get_model_class.where(:id => id).present?
15
+ end
16
+
17
+ # def find(klass, id)
18
+ # klass.find(id)
19
+ # end
20
+
21
+ # def first(klass)
22
+ # klass.first
23
+ # end
24
+
25
+ # def last(klass)
26
+ # klass.last
27
+ # end
28
+
29
+ # def create(klass, attributes)
30
+ # klass.create(attributes)
31
+ # end
32
+
33
+ # def update_attribute(klass, id, key, value)
34
+ # object = id.nil? ? klass.new(key.to_sym => value) : klass.find(id)
35
+
36
+ # ret = object.update_attribute(key, value)
37
+
38
+ # [ret, object]
39
+ # end
40
+
41
+ # def update_attributes(klass, id, attributes)
42
+ # object = id.nil? ? klass.new : klass.find(id)
43
+
44
+ # ret = object.update_attributes(attributes)
45
+
46
+ # [ret, object]
47
+ # end
48
+
49
+ # def where(klass, args)
50
+ # klass.where(args)
51
+ # end
52
+ end
53
+ end
@@ -0,0 +1,64 @@
1
+ require 'active_repository/adapters/active_hash_adapter'
2
+ require 'active_repository/adapters/default_adapter'
3
+ require 'active_repository/adapters/mongoid_adapter'
4
+
5
+ class PersistenceAdapter
6
+ class << self
7
+ def get_adapter(klass)
8
+ modules = klass.get_model_class.included_modules.map(&:to_s)
9
+ if modules.include?("Mongoid::Document")
10
+ MongoidAdapter
11
+ elsif modules.map(&:to_s).include?("DataMapper::Resource")
12
+ DataMapperAdapter
13
+ # elsif modules.map(&:to_s).include?("MongoMapper::Document")
14
+ # MongoMapperAdapter
15
+ else
16
+ DefaultAdapter
17
+ end
18
+ end
19
+
20
+ def all(klass)
21
+ get_adapter(klass).all(klass)
22
+ end
23
+
24
+ def create(klass, attributes)
25
+ get_adapter(klass).create(klass, attributes)
26
+ end
27
+
28
+ def delete_all(klass)
29
+ get_adapter(klass).delete_all(klass)
30
+ end
31
+
32
+ def exists?(klass, id)
33
+ get_adapter(klass).exists?(klass, id)
34
+ end
35
+
36
+ def find(klass, id)
37
+ get_adapter(klass).find(klass, id)
38
+ end
39
+
40
+ def first(klass)
41
+ get_adapter(klass).first(klass)
42
+ end
43
+
44
+ def last(klass)
45
+ get_adapter(klass).last(klass)
46
+ end
47
+
48
+ def update_attribute(klass, id, key, value)
49
+ get_adapter(klass).update_attribute(klass, id, key, value)
50
+ end
51
+
52
+ def update_attributes(klass, id, attributes)
53
+ get_adapter(klass).update_attributes(klass, id, attributes)
54
+ end
55
+
56
+ def where(klass, args)
57
+ get_adapter(klass).where(klass, args)
58
+ end
59
+ end
60
+
61
+ def method_missing(sym, *args, &block)
62
+ get_adapter(args.first).send(sym, args)
63
+ end
64
+ end
@@ -4,6 +4,7 @@ require 'active_repository/write_support'
4
4
  require 'active_repository/sql_query_executor'
5
5
  require 'active_repository/finders'
6
6
  require 'active_repository/writers'
7
+ require 'active_repository/adapters/persistence_adapter'
7
8
 
8
9
  begin
9
10
  klass = Module.const_get(Mongoid::Document)
@@ -49,7 +50,7 @@ module ActiveRepository
49
50
  # SaveInORMOrODMTest.set_save_in_memory(false)
50
51
  # end
51
52
  #
52
- # Author:: Caio Torres (mailto:efreesen@gmail.com)
53
+ # Author:: Caio Torres (mailto:efreesen@gmail.com)
53
54
  # License:: MIT
54
55
  class Base < ActiveHash::Base
55
56
  extend ActiveModel::Callbacks
@@ -62,15 +63,14 @@ module ActiveRepository
62
63
 
63
64
  class_attribute :model_class, :save_in_memory, :instance_writer => false
64
65
 
65
- before_validation :set_timestamps
66
-
67
- fields :created_at, :updated_at
66
+ after_validation :set_timestamps
68
67
 
69
68
  self.save_in_memory = true if self.save_in_memory == nil
70
69
 
71
70
  # Returns all persisted objects
72
71
  def self.all
73
- self == get_model_class ? super : get_model_class.all.map { |object| serialize!(object.attributes) }
72
+ (self == get_model_class ? super : PersistenceAdapter.all(self).map { |object| serialize!(object.attributes) })
73
+ # self == get_model_class ? super : get_model_class.all.map { |object| serialize!(object.attributes) }
74
74
  end
75
75
 
76
76
  # Constantize class name
@@ -80,20 +80,21 @@ module ActiveRepository
80
80
 
81
81
  # Deletes all persisted objects
82
82
  def self.delete_all
83
- self == get_model_class ? super : get_model_class.delete_all
83
+ self == get_model_class ? super : PersistenceAdapter.delete_all(self)
84
84
  end
85
85
 
86
86
  # Checks the existence of a persisted object with the specified id
87
87
  def self.exists?(id)
88
- if self == get_model_class
89
- !find_by_id(id).nil?
90
- else
91
- if mongoid?
92
- find_by_id(id).present?
93
- else
94
- get_model_class.exists?(id)
95
- end
96
- end
88
+ self == get_model_class ? find_by_id(id).present? : PersistenceAdapter.exists?(self, id)
89
+ # if self == get_model_class
90
+ # !find_by_id(id).nil?
91
+ # else
92
+ # if mongoid?
93
+ # find_by_id(id).present?
94
+ # else
95
+ # get_model_class.exists?(id)
96
+ # end
97
+ # end
97
98
  end
98
99
 
99
100
  # Returns the Class responsible for persisting the objects
@@ -105,9 +106,9 @@ module ActiveRepository
105
106
  # Converts Persisted object(s) to it's ActiveRepository counterpart
106
107
  def self.serialize!(other)
107
108
  case other.class.to_s
108
- when "Hash" then self.new.serialize!(other)
109
- when "Array" then other.map { |o| serialize!(o.attributes) }
110
- when "Moped::BSON::Document" then self.new.serialize!(other)
109
+ when "Hash", "ActiveSupport::HashWithIndifferentAccess" then self.new.serialize!(other)
110
+ when "Array" then other.map { |o| serialize!(o.attributes) }
111
+ when "Moped::BSON::Document" then self.new.serialize!(other)
111
112
  else self.new.serialize!(other.attributes)
112
113
  end
113
114
  end
@@ -150,7 +151,8 @@ module ActiveRepository
150
151
  else
151
152
  objects = []
152
153
  args = args.first.is_a?(Hash) ? args.first : args
153
- get_model_class.where(args).each do |object|
154
+
155
+ PersistenceAdapter.where(self, args).each do |object|
154
156
  objects << self.serialize!(object.attributes)
155
157
  end
156
158
 
@@ -168,9 +170,9 @@ module ActiveRepository
168
170
 
169
171
  # Gathers the persisted object from database and updates self with it's attributes.
170
172
  def reload
171
- object = self.class.get_model_class.find(self.id)
173
+ object = self.id.present? ? self.class.get_model_class.find(self.id) : self
172
174
 
173
- serialize! (self.class.get_model_class.find(self.id) || self).attributes
175
+ serialize! object.attributes
174
176
  end
175
177
 
176
178
  def save(force=false)
@@ -238,8 +240,10 @@ module ActiveRepository
238
240
 
239
241
  # Updates created_at and updated_at
240
242
  def set_timestamps
241
- self.created_at = DateTime.now.utc if self.new_record?
242
- self.updated_at = DateTime.now.utc
243
+ if self.errors.empty?
244
+ self.created_at = DateTime.now.utc if self.respond_to?(:created_at=) && self.created_at.nil?
245
+ self.updated_at = DateTime.now.utc if self.respond_to?(:updated_at=)
246
+ end
243
247
  end
244
248
  end
245
249
  end
@@ -3,12 +3,12 @@ module ActiveRepository #:nodoc:
3
3
  module Finders #:nodoc:
4
4
  # Defines fiend_by_field methods for the Class
5
5
  def define_custom_find_by_field(field_name)
6
- method_name = :"find_all_by_#{field_name}"
6
+ method_name = :"find_by_#{field_name}"
7
7
  the_meta_class.instance_eval do
8
8
  define_method(method_name) do |*args|
9
9
  object = nil
10
10
 
11
- object = self.find_by_field(field_name.to_sym, args)
11
+ object = self.where(field_name.to_sym => args).first
12
12
 
13
13
  object.nil? ? nil : serialize!(object.attributes)
14
14
  end
@@ -35,7 +35,7 @@ module ActiveRepository #:nodoc:
35
35
  if self == get_model_class
36
36
  super(id)
37
37
  else
38
- object = (id == :all) ? all : get_model_class.find(id)
38
+ object = (id == :all) ? all : PersistenceAdapter.find(self, id)
39
39
 
40
40
  serialize!(object)
41
41
  end
@@ -51,24 +51,27 @@ module ActiveRepository #:nodoc:
51
51
  def find_all_by_field(field_name, args)
52
52
  objects = []
53
53
 
54
+ # raise "field: #{field_name}; values: #{args.first.inspect}; all: #{get_model_class.all.inspect}"
55
+
54
56
  if self == get_model_class
55
57
  objects = self.where(field_name.to_sym => args.first)
56
58
  else
57
- if mongoid?
58
- objects = get_model_class.where(field_name.to_sym => args.first)
59
- else
60
- method_name = :"find_all_by_#{field_name}"
61
- objects = get_model_class.send(method_name, args)
62
- end
59
+ objects = PersistenceAdapter.where(self, field_name.to_sym => args.first)
60
+ # if mongoid?
61
+ # objects = get_model_class.where(field_name.to_sym => args.first)
62
+ # else
63
+ # method_name = :"find_all_by_#{field_name}"
64
+ # objects = get_model_class.send(method_name, args)
65
+ # end
63
66
  end
64
67
 
65
68
  objects
66
69
  end
67
70
 
68
71
  # Searches first object that matches #field_name field with the #args value(s)
69
- def find_by_field(field_name, args)
70
- self.find_all_by_field(field_name, args).first.dup
71
- end
72
+ # def find_by_field(field_name, args)
73
+ # self.find_all_by_field(field_name, args).first.dup
74
+ # end
72
75
 
73
76
  # Searches for an object that has id with #id value, if none is found returns nil
74
77
  def find_by_id(id)
@@ -77,13 +80,13 @@ module ActiveRepository #:nodoc:
77
80
 
78
81
  object.nil? ? nil : object.dup
79
82
  else
80
- object = nil
83
+ object = PersistenceAdapter.where(self, :id => id).first
81
84
 
82
- if mongoid?
83
- object = get_model_class.where(:id => id).entries.first
84
- else
85
- object = get_model_class.find_by_id(id)
86
- end
85
+ # if mongoid?
86
+ # object = get_model_class.where(:id => id).entries.first
87
+ # else
88
+ # object = get_model_class.find_by_id(id)
89
+ # end
87
90
 
88
91
  object.nil? ? nil : serialize!(object.attributes)
89
92
  end
@@ -102,7 +105,7 @@ module ActiveRepository #:nodoc:
102
105
  private
103
106
  # Returns the object in the position specified in #position
104
107
  def get(position)
105
- object = get_model_class.send(position)
108
+ object = PersistenceAdapter.send(position, self)
106
109
  object.present? ? serialize!(object.attributes) : nil
107
110
  end
108
111
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveRepository
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -79,7 +79,7 @@ module ActiveHash
79
79
  end
80
80
 
81
81
  def eql?(other)
82
- (other.instance_of?(self.class) || other.instance_of?(self.class.get_model_class)) && id.present? && (id == other.id) && (created_at == other.created_at)
82
+ (other.instance_of?(self.class) || other.instance_of?(self.class.get_model_class)) && id.present? && (id == other.id) && (!self.respond_to?(:created_at) || (created_at == other.created_at))
83
83
  end
84
84
 
85
85
  alias == eql?
@@ -3,26 +3,40 @@ module ActiveRepository
3
3
  module Writers
4
4
  # Creates an object and persists it.
5
5
  def create(attributes={})
6
- object = get_model_class.new(attributes)
6
+ object = self.new(attributes)
7
7
 
8
- object.id = nil unless exists?(object.id)
8
+ if object.present? && object.valid?
9
+ if get_model_class == self
10
+ object.id = nil unless exists?(object.id)
9
11
 
10
- if get_model_class == self
11
- object.save
12
- else
13
- repository = serialize!(object.attributes)
14
- object = repository.valid? ? get_model_class.create(attributes) : repository
12
+ object.save
13
+ else
14
+ object = PersistenceAdapter.create(self, object.attributes)
15
+ end
15
16
  end
16
17
 
17
- object.valid? ? serialize!(object.reload.attributes) : object
18
+ new_object = serialize!(object.reload.attributes)
19
+
20
+ new_object.valid?
21
+
22
+ new_object
18
23
  end
19
24
 
20
25
  # Searches for an object that matches the attributes on the parameter, if none is found
21
26
  # it creates one with the defined attributes.
22
27
  def find_or_create(attributes)
23
- object = get_model_class.where(attributes).first
28
+ object = where(attributes).first
29
+
30
+ object.nil? ? create(attributes) : object
31
+ end
24
32
 
25
- object = get_model_class.create(attributes) if object.nil?
33
+ def find_or_initialize(attributes)
34
+ object = where(attributes).first
35
+ object = self.new(attributes) if object.nil?
36
+
37
+ attributes.each do |key, value|
38
+ object.send("#{key.to_sym}=", value)
39
+ end
26
40
 
27
41
  serialize!(object.attributes)
28
42
  end
@@ -38,19 +52,22 @@ module ActiveRepository
38
52
 
39
53
  # Updates #key attribute with #value value.
40
54
  def update_attribute(key, value)
41
- ret = self.valid?
55
+ ret = self.valid?
42
56
 
43
57
  if ret
44
- object = (self.id.nil? ? self.class.get_model_class.new : self.class.get_model_class.find(self.id))
45
-
46
58
  if self.class == self.class.get_model_class
59
+ object = self.class.find_or_initialize(:id => self.id)
60
+
47
61
  self.send("#{key}=", value)
48
62
 
49
63
  ret = save
50
64
  else
51
- key = (key.to_s == 'id' ? '_id' : key.to_s) if mongoid?
65
+ # key = (key.to_s == 'id' ? '_id' : key.to_s) if mongoid?
66
+
67
+ ret, object = PersistenceAdapter.update_attribute(self.class, self.id, key, value)
52
68
 
53
- ret = object.update_attribute(key,value)
69
+ self.attributes = object.attributes
70
+ # object.update_attribute(key,value)
54
71
  end
55
72
 
56
73
  reload
@@ -71,9 +88,13 @@ module ActiveRepository
71
88
  end
72
89
  save
73
90
  else
74
- object = self.id.nil? ? model_class.new : model_class.find(self.id)
91
+ attributes = self.attributes.merge(attributes)
92
+ ret, object = PersistenceAdapter.update_attributes(self.class, self.id, attributes)
93
+
94
+ self.attributes = object.attributes
95
+ # object = self.id.nil? ? model_class.new : model_class.find(self.id)
75
96
 
76
- ret = object.update_attributes(attributes)
97
+ # ret = object.update_attributes(attributes)
77
98
  end
78
99
 
79
100
  reload
@@ -292,6 +292,7 @@ describe ActiveRepository::Base, "associations" do
292
292
  City.set_model_class(CityModel)
293
293
  City.set_save_in_memory(false)
294
294
  belongs_to :state
295
+ has_many :regions
295
296
  end
296
297
  end
297
298
 
@@ -18,7 +18,7 @@ describe ActiveRepository, "Base" do
18
18
 
19
19
  context "in_memory" do
20
20
  before do
21
- Country.fields :name, :monarch, :language
21
+ Country.fields :name, :monarch, :language, :created_at, :updated_at
22
22
  Country.set_model_class(Country)
23
23
  Country.set_save_in_memory(true)
24
24
 
@@ -62,7 +62,7 @@ describe ActiveRepository, "Base" do
62
62
 
63
63
  context "active_record" do
64
64
  before do
65
- Country.fields :name, :monarch, :language
65
+ Country.fields :name, :monarch, :language, :created_at, :updated_at
66
66
 
67
67
  class CountryModel < ActiveRecord::Base
68
68
  self.table_name = 'countries'
@@ -123,7 +123,7 @@ describe ActiveRepository, "Base" do
123
123
 
124
124
  context "mongoid" do
125
125
  before do
126
- Country.fields :name, :monarch, :language
126
+ Country.fields :name, :monarch, :language, :created_at, :updated_at
127
127
 
128
128
  Mongoid.load!("support/mongoid.yml", :development)
129
129
 
@@ -51,8 +51,6 @@ describe ActiveRepository, "Base" do
51
51
  t.string :monarch
52
52
  t.string :language
53
53
  t.datetime :founded_at
54
- t.datetime :created_at
55
- t.datetime :updated_at
56
54
  end
57
55
  end
58
56
 
@@ -2,14 +2,16 @@ require 'set'
2
2
 
3
3
  shared_examples ".update_attributes" do
4
4
  it "updates records" do
5
- country = Country.find(1)
5
+ id = Country.first.id
6
+ country = Country.find(id)
6
7
  country.update_attributes(:name => "Italy")
7
8
 
8
9
  Country.first.name.should == "Italy"
9
10
  end
10
11
 
11
12
  it "must not update id" do
12
- country = Country.find(1)
13
+ id = Country.first.id
14
+ country = Country.find(id)
13
15
 
14
16
  country.update_attributes(:id => 45, :name => "Russia")
15
17
 
@@ -48,24 +50,30 @@ shared_examples ".where" do
48
50
  end
49
51
 
50
52
  it "populates the data correctly" do
53
+ first_id = Country.first.id
54
+ last_id = Country.all[3].id
55
+
51
56
  records = Country.where(:language => 'English')
52
- records.first.id.should == 1
57
+ records.first.id.should == first_id
53
58
  records.first.name.should == "US"
54
- records.last.id.should == 4
59
+ records.last.id.should == last_id
55
60
  records.last.name.should == "UK"
56
61
  end
57
62
 
58
63
  it "filters the records from a AR-like conditions hash" do
64
+ first_id = Country.first.id
65
+
59
66
  record = Country.where(:name => 'US')
60
67
  record.count.should == 1
61
- record.first.id.should == 1
68
+ record.first.id.should == first_id
62
69
  record.first.name.should == 'US'
63
70
  end
64
71
 
65
72
  it "if id exists, use auto increment id" do
73
+ first_id = Country.first.id
66
74
  country = Country.create(:name => "Russia", :language => 'Russian')
67
75
 
68
- country.id.should_not == 1
76
+ country.id.should_not == first_id
69
77
  end
70
78
  end
71
79
 
@@ -91,24 +99,31 @@ end
91
99
 
92
100
  shared_examples ".first" do
93
101
  it "returns the first object" do
94
- Country.first.should == Country.find(1)
102
+ first = Country.first
103
+
104
+ first.should == Country.find(first.id)
95
105
  end
96
106
  end
97
107
 
98
108
  shared_examples ".last" do
99
109
  it "returns the last object" do
100
- Country.last.should == Country.find(5)
110
+ last = Country.last
111
+
112
+ last.should == Country.find(last.id)
101
113
  end
102
114
  end
103
115
 
104
116
  shared_examples ".find" do
105
117
  context "with an id" do
106
118
  it "finds the record with the specified id" do
107
- Country.find(2).id.should == 2
119
+ second_id = Country.all[1].id
120
+ Country.find(second_id).id.should == second_id
108
121
  end
109
122
 
110
123
  it "finds the record with the specified id as a string" do
111
- Country.find("2").id.should == 2
124
+ second_id = Country.all[1].id
125
+
126
+ Country.find(second_id.to_s).id.should == second_id
112
127
  end
113
128
 
114
129
  it "raises ActiveHash::RecordNotFound when id not found" do
@@ -130,7 +145,9 @@ shared_examples ".find" do
130
145
 
131
146
  context "with an array of ids" do
132
147
  it "returns all matching ids" do
133
- Country.find([1, 3]).should == [Country.find(1), Country.find(3)]
148
+ countries = Country.all
149
+ ids = [countries[0].id, countries[2].id]
150
+ Country.find(ids).should == [Country.find(ids.first), Country.find(ids.last)]
134
151
  end
135
152
 
136
153
  it "raises ActiveHash::RecordNotFound when id not found" do
@@ -144,11 +161,13 @@ end
144
161
  shared_examples ".find_by_id" do
145
162
  context "with an id" do
146
163
  it "finds the record with the specified id" do
147
- Country.find_by_id(2).id.should == 2
164
+ id = Country.all[1].id
165
+ Country.find_by_id(id).id.should == id
148
166
  end
149
167
 
150
168
  it "finds the record with the specified id as a string" do
151
- Country.find_by_id("2").id.should == 2
169
+ id = Country.all[1].id
170
+ Country.find_by_id(id.to_s).id.should == id
152
171
  end
153
172
  end
154
173
 
@@ -170,13 +189,15 @@ shared_examples "custom finders" do
170
189
  describe "with a match" do
171
190
  context "for a non-nil argument" do
172
191
  it "returns the first matching record" do
173
- Country.find_by_name("US").id.should == 1
192
+ id = Country.first.id
193
+ Country.where(:name => "US").first.id.should == id
174
194
  end
175
195
  end
176
196
 
177
197
  context "for a nil argument" do
178
198
  it "returns the first matching record" do
179
- Country.find_by_language(nil).id.should == 5
199
+ id = Country.all[2].id
200
+ Country.where(:language => 'Spanish').first.id.should == id
180
201
  end
181
202
  end
182
203
  end
@@ -216,14 +237,16 @@ shared_examples "custom finders" do
216
237
  describe "find_by_<field_one>_and_<field_two>" do
217
238
  describe "with a match" do
218
239
  it "returns the first matching record" do
219
- Country.find_by_name_and_language("Canada", "English").id.should == 2
220
- Country.find_by_language_and_name("English", "Canada").id.should == 2
240
+ id = Country.all[1].id
241
+ Country.find_by_name_and_language("Canada", "English").id.should == id
242
+ Country.find_by_language_and_name("English", "Canada").id.should == id
221
243
  end
222
244
  end
223
245
 
224
246
  describe "with a match based on to_s" do
225
247
  it "returns the first matching record" do
226
- Country.find_by_name_and_id("Canada", "2").id.should == 2
248
+ id = Country.all[1].id
249
+ Country.find_by_name_and_id("Canada", id.to_s).id.should == id
227
250
  end
228
251
  end
229
252
 
@@ -351,8 +374,8 @@ end
351
374
 
352
375
  shared_examples "#to_param" do
353
376
  it "should return id as a string" do
354
- id = Country.last.id + 1
355
- Country.create(:id => id).to_param.should == id.to_s
377
+ country = Country.create
378
+ country.to_param.should == country.id.to_s
356
379
  end
357
380
  end
358
381
 
@@ -431,7 +454,9 @@ shared_examples "#readonly?" do
431
454
  end
432
455
 
433
456
  it "updates a record" do
434
- country = Country.find(1)
457
+ id = Country.first.id
458
+
459
+ country = Country.find(id)
435
460
 
436
461
  country.name = "Germany"
437
462
 
@@ -440,7 +465,7 @@ shared_examples "#readonly?" do
440
465
  Country.all.size.should == 5
441
466
  country.should be_valid
442
467
  country.name.should == "Germany"
443
- country.id.should == 1
468
+ country.id.should == id
444
469
  end
445
470
  end
446
471
 
@@ -452,9 +477,10 @@ shared_examples "#cache_key" do
452
477
  it 'should use the record\'s updated_at if present' do
453
478
  country = Country.create(:name => "foo")
454
479
 
455
- id = Country.last.id
480
+ country.reload
456
481
 
457
482
  date_string = country.updated_at.nil? ? "" : "-#{country.updated_at.to_s(:number)}"
483
+ id = country.id
458
484
 
459
485
  Country.first.cache_key.should == "countries/#{id}#{date_string}"
460
486
  end
@@ -472,15 +498,15 @@ shared_examples "#save" do
472
498
 
473
499
  it "adds the new object to the data collection" do
474
500
  Country.all.should be_empty
475
- country = Country.new :name => "foo", :monarch => nil, :language => nil
476
- country.persist.should be_true
501
+ country = Country.new :name => "foo", :monarch => "King", :language => "bar"
502
+ country.save.should be_true
477
503
  country.reload
478
504
 
479
- countries_attributes = Country.all.map(&:attributes)
505
+ countries_attributes = Country.first.attributes
480
506
  countries_attributes.delete(:created_at)
481
507
  countries_attributes.delete(:updated_at)
482
508
 
483
- expected_attributes = [country].map(&:attributes)
509
+ expected_attributes = country.attributes
484
510
  expected_attributes.delete(:created_at)
485
511
  expected_attributes.delete(:updated_at)
486
512
 
@@ -523,13 +549,13 @@ shared_examples ".create" do
523
549
  countries_attributes.should == expected_attributes
524
550
  end
525
551
 
526
- it "adds an auto-incrementing id if the id is nil" do
552
+ it "adds an auto-incrementing id if the id is nil and not MongoMapper" do
527
553
  country1 = Country.new :name => "foo"
528
554
  country1.save
529
555
 
530
556
  country2 = Country.new :name => "bar"
531
557
  country2.save
532
- country2.id.should == country1.id + 1
558
+ country2.id.should == ((country1.id.class.name == "BSON::ObjectId") ? country2.id : country1.id + 1)
533
559
  end
534
560
 
535
561
  it "adds the new object to the data collection" do
@@ -291,10 +291,11 @@ shared_examples "is" do
291
291
  end
292
292
 
293
293
  it "attribute is not condition" do
294
+ id = Country.last.id
294
295
  records = Country.where("founded_at is not null")
295
296
  records.count.should == 1
296
- records.first.id.should == 5
297
- records.should == [Country.find(5)]
297
+ records.first.id.should == id
298
+ records.should == [Country.find(id)]
298
299
  end
299
300
  end
300
301
 
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_repository
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
- prerelease:
4
+ version: 0.2.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Caio Torres
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-02-23 00:00:00.000000000 Z
11
+ date: 2013-03-23 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: active_hash
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: activemodel
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ! '>='
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ! '>='
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ! '>='
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: activerecord
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ! '>='
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ! '>='
76
67
  - !ruby/object:Gem::Version
@@ -78,7 +69,6 @@ dependencies:
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: mongoid
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
73
  - - ! '>='
84
74
  - !ruby/object:Gem::Version
@@ -86,7 +76,20 @@ dependencies:
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mongo_mapper
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
90
93
  requirements:
91
94
  - - ! '>='
92
95
  - !ruby/object:Gem::Version
@@ -94,7 +97,6 @@ dependencies:
94
97
  - !ruby/object:Gem::Dependency
95
98
  name: rake
96
99
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
100
  requirements:
99
101
  - - ! '>='
100
102
  - !ruby/object:Gem::Version
@@ -102,7 +104,6 @@ dependencies:
102
104
  type: :development
103
105
  prerelease: false
104
106
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
107
  requirements:
107
108
  - - ! '>='
108
109
  - !ruby/object:Gem::Version
@@ -110,7 +111,6 @@ dependencies:
110
111
  - !ruby/object:Gem::Dependency
111
112
  name: sqlite3
112
113
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
114
  requirements:
115
115
  - - ! '>='
116
116
  - !ruby/object:Gem::Version
@@ -118,7 +118,6 @@ dependencies:
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
121
  requirements:
123
122
  - - ! '>='
124
123
  - !ruby/object:Gem::Version
@@ -133,6 +132,10 @@ files:
133
132
  - LICENSE
134
133
  - README.md
135
134
  - active_repository.gemspec
135
+ - lib/active_repository/adapters/active_hash_adapter.rb
136
+ - lib/active_repository/adapters/default_adapter.rb
137
+ - lib/active_repository/adapters/mongoid_adapter.rb
138
+ - lib/active_repository/adapters/persistence_adapter.rb
136
139
  - lib/active_repository/associations.rb
137
140
  - lib/active_repository/base.rb
138
141
  - lib/active_repository/finders.rb
@@ -152,27 +155,26 @@ files:
152
155
  homepage: http://github.com/efreesen/active_repository
153
156
  licenses:
154
157
  - MIT
158
+ metadata: {}
155
159
  post_install_message:
156
160
  rdoc_options: []
157
161
  require_paths:
158
162
  - lib
159
163
  required_ruby_version: !ruby/object:Gem::Requirement
160
- none: false
161
164
  requirements:
162
165
  - - ! '>='
163
166
  - !ruby/object:Gem::Version
164
167
  version: '0'
165
168
  required_rubygems_version: !ruby/object:Gem::Requirement
166
- none: false
167
169
  requirements:
168
170
  - - ! '>='
169
171
  - !ruby/object:Gem::Version
170
172
  version: '0'
171
173
  requirements: []
172
174
  rubyforge_project:
173
- rubygems_version: 1.8.23
175
+ rubygems_version: 2.0.3
174
176
  signing_key:
175
- specification_version: 3
177
+ specification_version: 4
176
178
  summary: An implementation of repository pattern that can connect with any ORM
177
179
  test_files:
178
180
  - Gemfile