active_redis 0.0.2 → 0.0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/active_redis/associations.rb +15 -5
- data/lib/active_redis/attributes.rb +29 -10
- data/lib/active_redis/connection.rb +7 -0
- data/lib/active_redis/connection_ext/calculations_layer.rb +1 -1
- data/lib/active_redis/connection_ext/finders_layer.rb +5 -2
- data/lib/active_redis/connection_ext/persistence_layer.rb +1 -2
- data/lib/active_redis/connection_ext/scripts_layer.rb +9 -0
- data/lib/active_redis/finders.rb +10 -1
- data/lib/active_redis/helpers/lua_scripts.rb +2 -0
- data/lib/active_redis/naming.rb +4 -0
- data/lib/active_redis/persistence.rb +1 -1
- data/lib/active_redis/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YWEwZjlmY2YyODRkNTY3ZTU3NmVlZTlkOTFhNDU0MTM5NjMwNGY3NQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YzNmNjQwZGVhNDVmOTdlZWNmNTM4OTdhNmY2NzhiZTI4YmU2MDUyYg==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZjIyZDkwNDk5ODIyMmIzMDEzNzdlZjA3NWIzZmI1MmE5ZDU4YTJhMGYyNDA0
|
10
|
+
YTRlNjJhMWUwNDY0YTdmMjQ4NTdkMmU5MWY5ZDNlNjY5ZmZiYzE1ODk1NGI1
|
11
|
+
NWZjZDk2ZGI2N2ZhYmJhMmI1ZDZjN2U4YTY3ZDU0MTZhYzQ1NzY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZTRjYTNjY2EwNTNkMTEwOWQwOGJkMjJlZGMxNjdhY2FmODdhYjMxNDNmNTY3
|
14
|
+
Y2ExZTFkMmQ3OGU3NTQ1YmUyNWM4NjFkNWU4ODI3Njg3MjczYjBiZDAzYzZm
|
15
|
+
ZjkzNzE1MDllYTY4N2E5ZTFkZWIxMjQ5YjYxMWQzN2YwZWFjMWU=
|
@@ -7,9 +7,13 @@ module ActiveRedis
|
|
7
7
|
|
8
8
|
module ClassMethods
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
def has_one(name)
|
11
|
+
define_method name.to_s do
|
12
|
+
name.to_s.capitalize.constantize.where("#{self.class.foreign_key_name}" => self.id).first
|
13
|
+
end
|
14
|
+
define_method "#{name.to_s}=" do |value|
|
15
|
+
value.send "#{self.class.foreign_key_name}=", self.id
|
16
|
+
end
|
13
17
|
end
|
14
18
|
|
15
19
|
# TODO: add has_many functionality
|
@@ -18,8 +22,14 @@ module ActiveRedis
|
|
18
22
|
end
|
19
23
|
|
20
24
|
# TODO: add belongs_to functionality
|
21
|
-
def belongs_to
|
22
|
-
|
25
|
+
def belongs_to(name)
|
26
|
+
define_attributes_accessors "#{name.to_s}_id"
|
27
|
+
define_method name.to_s do
|
28
|
+
name.to_s.capitalize.constantize.find(self.send("#{name.to_s}_id"))
|
29
|
+
end
|
30
|
+
define_method "#{name.to_s}=" do |value|
|
31
|
+
self.send "#{name.to_s}_id=", value.id
|
32
|
+
end
|
23
33
|
end
|
24
34
|
|
25
35
|
end
|
@@ -6,7 +6,8 @@ module ActiveRedis
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def attributes=(value)
|
9
|
-
raise ActiveRedis::InvalidArgumentError, "Value must be a Hash"
|
9
|
+
raise ActiveRedis::InvalidArgumentError, "Value must be a Hash or Array" if !value.is_a?(Hash) && !value.is_a?(Array)
|
10
|
+
value = Hash[*value] if value.is_a?(Array)
|
10
11
|
value.each { |attribute, value| self.send("#{attribute}=", value) }
|
11
12
|
end
|
12
13
|
|
@@ -21,18 +22,36 @@ module ActiveRedis
|
|
21
22
|
|
22
23
|
def attributes(*attrs)
|
23
24
|
attrs = attrs.concat(ActiveRedis::Constants::DEFAULT_ATTRIBUTES)
|
25
|
+
class << self; attr_accessor :defined_attributes; end
|
26
|
+
self.defined_attributes ||= []
|
27
|
+
define_attributes_accessors attrs
|
28
|
+
end
|
29
|
+
|
30
|
+
def define_attributes_accessors(attrs)
|
31
|
+
attrs = *attrs unless attrs.is_a?(Array)
|
24
32
|
attrs.each do |attribute|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
33
|
+
read_attribute attribute
|
34
|
+
write_attribute attribute
|
35
|
+
register_attribute attribute
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def register_attribute(attribute)
|
42
|
+
self.defined_attributes << attribute unless self.defined_attributes.include?(attribute)
|
43
|
+
end
|
44
|
+
|
45
|
+
def read_attribute(attribute)
|
46
|
+
define_method attribute do
|
47
|
+
instance_variable_get("@#{attribute}")
|
31
48
|
end
|
32
|
-
|
33
|
-
|
49
|
+
end
|
50
|
+
|
51
|
+
def write_attribute(attribute)
|
52
|
+
define_method "#{attribute}=" do |value|
|
53
|
+
instance_variable_set("@#{attribute}", value)
|
34
54
|
end
|
35
|
-
self.defined_attributes = attrs
|
36
55
|
end
|
37
56
|
|
38
57
|
end
|
@@ -9,6 +9,7 @@ module ActiveRedis
|
|
9
9
|
|
10
10
|
include ActiveRedis::ConnectionExt::FindersLayer
|
11
11
|
include ActiveRedis::ConnectionExt::PersistenceLayer
|
12
|
+
include ActiveRedis::ConnectionExt::ScriptsLayer
|
12
13
|
|
13
14
|
calculations ActiveRedis::Constants::CALCULATION_METHODS
|
14
15
|
|
@@ -18,5 +19,11 @@ module ActiveRedis
|
|
18
19
|
|
19
20
|
attr_accessor :adapter
|
20
21
|
|
22
|
+
private
|
23
|
+
|
24
|
+
def run_eval(type, keys = [], argv = [])
|
25
|
+
adapter.eval send("#{type}_script"), keys: keys, argv: argv
|
26
|
+
end
|
27
|
+
|
21
28
|
end
|
22
29
|
end
|
@@ -12,7 +12,7 @@ module ActiveRedis::ConnectionExt
|
|
12
12
|
methods.each do |method|
|
13
13
|
eval_string += <<-EVAL
|
14
14
|
def calculate_#{method}(model, attributes = "")
|
15
|
-
|
15
|
+
run_eval :#{method}, [model.key_name], [attributes]
|
16
16
|
end
|
17
17
|
EVAL
|
18
18
|
end
|
@@ -2,7 +2,6 @@ module ActiveRedis::ConnectionExt
|
|
2
2
|
module FindersLayer
|
3
3
|
|
4
4
|
def fetch_row(model, id)
|
5
|
-
raise ActiveRedis::NotSpecifiedIdError, "Must specified ID for finding record!" unless id
|
6
5
|
adapter.hgetall model.table_name(id)
|
7
6
|
end
|
8
7
|
|
@@ -11,7 +10,11 @@ module ActiveRedis::ConnectionExt
|
|
11
10
|
end
|
12
11
|
|
13
12
|
def fetch_where(model, params)
|
14
|
-
|
13
|
+
run_eval :where, [model.key_name], params.flatten
|
14
|
+
end
|
15
|
+
|
16
|
+
def fetch_all(model)
|
17
|
+
fetch_where model, {}
|
15
18
|
end
|
16
19
|
|
17
20
|
end
|
@@ -8,9 +8,8 @@ module ActiveRedis::ConnectionExt
|
|
8
8
|
|
9
9
|
def next_id(model)
|
10
10
|
table = model.info_table_name
|
11
|
-
create_info_table(model) unless
|
11
|
+
create_info_table(model) unless adapter.exists(table)
|
12
12
|
adapter.hincrby table, "next_id", 1
|
13
|
-
adapter.hget table, "next_id"
|
14
13
|
end
|
15
14
|
|
16
15
|
def create_info_table(model)
|
data/lib/active_redis/finders.rb
CHANGED
@@ -2,12 +2,21 @@ module ActiveRedis
|
|
2
2
|
module Finders
|
3
3
|
|
4
4
|
def find(*ids)
|
5
|
-
ids.
|
5
|
+
res = ids.inject([]) do |result, id|
|
6
|
+
attrs = ActiveRedis.connection.fetch_row(self, id)
|
7
|
+
result << self.new(attrs) if attrs.any?
|
8
|
+
result
|
9
|
+
end
|
10
|
+
ids.count == 1 ? res.first : res
|
6
11
|
end
|
7
12
|
|
8
13
|
def where(params)
|
9
14
|
ActiveRedis.connection.fetch_where(self, params).map { |attrs| self.new(attrs) }
|
10
15
|
end
|
11
16
|
|
17
|
+
def all
|
18
|
+
ActiveRedis.connection.fetch_all(self).map { |attrs| self.new(attrs) }
|
19
|
+
end
|
20
|
+
|
12
21
|
end
|
13
22
|
end
|
data/lib/active_redis/naming.rb
CHANGED
data/lib/active_redis/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_redis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Gernyak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|
@@ -90,6 +90,7 @@ files:
|
|
90
90
|
- lib/active_redis/connection_ext/calculations_layer.rb
|
91
91
|
- lib/active_redis/connection_ext/finders_layer.rb
|
92
92
|
- lib/active_redis/connection_ext/persistence_layer.rb
|
93
|
+
- lib/active_redis/connection_ext/scripts_layer.rb
|
93
94
|
- lib/active_redis/constants.rb
|
94
95
|
- lib/active_redis/errors.rb
|
95
96
|
- lib/active_redis/finders.rb
|