active_redis 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +6 -14
  2. data/README.md +9 -5
  3. data/Rakefile +7 -0
  4. data/active_redis.gemspec +3 -0
  5. data/lib/active_redis.rb +9 -0
  6. data/lib/active_redis/associations.rb +25 -24
  7. data/lib/active_redis/associations/association.rb +36 -0
  8. data/lib/active_redis/associations/belongs_to_association.rb +32 -0
  9. data/lib/active_redis/associations/has_many_association.rb +26 -0
  10. data/lib/active_redis/associations/has_one_association.rb +24 -0
  11. data/lib/active_redis/attributes.rb +5 -4
  12. data/lib/active_redis/attributes/integer_attribute.rb +2 -2
  13. data/lib/active_redis/attributes/string_attribute.rb +2 -2
  14. data/lib/active_redis/attributes/time_attribute.rb +2 -2
  15. data/lib/active_redis/base.rb +4 -4
  16. data/lib/active_redis/config.rb +13 -10
  17. data/lib/active_redis/connection_ext/finders_layer.rb +12 -3
  18. data/lib/active_redis/constants.rb +2 -0
  19. data/lib/active_redis/errors.rb +6 -0
  20. data/lib/active_redis/helpers/lua_scripts.rb +7 -0
  21. data/lib/active_redis/inspector.rb +1 -1
  22. data/lib/active_redis/logs/basic_log.rb +11 -0
  23. data/lib/active_redis/logs/console_log.rb +14 -0
  24. data/lib/active_redis/logs/query_logger.rb +29 -0
  25. data/lib/active_redis/lua_scripts/main.lua +127 -50
  26. data/lib/active_redis/naming.rb +2 -0
  27. data/lib/active_redis/persistence.rb +12 -1
  28. data/lib/active_redis/query_chainer.rb +61 -0
  29. data/lib/active_redis/query_executor.rb +24 -0
  30. data/lib/active_redis/query_iterator.rb +27 -0
  31. data/lib/active_redis/railtie.rb +4 -2
  32. data/lib/active_redis/relation.rb +18 -0
  33. data/lib/active_redis/version.rb +1 -1
  34. data/specs/associations_spec.rb +94 -0
  35. data/specs/attributes_spec.rb +221 -0
  36. data/specs/calculations_spec.rb +28 -0
  37. data/specs/config_spec.rb +27 -0
  38. data/specs/inspector_spec.rb +45 -0
  39. data/specs/naming_spec.rb +56 -0
  40. data/specs/test_helper.rb +7 -0
  41. metadata +77 -18
  42. data/lib/active_redis/finders.rb +0 -22
@@ -1,12 +1,14 @@
1
1
  require 'active_redis/adapters/basic_adapter'
2
+ require 'active_redis/logs/console_log'
2
3
 
3
4
  module ActiveRedis
4
5
  class Railtie < Rails::Railtie
5
6
  config.after_initialize do
6
7
  ActiveRedis.connection = ActiveRedis::Connection.new(
7
- ActiveRedis.config.connection_options || {},
8
- ActiveRedis.config.adapter || ActiveRedis::Adapters::BasicAdapter
8
+ ActiveRedis.config.connection_options || {},
9
+ ActiveRedis.config.adapter || ActiveRedis::Adapters::BasicAdapter
9
10
  )
11
+ ActiveRedis.log = (ActiveRedis.config.log || ActiveRedis::Logs::ConsoleLog).new
10
12
  end
11
13
  end
12
14
  end
@@ -0,0 +1,18 @@
1
+ module ActiveRedis
2
+ autoload :QueryChainer, 'active_redis/query_chainer'
3
+ module Relation
4
+
5
+ %w{where order limit top}.each do |method|
6
+ class_eval <<-CODE
7
+ def #{method}(options = {})
8
+ if self.class.name == "ActiveRedis::QueryChainer"
9
+ self.apply_#{method}(options)
10
+ else
11
+ QueryChainer.new(self).apply_#{method}(options)
12
+ end
13
+ end
14
+ CODE
15
+ end
16
+
17
+ end
18
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveRedis
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -0,0 +1,94 @@
1
+ require File.dirname(File.realdirpath(__FILE__)) + '/test_helper.rb'
2
+ require 'active_redis/associations'
3
+ require 'active_redis/constants'
4
+ require 'active_redis/errors'
5
+
6
+ module ActiveRedis
7
+ module Associations
8
+ class SomeAssociation
9
+ def initialize(name, target);end
10
+ end
11
+ end
12
+ end
13
+
14
+ TestObject.class_eval do
15
+ def save;end
16
+ end
17
+
18
+ describe ActiveRedis::Associations do
19
+
20
+ subject { TestObject.new }
21
+
22
+ before do
23
+ TestObject.send :include, ActiveRedis::Associations
24
+ end
25
+
26
+ it "is has associations attribute" do
27
+ TestObject.must_respond_to :associations
28
+ TestObject.must_respond_to :associations=
29
+ end
30
+
31
+ describe "class methods" do
32
+
33
+ describe "#association" do
34
+
35
+ describe "when al least one association registered" do
36
+
37
+ let(:some_assoc) { mock() }
38
+
39
+ before do
40
+ TestObject.associations = {assoc: some_assoc}
41
+ end
42
+
43
+ it "is return registered association by name" do
44
+ TestObject.association(:assoc).must_be_same_as some_assoc
45
+ end
46
+
47
+ end
48
+
49
+ describe "when try to fetch unregistered association" do
50
+
51
+ before do
52
+ TestObject.associations = {}
53
+ end
54
+
55
+ it "is raise UnregisteredAssociationError" do
56
+ lambda{
57
+ TestObject.association(:assoc)
58
+ }.must_raise(ActiveRedis::UnregisteredAssociationError)
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+
65
+ ActiveRedis::Constants::ASSOCIATIONS.each do |assoc|
66
+ describe "##{assoc}" do
67
+
68
+ it "is register #{assoc} association with name" do
69
+ TestObject.expects(:register_association).with(:object, assoc.to_sym, {}).returns true
70
+ TestObject.send assoc, :object
71
+ end
72
+
73
+ end
74
+ end
75
+
76
+ describe "private methods" do
77
+
78
+ describe "#register_association" do
79
+
80
+ let(:assoc) { mock() }
81
+
82
+ it "is create certain class by assoc name" do
83
+ ActiveRedis::Associations::SomeAssociation.expects(:new).with(:object, TestObject, {}).returns assoc
84
+ TestObject.send :register_association, :object, :some, {}
85
+ TestObject.associations.must_be :has_key?, :object
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+
94
+ end
@@ -0,0 +1,221 @@
1
+ require File.dirname(File.realdirpath(__FILE__)) + '/test_helper.rb'
2
+ require 'active_redis/attributes'
3
+ require 'active_redis/constants'
4
+ require 'active_redis/errors'
5
+
6
+ module ActiveRedis
7
+ module Attributes
8
+ class SomeAttribute
9
+ def load(value);end
10
+ def dump(value);end
11
+ end
12
+ end
13
+ end
14
+
15
+ describe ActiveRedis::Attributes do
16
+
17
+ subject { TestObject.new }
18
+
19
+ before do
20
+ TestObject.send :include, ActiveRedis::Attributes
21
+ end
22
+
23
+ describe "instance methods" do
24
+
25
+ describe "#attributes=" do
26
+
27
+ describe "when passing invalid value" do
28
+
29
+ it "is raise InvalidArgumentError" do
30
+ lambda{
31
+ subject.send :attributes=, :bad_value
32
+ }.must_raise ActiveRedis::InvalidArgumentError
33
+ end
34
+
35
+ end
36
+
37
+ describe "when passing valid value" do
38
+
39
+ describe "when value is Hash" do
40
+
41
+ it "is mapping passing values to attributes" do
42
+ subject.expects(:attr1=).with(:attr1_value)
43
+ lambda{
44
+ subject.send :attributes=, attr1: :attr1_value
45
+ }.must_be_silent
46
+ end
47
+
48
+ end
49
+
50
+ describe "when value is Array" do
51
+
52
+ it "is mapping passing values to attributes" do
53
+ subject.expects(:attr1=).with(:attr1_value)
54
+ lambda{
55
+ subject.send :attributes=, [:attr1, :attr1_value]
56
+ }.must_be_silent
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+
65
+ describe "#attributes" do
66
+
67
+ before do
68
+ TestObject.stubs(:attributes_list).returns [:attr1]
69
+ subject.stubs(:attr1).returns :attr1_value
70
+ end
71
+
72
+ it "is return attributes hash" do
73
+ attrs = subject.attributes
74
+ attrs.must_be :count, 1
75
+ attrs.must_include :attr1
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+
82
+ describe "class methods" do
83
+
84
+ describe "#attributes" do
85
+
86
+ describe "when passing valid value" do
87
+
88
+ it "is raise InvalidArgumentError" do
89
+ lambda{
90
+ TestObject.attributes :attr1
91
+ }.must_raise ActiveRedis::InvalidArgumentError
92
+ end
93
+
94
+ end
95
+
96
+ describe "when passing Hash value" do
97
+
98
+ before do
99
+ ActiveRedis::Constants::DEFAULT_ATTRIBUTES = {}
100
+ end
101
+
102
+ it "is call define_attributes_accessors" do
103
+ TestObject.expects(:define_attributes_accessors).with(attr1: :some)
104
+ lambda{
105
+ TestObject.attributes attr1: :some
106
+ }.must_be_silent
107
+ TestObject.must_respond_to :defined_attributes
108
+ end
109
+
110
+ end
111
+
112
+ end
113
+
114
+ describe "#define_attributes_accessors" do
115
+
116
+ describe "when attribute is already registered" do
117
+
118
+ it "is doesn't define accessors functions" do
119
+ TestObject.expects(:read_attribute).at_least 0
120
+ TestObject.expects(:write_attribute).at_least 0
121
+ TestObject.expects(:register_attribute).with(:attr1, :some).returns false
122
+ TestObject.define_attributes_accessors attr1: :some
123
+ end
124
+
125
+ end
126
+
127
+ describe "when trying define new attribute" do
128
+
129
+ it "is define read/write attribute's functions" do
130
+ TestObject.expects(:read_attribute).with(:attr1)
131
+ TestObject.expects(:write_attribute).with(:attr1)
132
+ TestObject.expects(:register_attribute).with(:attr1, :some).returns true
133
+ TestObject.define_attributes_accessors attr1: :some
134
+ end
135
+
136
+ end
137
+
138
+ end
139
+
140
+ describe "#attributes_list" do
141
+
142
+ before do
143
+ TestObject.defined_attributes = {attr1: :some}
144
+ end
145
+
146
+ it "is return array with attributes names" do
147
+ TestObject.attributes_list.must_equal ["attr1"]
148
+ end
149
+
150
+ end
151
+
152
+ describe "private methods" do
153
+
154
+ describe "#register_attribute" do
155
+
156
+ describe "when attribute already exists" do
157
+
158
+ before do
159
+ TestObject.defined_attributes = {attr1: :some}
160
+ end
161
+
162
+ it "is return false" do
163
+ result = TestObject.send :register_attribute, :attr1, :some
164
+ result.must_equal false
165
+ end
166
+
167
+ end
168
+
169
+ describe "when try to register new attribute" do
170
+
171
+ before do
172
+ TestObject.defined_attributes = {}
173
+ end
174
+
175
+ it "is return true" do
176
+ result = TestObject.send :register_attribute, :attr1, :some
177
+ TestObject.defined_attributes.must_include :attr1
178
+ result.must_equal true
179
+ end
180
+
181
+ end
182
+
183
+ end
184
+
185
+ describe "#attribute_class" do
186
+
187
+ before do
188
+ TestObject.defined_attributes = {attr1: {class: :attr1_class}}
189
+ end
190
+
191
+ it "is return associated class" do
192
+ result = TestObject.send :attribute_class, :attr1
193
+ result.must_equal :attr1_class
194
+ end
195
+
196
+ end
197
+
198
+ describe "#read_attribute" do
199
+
200
+ it "is define read method for attribute" do
201
+ TestObject.expects(:define_method).with(:attr1)
202
+ TestObject.send :read_attribute, :attr1
203
+ end
204
+
205
+ end
206
+
207
+
208
+ describe "#write_attribute" do
209
+
210
+ it "is define write method for attribute" do
211
+ TestObject.expects(:define_method).with("attr1=")
212
+ TestObject.send :write_attribute, :attr1
213
+ end
214
+
215
+ end
216
+
217
+ end
218
+
219
+ end
220
+
221
+ end
@@ -0,0 +1,28 @@
1
+ require File.dirname(File.realdirpath(__FILE__)) + '/test_helper.rb'
2
+ require 'active_redis/calculations'
3
+ require 'active_redis/constants'
4
+ require 'active_redis/errors'
5
+
6
+ describe ActiveRedis::Calculations do
7
+
8
+ let(:connection) { mock() }
9
+
10
+ before do
11
+ TestObject.send :extend, ActiveRedis::Calculations
12
+ ActiveRedis.stubs(:connection).returns connection
13
+ end
14
+
15
+ ActiveRedis::Constants::CALCULATION_METHODS.each do |method|
16
+
17
+ describe "##{method}" do
18
+
19
+ it "is call #{method} calculate on connection" do
20
+ connection.expects("calculate_#{method}")
21
+ TestObject.send method
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(File.realdirpath(__FILE__)) + '/test_helper.rb'
2
+ require 'active_redis/config'
3
+
4
+ describe ActiveRedis::Config do
5
+
6
+ subject { ActiveRedis::Config.new }
7
+
8
+ let(:options) { :connection_options }
9
+ let(:adapter) { :adapter }
10
+ let(:log) { :log }
11
+
12
+ it "is has connection_options attribute" do
13
+ subject.connection_options = options
14
+ subject.connection_options.must_be_same_as options
15
+ end
16
+
17
+ it "is has adapter attribute" do
18
+ subject.adapter = adapter
19
+ subject.adapter.must_be_same_as adapter
20
+ end
21
+
22
+ it "is has log attribute" do
23
+ subject.log = log
24
+ subject.log.must_be_same_as log
25
+ end
26
+
27
+ end
@@ -0,0 +1,45 @@
1
+ require File.dirname(File.realdirpath(__FILE__)) + '/test_helper.rb'
2
+ require 'active_redis/inspector'
3
+
4
+ describe ActiveRedis::Inspector do
5
+
6
+ subject { TestObject.new }
7
+
8
+ before do
9
+ TestObject.send :include, ActiveRedis::Inspector
10
+ end
11
+
12
+ describe "#inspect" do
13
+
14
+ before do
15
+ TestObject.stubs(:attributes_list).returns []
16
+ end
17
+
18
+ it "is shouldn't be empty" do
19
+ subject.inspect.wont_be_empty
20
+ end
21
+
22
+ it "is contains class name" do
23
+ subject.inspect.must_match /TestObject/
24
+ end
25
+
26
+ describe "when attributes list isn't blank" do
27
+
28
+ before do
29
+ TestObject.stubs(:attributes_list).returns [:attr1]
30
+ subject.stubs(:attr1).returns "attr1_value"
31
+ end
32
+
33
+ it "is contains attribute name" do
34
+ subject.inspect.must_match /attr1/
35
+ end
36
+
37
+ it "is contains attribute value" do
38
+ subject.inspect.must_match /attr1_value/
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+
45
+ end