active_redis 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- Mzc0OWM3NzZhZmM1ZTViNWU1NmJlNDI3MzAyM2QyMDEzZDA1MmUzZg==
4
+ MjMwYzRjYzgwNGQzN2U4MTk4MDc0NGVhNzE5YmUzOWEyNjZjNmQ0Mw==
5
5
  data.tar.gz: !binary |-
6
- OWM3MjQwNTU2NDFjZDBmMzg5NWY5NjRjZGYyYmEwMGQzMDhlZTJhMA==
6
+ MmYwMzc0ZGI0YmZjMzczMTU2MTI0MzE2YzMyMjU0YTU3NDg2ZGQyNA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZmQzNzNlZjQzZTMxMDA0MmI2YjA3MWQzNWUzNDdjY2UwZDA5MWFkNzQ3MzI3
10
- MzUwYTMyZjEyMmY5YTU2YzJjN2NiZjM4ZGRhODc0NTY2MWY3MTcyMDViYjI2
11
- YzhmZmZhMGRmZjU0NDBlZWFiYmE3ZjVmOWQyZDZhMmI3OTI0MDI=
9
+ MzM1ZDAzMzZmMTE2ZjIxNTYxYzIzNTdhZjhkYzA0ZWM5OTljYmY2ZjM4NzEw
10
+ ZTEwYjZkMDg0NDJhYmNkODNiNmU5NjM1MDQ5N2FmZDk2NDQzODBmNjRhYTY1
11
+ NGI0MzA3YjM3MWQ2YTUwMWM0ZGZhOTgyZDQ1NGFjMjFkYmRiMDY=
12
12
  data.tar.gz: !binary |-
13
- ZmIyNTYzNjE0OTY0ZmIzZTkwOGY1OGU3MTcyYmIyNTk5NjY0M2UxNjM0YmQ4
14
- OTU3OWUwNDQwYTEyNTk5OTg0ODhjOWY5YzlkYjJmMDk4OTdmZWQ2ZDgwZWVl
15
- M2E5ODUxMzExYzEwMzViZTFmMmY5YjIwMDNkODU0ODllMThkMjY=
13
+ MjNmMTdiYWY1M2E3OWFiOWRlMTRlYzg3MmM5Mzg2MDQwYjg5YmQyMjM1Mjll
14
+ NzUzYjI4ZmU1OWIyMmZkYTgyZTY1NTUyZmI3OWY4ZjRhNjQ2YTBiZjFiYTM5
15
+ ZjliYWE0ODM0MDkxMTVmOWI0NWI1YjMzMTc4NjMxMWI2ZjkzOGY=
data/README.md CHANGED
@@ -67,26 +67,51 @@ article.update(title: "New article title") # => true
67
67
  p article.title # => New article title
68
68
 
69
69
  another_article.destroy
70
+
71
+ Article.destroy_all
72
+
73
+ p Article.count # => 0
70
74
  ```
71
75
 
72
76
  ### Finders and Calculations
73
77
 
74
- Now you may find 'row' by it's id
78
+ You may find 'row' by it's id
75
79
 
76
80
  ```ruby
77
81
  Article.find(1) # => [#<Article:0x007fec2526f4f8 @updated_at="1382608780", @link="http://someblog.com/1", @id="1", @title="Some title", @created_at="1382608780">]
78
82
  ```
79
83
 
80
- Now gem add support for some aggregation functions like __sum__, __min__, __max__, __pluck__.
84
+ Also gem add support for some aggregation functions like __sum__, __min__, __max__, __pluck__
85
+
86
+ ```ruby
87
+ class Article < ActiveRedis::Base
88
+ attributes :link, :title, :views
89
+ end
90
+
91
+ Article.create(views: 1000, link: "http://someblog.com/1", title: "Title article")
92
+ Article.create(views: 3000, link: "http://someblog.com/2", title: "Title article")
93
+
94
+ Article.sum(:views) # => 4000
95
+ Article.min(:views) # => 1000
96
+ Article.max(:views) # => 3000
97
+ Article.pluck(:id) # => ["1", "2"]
98
+ ```
99
+
100
+ From version 0.0.2 you are able to search item by multiple attributes using method __where__
101
+
102
+ ```ruby
103
+ Article.where(title: "Title article", views: 1000)
104
+ ```
81
105
 
82
106
  ### Future work
83
107
 
84
108
  At an early time I want to implement such features:
85
109
 
86
- 1. Add finders like ActiveRecord's where
87
- 2. Add _all_ class method
88
- 3. Add associations
89
- 4. Setting/getting attributes with it's types
110
+ 1. Add _all_ class method
111
+ 2. Implement Association module
112
+ 3. Setting/getting attributes with it's types
113
+ 4. Relational Operators in where
114
+ 5. Scopes???
90
115
 
91
116
  ## Contributing
92
117
 
@@ -10,5 +10,9 @@ module ActiveRedis::ConnectionExt
10
10
  adapter.keys model.key_name
11
11
  end
12
12
 
13
+ def fetch_where(model, params)
14
+ adapter.eval where_script, keys: [model.key_name], argv: params.flatten
15
+ end
16
+
13
17
  end
14
18
  end
@@ -5,7 +5,9 @@ module ActiveRedis
5
5
  ids.map { |id| self.new(ActiveRedis.connection.fetch_row(self, id)) }
6
6
  end
7
7
 
8
- # TODO: Add finders methods like where
8
+ def where(params)
9
+ ActiveRedis.connection.fetch_where(self, params).map { |attrs| self.new(attrs) }
10
+ end
9
11
 
10
12
  end
11
13
  end
@@ -11,6 +11,13 @@ module ActiveRedis
11
11
  end
12
12
  end
13
13
 
14
+ def where_script
15
+ <<-LUA
16
+ #{LuaLoader.get_main}
17
+ return where_finder(KEYS[1], ARGV)
18
+ LUA
19
+ end
20
+
14
21
  class LuaLoader
15
22
 
16
23
  def self.get_main
@@ -47,4 +47,26 @@ local calculate_sum = function (key, attr)
47
47
  sum = sum + s
48
48
  end
49
49
  return sum
50
+ end
51
+
52
+ local is_match_record = function (key, argv)
53
+ for i = 1, #argv, 2 do
54
+ local k = argv[i]
55
+ local value = argv[i+1]
56
+ if (redis.call("HGET", key, k) ~= value) then
57
+ return false
58
+ end
59
+ end
60
+ return true
61
+ end
62
+
63
+ local where_finder = function (key, argv)
64
+ local result = {}
65
+ local ks = keys(key)
66
+ for index, k in pairs(ks) do
67
+ if is_match_record(k, argv) == true then
68
+ table.insert(result, redis.call("HGETALL", k))
69
+ end
70
+ end
71
+ return result
50
72
  end
@@ -6,7 +6,7 @@ module ActiveRedis
6
6
  end
7
7
 
8
8
  def initialize(attrs = {})
9
- self.attributes = attrs
9
+ self.attributes = Hash[*attrs]
10
10
  end
11
11
 
12
12
  def save
@@ -1,3 +1,3 @@
1
1
  module ActiveRedis
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_redis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Gernyak