uorm 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -15,6 +15,14 @@ GEM
15
15
  activesupport
16
16
  bson_ext (1.8.0)
17
17
  bson (~> 1.8.0)
18
+ columnize (0.3.6)
19
+ debugger (1.2.3)
20
+ columnize (>= 0.3.1)
21
+ debugger-linecache (~> 1.1.1)
22
+ debugger-ruby_core_source (~> 1.1.5)
23
+ debugger-linecache (1.1.2)
24
+ debugger-ruby_core_source (>= 1.1.1)
25
+ debugger-ruby_core_source (1.1.6)
18
26
  diff-lcs (1.1.3)
19
27
  em-mongo (0.4.3)
20
28
  bson (>= 1.1.3)
@@ -22,8 +30,11 @@ GEM
22
30
  em-synchrony (1.0.2)
23
31
  eventmachine (>= 1.0.0.beta.1)
24
32
  eventmachine (1.0.0)
33
+ hiredis (0.4.5)
25
34
  i18n (0.6.1)
26
35
  multi_json (1.5.0)
36
+ oj (2.0.0)
37
+ redis (3.0.2)
27
38
  rspec (2.12.0)
28
39
  rspec-core (~> 2.12.0)
29
40
  rspec-expectations (~> 2.12.0)
@@ -38,6 +49,10 @@ PLATFORMS
38
49
 
39
50
  DEPENDENCIES
40
51
  bson_ext
52
+ debugger
41
53
  em-mongo
54
+ hiredis
55
+ oj
56
+ redis
42
57
  rspec
43
58
  uorm!
@@ -0,0 +1,46 @@
1
+ module Uorm
2
+ module Redis
3
+ class Persistance
4
+ attr_reader :collection, :model
5
+
6
+ def initialize model
7
+ @model = model
8
+ @collection = ::Redis.new driver: :synchrony
9
+ end
10
+
11
+ def new? object
12
+ object.id ? false : true
13
+ end
14
+
15
+ def all options = {}
16
+ keys = collection.keys
17
+ collection.mget(keys).map do |val|
18
+ MultiJson.load val if val
19
+ end
20
+ end
21
+
22
+ def find id
23
+ MultiJson.load collection.get id
24
+ end
25
+
26
+ def create object
27
+ object.id = SecureRandom.uuid
28
+ write object
29
+ end
30
+
31
+ def update object
32
+ write object
33
+ end
34
+
35
+ def delete object
36
+ collection.del object.id
37
+ end
38
+
39
+ private
40
+
41
+ def write object
42
+ collection.set object.id, MultiJson.dump(object)
43
+ end
44
+ end
45
+ end
46
+ end
data/lib/uorm/redis.rb ADDED
@@ -0,0 +1,27 @@
1
+ module Uorm
2
+ module Redis
3
+ def self.included base
4
+ base.instance_eval do
5
+ field :id, type: Type::String
6
+
7
+ def persistance
8
+ @persistance ||= Persistance.new self
9
+ end
10
+
11
+ def collection
12
+ persistance.collection
13
+ end
14
+ end
15
+
16
+ def initialize values = {}
17
+ values[:id] ||= nil
18
+ super values
19
+ end
20
+
21
+ def persistance
22
+ self.class.persistance
23
+ end
24
+ end
25
+ end
26
+ end
27
+
data/lib/uorm/type.rb CHANGED
@@ -40,6 +40,8 @@ module Uorm
40
40
  class << self
41
41
  def convert value
42
42
  ::Time.parse value.to_s
43
+ rescue
44
+ ::Time.utc(value.to_i)
43
45
  end
44
46
 
45
47
  def as_json value
data/lib/uorm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Uorm
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
data/lib/uorm.rb CHANGED
@@ -3,6 +3,7 @@ require 'uorm/version'
3
3
  require 'active_support/callbacks'
4
4
  require 'active_support/core_ext/hash'
5
5
 
6
+
6
7
  module Uorm
7
8
  autoload :Type, 'uorm/type'
8
9
  autoload :Field, 'uorm/field'
@@ -11,8 +12,14 @@ module Uorm
11
12
  autoload :DSL, 'uorm/dsl'
12
13
  autoload :Callbacks, 'uorm/callbacks'
13
14
 
15
+ autoload :Redis, 'uorm/redis'
16
+ module Redis
17
+ autoload :Persistance, 'uorm/redis/persistance'
18
+ require 'multi_json'
19
+ end
20
+
14
21
  module EM
15
- autoload :Mongo, 'uorm/em-mongo'
22
+ autoload :Mongo, 'uorm/em-mongo'
16
23
 
17
24
  module Mongo
18
25
  require 'uorm/em-mongo/object_id'
@@ -20,4 +27,5 @@ module Uorm
20
27
  autoload :Persistance, 'uorm/em-mongo/persistance'
21
28
  end
22
29
  end
30
+
23
31
  end
@@ -1,22 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
- class CallbackTester
4
- class << self
5
- def after_create model
6
- model.id.to_s.should_not == ''
7
- end
3
+ require 'em-mongo'
4
+ require 'em-synchrony/em-mongo'
8
5
 
9
- def after_save model
10
- model.is_a?(AModel).should == true
11
- end
12
- end
13
- end
6
+ Uorm::EM::Mongo::DB.name = 'uorm_test'
14
7
 
15
- class AModel
8
+ class AMongoModel
16
9
  include Uorm::DSL
17
10
  include Uorm::Attributes
18
11
  include Uorm::EM::Mongo
19
- include Uorm::Callbacks
20
12
 
21
13
  field :reference, type: Uorm::Type::ObjectId
22
14
  field :name, type: Uorm::Type::String
@@ -24,13 +16,10 @@ class AModel
24
16
  field :dollars, type: Uorm::Type::Float
25
17
  field :favorite, type: Uorm::Type::Boolean
26
18
  field :created, type: Uorm::Type::Time
27
-
28
- set_callback :save, :after, CallbackTester
29
- set_callback :create, :after, CallbackTester
30
19
  end
31
20
 
32
- describe AModel do
33
- after { AModel.collection.remove }
21
+ describe AMongoModel do
22
+ after { AMongoModel.collection.remove }
34
23
 
35
24
  let(:attributes) do
36
25
  {
@@ -44,7 +33,7 @@ describe AModel do
44
33
  end
45
34
 
46
35
  describe '.new' do
47
- subject { AModel.new attributes }
36
+ subject { AMongoModel.new attributes }
48
37
 
49
38
  its(:reference) { should == BSON::ObjectId('50995b8c15529d4676000001') }
50
39
  its(:name) { should == 'whatever' }
@@ -68,93 +57,93 @@ describe AModel do
68
57
 
69
58
  describe '.all' do
70
59
  it 'returns a collection of all objects' do
71
- AModel.collection.insert name: 'findme'
60
+ AMongoModel.collection.insert name: 'findme'
72
61
 
73
- AModel.all.should be_collection_of AModel
62
+ AMongoModel.all.should be_collection_of AMongoModel
74
63
  end
75
64
 
76
65
  it 'returns a collection of objects with the query' do
77
- AModel.collection.insert name: 'findme'
66
+ AMongoModel.collection.insert name: 'findme'
78
67
 
79
- AModel.all(name: 'findme').should be_collection_of AModel
68
+ AMongoModel.all(name: 'findme').should be_collection_of AMongoModel
80
69
  end
81
70
 
82
71
  it 'converts time strings into time objects' do
83
- AModel.collection.insert created: Time.parse('2001-01-02')
72
+ AMongoModel.collection.insert created: Time.parse('2001-01-02')
84
73
 
85
- AModel.all(created: { '$gt' => '2001-01-01' }).
86
- should be_collection_of AModel
74
+ AMongoModel.all(created: { '$gt' => '2001-01-01' }).
75
+ should be_collection_of AMongoModel
87
76
 
88
- AModel.all(created: { '$lt' => Time.parse('2001-01-01')}).
77
+ AMongoModel.all(created: { '$lt' => Time.parse('2001-01-01')}).
89
78
  should be_empty
90
79
  end
91
80
 
92
81
  it 'converts reference strings into BSON::ObjectId objects' do
93
- AModel.collection.insert reference: BSON::ObjectId('50995b8c15529d4676000001')
82
+ AMongoModel.collection.insert reference: BSON::ObjectId('50995b8c15529d4676000001')
94
83
 
95
- AModel.all(reference: '50995b8c15529d4676000001').
96
- should be_collection_of AModel
84
+ AMongoModel.all(reference: '50995b8c15529d4676000001').
85
+ should be_collection_of AMongoModel
97
86
  end
98
87
 
99
88
  it 'converts multiple query parameters' do
100
- AModel.collection.insert reference: BSON::ObjectId('50995b8c15529d4676000001'), created: Time.parse('2003-01-01')
89
+ AMongoModel.collection.insert reference: BSON::ObjectId('50995b8c15529d4676000001'), created: Time.parse('2003-01-01')
101
90
 
102
- AModel.all(reference: '50995b8c15529d4676000001', created: '2003-01-01').
103
- should be_collection_of AModel
91
+ AMongoModel.all(reference: '50995b8c15529d4676000001', created: '2003-01-01').
92
+ should be_collection_of AMongoModel
104
93
  end
105
94
  end
106
95
 
107
96
  describe '.find' do
108
97
  it 'returns the first object with the given id' do
109
- model = AModel.create attributes
110
- AModel.find(model.id).should be_instance_of AModel
98
+ model = AMongoModel.create attributes
99
+ AMongoModel.find(model.id).should be_instance_of AMongoModel
111
100
  end
112
101
 
113
102
  it 'converts id BSON::ObjectId' do
114
- model = AModel.create attributes
115
- AModel.find(model.id.to_s).should be_instance_of AModel
103
+ model = AMongoModel.create attributes
104
+ AMongoModel.find(model.id.to_s).should be_instance_of AMongoModel
116
105
  end
117
106
  end
118
107
 
119
108
  describe '.create' do
120
109
  it 'adds the object to the collection' do
121
- expect { AModel.create attributes }.to change { AModel.collection.count }
110
+ expect { AMongoModel.create attributes }.to change { AMongoModel.collection.count }
122
111
  end
123
112
 
124
113
  it 'returns an object with the newly created attributes' do
125
- AModel.create(attributes).should be_instance_of AModel
114
+ AMongoModel.create(attributes).should be_instance_of AMongoModel
126
115
  end
127
116
  end
128
117
 
129
118
  describe '#save' do
130
119
  it 'creates the object if it does not have an id' do
131
- model = AModel.new attributes
120
+ model = AMongoModel.new attributes
132
121
  expect { model.save }.to change { model.id }
133
122
  end
134
123
 
135
124
  it 'updates the object if it does exist' do
136
- model = AModel.create attributes
125
+ model = AMongoModel.create attributes
137
126
  expect { model.save }.to_not change { model.id }
138
127
  end
139
128
  end
140
129
 
141
130
  describe '#update' do
142
131
  it 'updates the object with the given attributes' do
143
- model = AModel.create attributes
132
+ model = AMongoModel.create attributes
144
133
  expect { model.update name: 'new' }.to change { model.name }
145
134
  end
146
135
 
147
136
  it 'updates the collection with the given attributes' do
148
- model = AModel.create attributes
137
+ model = AMongoModel.create attributes
149
138
  expect { model.update name: 'another' }.
150
- to change { AModel.collection.find(name: 'another').count }
139
+ to change { AMongoModel.collection.find(name: 'another').count }
151
140
  end
152
141
  end
153
142
 
154
143
  describe '#delete' do
155
144
  it 'removes the object from the collection' do
156
- model = AModel.create attributes
157
- expect { model.delete }.to change { AModel.collection.count }
145
+ model = AMongoModel.create attributes
146
+ expect { model.delete }.to change { AMongoModel.collection.count }
158
147
  end
159
148
  end
160
149
  end
@@ -0,0 +1,112 @@
1
+ require 'spec_helper'
2
+
3
+ require 'redis'
4
+ require 'hiredis'
5
+
6
+ class ARedisModel
7
+ include Uorm::DSL
8
+ include Uorm::Attributes
9
+ include Uorm::Redis
10
+
11
+ field :reference, type: Uorm::Type::String
12
+ field :name, type: Uorm::Type::String
13
+ field :count, type: Uorm::Type::Integer
14
+ field :dollars, type: Uorm::Type::Float
15
+ field :favorite, type: Uorm::Type::Boolean
16
+ field :created, type: Uorm::Type::Time
17
+ end
18
+
19
+ describe ARedisModel do
20
+ after { ARedisModel.collection.flushdb }
21
+
22
+ let(:attributes) do
23
+ {
24
+ reference: '2d931510-d99f-494a-8c67-87feb05e1594',
25
+ name: 'whatever',
26
+ count: 5.to_s,
27
+ dollars: 2.3.to_s,
28
+ favorite: true.to_s,
29
+ created: Time.utc(0).to_s
30
+ }
31
+ end
32
+
33
+ describe '.new' do
34
+ subject { ARedisModel.new attributes }
35
+
36
+ its(:reference) { should == '2d931510-d99f-494a-8c67-87feb05e1594' }
37
+ its(:name) { should == 'whatever' }
38
+ its(:count) { should == 5 }
39
+ its(:dollars) { should == 2.3 }
40
+ its(:favorite) { should be_true }
41
+ its(:created) { should == Time.utc(0) }
42
+
43
+ its(:as_json) do
44
+ should == {
45
+ reference: '2d931510-d99f-494a-8c67-87feb05e1594',
46
+ name: "whatever",
47
+ count: 5,
48
+ dollars: 2.3,
49
+ favorite: true,
50
+ created: Time.utc(0).to_i,
51
+ id: nil
52
+ }
53
+ end
54
+ end
55
+
56
+
57
+ describe '.all' do
58
+ it 'returns a collection of all objects' do
59
+ ARedisModel.create attributes
60
+ ARedisModel.all.should be_collection_of ARedisModel
61
+ end
62
+ end
63
+
64
+ describe '.find' do
65
+ it 'returns the first object with the given id' do
66
+ model = ARedisModel.create attributes
67
+ ARedisModel.find(model.id).should be_instance_of ARedisModel
68
+ end
69
+ end
70
+
71
+ describe '.create' do
72
+ it 'adds the object to the collection' do
73
+ expect { ARedisModel.create attributes }.to change { ARedisModel.collection.dbsize }
74
+ end
75
+
76
+ it 'returns an object with the newly created attributes' do
77
+ ARedisModel.create(attributes).should be_instance_of ARedisModel
78
+ end
79
+ end
80
+
81
+ describe '#save' do
82
+ it 'creates the object if it does not have an id' do
83
+ model = ARedisModel.new attributes
84
+ expect { model.save }.to change { model.id }
85
+ end
86
+
87
+ it 'updates the object if it does exist' do
88
+ model = ARedisModel.create attributes
89
+ expect { model.save }.to_not change { model.id }
90
+ end
91
+ end
92
+
93
+ describe '#update' do
94
+ it 'updates the object with the given attributes' do
95
+ model = ARedisModel.create attributes
96
+ expect { model.update name: 'new' }.to change { model.name }
97
+ end
98
+
99
+ it 'updates the collection with the given attributes' do
100
+ model = ARedisModel.create attributes
101
+ expect { model.update name: 'another' }.
102
+ to change { ARedisModel.find(model.id).name }
103
+ end
104
+ end
105
+
106
+ describe '#delete' do
107
+ it 'removes the object from the collection' do
108
+ model = ARedisModel.create attributes
109
+ expect { model.delete }.to change { ARedisModel.collection.dbsize }
110
+ end
111
+ end
112
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,21 +1,14 @@
1
+ require 'support/be_collection_of'
1
2
  require 'uorm'
2
3
 
3
- require 'em-mongo'
4
4
  require 'em-synchrony'
5
- require 'em-synchrony/em-mongo'
6
-
7
- Uorm::EM::Mongo::DB.name = 'uorm_test'
8
-
9
- require 'support/be_collection_of'
10
5
 
11
6
  # monkey patch to wrap all tests in the reactor
12
7
  module RSpec
13
8
  module Core
14
9
  class ExampleGroup
15
10
  class << self
16
-
17
11
  alias_method :run_alias, :run
18
-
19
12
  def run(reporter)
20
13
  if EM.reactor_running?
21
14
  run_alias reporter
data/uorm.gemspec CHANGED
@@ -6,11 +6,11 @@ require 'uorm/version'
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = "uorm"
8
8
  gem.version = Uorm::VERSION
9
- gem.authors = ["Dennis Rogenius"]
10
- gem.email = ["denro03@gmail.com"]
9
+ gem.authors = ['Dennis Rogenius']
10
+ gem.email = ['denro03@gmail.com']
11
11
  gem.description = %q{A modular, flexible and lightweight ORM}
12
12
  gem.summary = %q{A small ORM for ruby with focus on weight and modularity}
13
- gem.homepage = ""
13
+ gem.homepage = 'https://github.com/denro/uorm'
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -21,6 +21,10 @@ Gem::Specification.new do |gem|
21
21
  gem.add_dependency 'em-synchrony'
22
22
 
23
23
  gem.add_development_dependency 'rspec'
24
+ gem.add_development_dependency 'debugger'
24
25
  gem.add_development_dependency 'em-mongo'
25
26
  gem.add_development_dependency 'bson_ext'
27
+ gem.add_development_dependency 'oj'
28
+ gem.add_development_dependency 'redis'
29
+ gem.add_development_dependency 'hiredis'
26
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uorm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -59,6 +59,22 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: debugger
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
62
78
  - !ruby/object:Gem::Dependency
63
79
  name: em-mongo
64
80
  requirement: !ruby/object:Gem::Requirement
@@ -91,6 +107,54 @@ dependencies:
91
107
  - - ! '>='
92
108
  - !ruby/object:Gem::Version
93
109
  version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: oj
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: redis
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: hiredis
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
94
158
  description: A modular, flexible and lightweight ORM
95
159
  email:
96
160
  - denro03@gmail.com
@@ -114,13 +178,16 @@ files:
114
178
  - lib/uorm/em-mongo/persistance.rb
115
179
  - lib/uorm/field.rb
116
180
  - lib/uorm/field_collection.rb
181
+ - lib/uorm/redis.rb
182
+ - lib/uorm/redis/persistance.rb
117
183
  - lib/uorm/type.rb
118
184
  - lib/uorm/version.rb
185
+ - spec/em-mongo_spec.rb
186
+ - spec/redis_synchrony_spec.rb
119
187
  - spec/spec_helper.rb
120
188
  - spec/support/be_collection_of.rb
121
- - spec/urm_spec.rb
122
189
  - uorm.gemspec
123
- homepage: ''
190
+ homepage: https://github.com/denro/uorm
124
191
  licenses: []
125
192
  post_install_message:
126
193
  rdoc_options: []
@@ -145,6 +212,7 @@ signing_key:
145
212
  specification_version: 3
146
213
  summary: A small ORM for ruby with focus on weight and modularity
147
214
  test_files:
215
+ - spec/em-mongo_spec.rb
216
+ - spec/redis_synchrony_spec.rb
148
217
  - spec/spec_helper.rb
149
218
  - spec/support/be_collection_of.rb
150
- - spec/urm_spec.rb