rmodel 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: deb9462cdda8354cf1ae328254df31b82ddd3a74
4
- data.tar.gz: 98e72a0ff775b44114b7c90383046806f15948db
3
+ metadata.gz: 7d6692638747de91e5cb796e6141d501bca570af
4
+ data.tar.gz: 5a80109b93f41f8bd2c68ae110420c8b9d20ab8c
5
5
  SHA512:
6
- metadata.gz: dd4006a1d057a592bac459e31f39039c496a89d9c379c7953bea129bc8e3acbd92f13b7cd6d2c32a5ad8ee8a71caebdd8b72a9cd328de1ed41f697ab033b8be3
7
- data.tar.gz: d3e1d56effdceeb17d3978b8242f35aaf9689b2eddf5b71fffc30e6665aea25972672bddcf1d1d3f4053559ffe59cc7d74362cacb5d7877befcaf10cf5c33279
6
+ metadata.gz: 468a0b7cefbf30b51321ec32707dd86ef377bba8f0a0238e3f63b43603acec73ac87beefc1f67ddffc80effda796352bd961aec65151fd86d0c2bb0faa3d51a6
7
+ data.tar.gz: 28cd0af645f741b54336d2da76ea76833f0bfa1e4f5ed069804a58f98f5381672a3bbbc58cae72a294cd944cac3332e0cb208386c808f7bdb4a73b72ee8ddcc4
data/.travis.yml CHANGED
@@ -1,7 +1,6 @@
1
1
  language: ruby
2
2
  script: rspec
3
3
  rvm:
4
- - 1.9.3
5
4
  - 2.0.0
6
5
  - 2.1.7
7
6
  - 2.2.3
data/README.md CHANGED
@@ -116,7 +116,7 @@ userRepository.remove(bob)
116
116
 
117
117
  p userRepository.find(john.id) # #<User:0x000000037237d0 @name="John Smith" ... >
118
118
  p userRepository.find(bob.id) # nil
119
- p userRepository.find!(bob.id) # nil
119
+ p userRepository.find!(bob.id) # raises Rmodel::NotFound
120
120
  ```
121
121
 
122
122
  ### Scopes
@@ -156,6 +156,34 @@ userRepository.query.have_email.remove
156
156
  p userRepository.query.count # 0
157
157
  ```
158
158
 
159
+ ### Timestamps
160
+
161
+ Here is an example how to track the time, when the entity was created and updated.
162
+
163
+ ```ruby
164
+ class Thing
165
+ attr_accessor :id, :name, :created_at, :updated_at
166
+ end
167
+
168
+ class ThingRepository < Rmodel::Mongo::Repository
169
+ simple_factory Thing, :name, :created_at, :updated_at
170
+ end
171
+ repo = ThingRepository.new
172
+
173
+ thing = Thing.new
174
+ thing.name = 'chair'
175
+ repo.insert(thing)
176
+ p thing.created_at
177
+
178
+ sleep 2
179
+
180
+ thing.name = 'table'
181
+ repo.update(thing)
182
+ p thing.updated_at
183
+ ```
184
+
185
+ To enable time tracking just add attributes `created_at` and `updated_at` or one of them to your entity.
186
+
159
187
  ## Contributing
160
188
 
161
189
  1. Fork it ( https://github.com/alexei-lexx/rmodel/fork )
@@ -0,0 +1,25 @@
1
+ require 'rmodel'
2
+
3
+ Rmodel.setup do
4
+ client :default, { hosts: [ 'localhost' ], database: 'test' }
5
+ end
6
+
7
+ class Thing
8
+ attr_accessor :id, :name, :created_at, :updated_at
9
+ end
10
+
11
+ class ThingRepository < Rmodel::Mongo::Repository
12
+ simple_factory Thing, :name, :created_at, :updated_at
13
+ end
14
+ repo = ThingRepository.new
15
+
16
+ thing = Thing.new
17
+ thing.name = 'chair'
18
+ repo.insert(thing)
19
+ p "#{thing.created_at} / #{thing.updated_at}"
20
+
21
+ sleep 2
22
+
23
+ thing.name = 'table'
24
+ repo.update(thing)
25
+ p "#{thing.created_at} / #{thing.updated_at}"
@@ -1,10 +1,12 @@
1
1
  require 'mongo'
2
2
  require 'active_support/inflector'
3
3
  require 'rmodel/mongo/repository_ext/queryable'
4
+ require 'rmodel/mongo/repository_ext/timestampable'
4
5
 
5
6
  module Rmodel::Mongo
6
7
  class Repository
7
8
  include RepositoryExt::Queryable
9
+ prepend RepositoryExt::Timestampable
8
10
 
9
11
  def initialize
10
12
  @client = Rmodel.setup.establish_mongo_client(self.class.client_name || :default) or
@@ -0,0 +1,20 @@
1
+ module Rmodel::Mongo
2
+ module RepositoryExt
3
+ module Timestampable
4
+
5
+ def insert(object)
6
+ if object.respond_to?(:created_at) && object.respond_to?(:created_at=)
7
+ object.created_at ||= Time.now
8
+ end
9
+ super
10
+ end
11
+
12
+ def update(object)
13
+ if object.respond_to?(:updated_at) && object.respond_to?(:updated_at=)
14
+ object.updated_at = Time.now
15
+ end
16
+ super
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Rmodel
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,73 @@
1
+ RSpec.describe Rmodel::Mongo::Repository do
2
+ describe Rmodel::Mongo::RepositoryExt::Timestampable do
3
+ subject(:repo) { ThingRepository.new }
4
+
5
+ context 'when the entity object has attributes created_at and updated_at' do
6
+ before do
7
+ stub_const('Thing', Struct.new(:id, :name, :created_at, :updated_at))
8
+ stub_const('ThingRepository', Class.new(Rmodel::Mongo::Repository) {
9
+ simple_factory Thing, :name, :created_at, :updated_at
10
+ })
11
+ end
12
+
13
+ context 'when we insert(object)' do
14
+ context 'and the object.created_at is already set' do
15
+ let(:thing) { Thing.new(nil, 'chair', Time.now) }
16
+
17
+ it 'doesnt change the value of created_at' do
18
+ set_created_at = thing.created_at
19
+ repo.insert(thing)
20
+ expect(thing.created_at).to eq set_created_at
21
+ end
22
+ end
23
+
24
+ context 'and the object.created_at is not set yet' do
25
+ let(:thing) { Thing.new(nil, 'chair') }
26
+
27
+ it 'sets the value of created_at' do
28
+ repo.insert(thing)
29
+ expect(thing.created_at).not_to be_nil
30
+ end
31
+ end
32
+ end
33
+
34
+ context 'when we update(object)' do
35
+ let(:thing) { Thing.new(nil, 'chair') }
36
+ before { repo.insert(thing) }
37
+
38
+ it 'sets the value of created_at' do
39
+ thing.name = 'table'
40
+ repo.update(thing)
41
+ expect(thing.updated_at).not_to be_nil
42
+ end
43
+ end
44
+ end
45
+
46
+ context 'when the entity has no attributes :created_at and updated_at' do
47
+ before do
48
+ stub_const('Thing', Struct.new(:id, :name))
49
+ stub_const('ThingRepository', Class.new(Rmodel::Mongo::Repository) {
50
+ simple_factory Thing, :name
51
+ })
52
+ end
53
+ let(:thing) { Thing.new(nil, 'chair') }
54
+
55
+ context 'when we insert(object)' do
56
+ it 'does nothing special' do
57
+ repo.insert(thing)
58
+ expect(thing.respond_to?(:created_at)).to be false
59
+ end
60
+ end
61
+
62
+ context 'when we update(object)' do
63
+ before { repo.insert(thing) }
64
+
65
+ it 'does nothing special' do
66
+ thing.name = 'table'
67
+ repo.update(thing)
68
+ expect(thing.respond_to?(:updated_at)).to be false
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rmodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexei
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-24 00:00:00.000000000 Z
11
+ date: 2015-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo
@@ -108,20 +108,23 @@ files:
108
108
  - LICENSE.txt
109
109
  - README.md
110
110
  - Rakefile
111
+ - examples/timestamps.rb
111
112
  - examples/user.rb
112
113
  - lib/rmodel.rb
113
114
  - lib/rmodel/errors.rb
114
115
  - lib/rmodel/mongo/repository.rb
115
116
  - lib/rmodel/mongo/repository_ext/query.rb
116
117
  - lib/rmodel/mongo/repository_ext/queryable.rb
118
+ - lib/rmodel/mongo/repository_ext/timestampable.rb
117
119
  - lib/rmodel/mongo/setup.rb
118
120
  - lib/rmodel/mongo/simple_factory.rb
119
121
  - lib/rmodel/setup.rb
120
122
  - lib/rmodel/version.rb
121
123
  - rmodel.gemspec
122
124
  - spec/rmodel/mongo/repository_crud_spec.rb
125
+ - spec/rmodel/mongo/repository_ext/queryable_spec.rb
126
+ - spec/rmodel/mongo/repository_ext/timestampable_spec.rb
123
127
  - spec/rmodel/mongo/repository_initialize_spec.rb
124
- - spec/rmodel/mongo/repository_queryable_spec.rb
125
128
  - spec/rmodel/mongo/simple_factory_spec.rb
126
129
  - spec/rmodel/setup_spec.rb
127
130
  - spec/shared/clean_moped.rb
@@ -152,8 +155,9 @@ specification_version: 4
152
155
  summary: Rmodel is an ORM library, which tends to follow the SOLID principles.
153
156
  test_files:
154
157
  - spec/rmodel/mongo/repository_crud_spec.rb
158
+ - spec/rmodel/mongo/repository_ext/queryable_spec.rb
159
+ - spec/rmodel/mongo/repository_ext/timestampable_spec.rb
155
160
  - spec/rmodel/mongo/repository_initialize_spec.rb
156
- - spec/rmodel/mongo/repository_queryable_spec.rb
157
161
  - spec/rmodel/mongo/simple_factory_spec.rb
158
162
  - spec/rmodel/setup_spec.rb
159
163
  - spec/shared/clean_moped.rb