hash_store 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 +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +4 -0
- data/README.md +46 -7
- data/Rakefile +5 -0
- data/hash_store.gemspec +1 -0
- data/lib/hash_store/base.rb +16 -7
- data/lib/hash_store/version.rb +1 -1
- data/spec/lib/hash_store/base_spec.rb +30 -1
- data/spec/spec_helper.rb +2 -0
- metadata +16 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d20653833590acd7440f21df77f05a4534c37989
|
4
|
+
data.tar.gz: bb346d67ded438d67c61977136aa8f2ab85dbcf4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 396256c0ff8ffb3aae139e0087a251abfea1bb796667894ccc2c056e1ea170de4d02e8551f0358912d328200be4bad3f5ed22b1e2c6284b46f34879a3023b5a0
|
7
|
+
data.tar.gz: a83cec0b8eb9fb016e76b251747a13b72717d0c4f13856366900f9d37fd26b46bb1d5ce75d0b3f8eb62079feb71b41e494ebd357b81501693be3ed552dfaaba6
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,9 +1,18 @@
|
|
1
1
|
# HashStore
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/hash_store.png)](http://badge.fury.io/rb/hash_store)
|
4
|
+
[![Build Status](https://travis-ci.org/curi1119/hash_store.png?branch=master)](https://travis-ci.org/curi1119/hash_store)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/curi1119/hash_store/badge.png)](https://coveralls.io/r/curi1119/hash_store)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/curi1119/hash_store.png)](https://codeclimate.com/github/curi1119/hash_store)
|
7
|
+
[![Dependency Status](https://gemnasium.com/curi1119/hash_store.png)](https://gemnasium.com/curi1119/hash_store)
|
8
|
+
|
3
9
|
HashStore store RubyHash into Redis as JSON.
|
4
|
-
|
10
|
+
|
11
|
+
Automatically add redis command(GET,SET,DEL,EXITS) methods to your class.
|
5
12
|
HashStore was designed to work with ActiveRecord, but also work with Non-ActiveRecord Class.
|
6
13
|
|
14
|
+
-
|
15
|
+
|
7
16
|
## Installation
|
8
17
|
|
9
18
|
Add this line to your application's Gemfile:
|
@@ -44,22 +53,38 @@ class User < ActiveRecord::Base
|
|
44
53
|
key: ->(model){ "hoge:#{model.id}" },
|
45
54
|
hash: ->(model){ {id: model.id, name: model.name } }
|
46
55
|
```
|
56
|
+
see more detail
|
57
|
+
[schema](https://github.com/curi1119/hash_store/blob/master/spec/db/schema.rb)
|
58
|
+
[model](https://github.com/curi1119/hash_store/blob/master/spec/support/models.rb)
|
59
|
+
[sample data](https://github.com/curi1119/hash_store/blob/master/spec/factories/models.rb)
|
47
60
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
61
|
+
#### Default Behavior
|
62
|
+
```ruby
|
63
|
+
hash_store
|
64
|
+
```
|
65
|
+
This is default behavior.
|
66
|
+
Redis key will be "{table_name}:#{record id}" .
|
67
|
+
Hash will contain all columns except created_at, updated_at.
|
52
68
|
```
|
53
69
|
User#hash_store_key # => "users:1"
|
54
70
|
User#set_hash! # => SET command for this instance.
|
55
71
|
User#get_hash # => GET command for this instance.
|
56
72
|
nil if key not found.
|
57
73
|
return hash(like {"address"=>"Nagoya, Japan", "first_name"=>"Hoge", "id"=>7, "last_name"=>"Foo"})
|
74
|
+
User#get_hash(json: true) # => If you pass {json: true}, get_hash don't convert json to hash.
|
75
|
+
Its return json string("{\"address\":\"Nagoya, Japan\",\"first_name\":\"Hoge\",\"id\":8,\"last_name\":\"Foo\"}")
|
58
76
|
User#del_hash! # => DEL command for this instance.
|
59
77
|
User#exists_hash? # => true if key present on redis, false otherwise.
|
78
|
+
|
79
|
+
User.hash_store_key # => returns key proc object.
|
80
|
+
User.get_hash("users:1") # => returns same hash as User#get_hash
|
60
81
|
```
|
61
82
|
|
62
|
-
####
|
83
|
+
#### Customize Hash
|
84
|
+
```ruby
|
85
|
+
hash_store :address, hash: ->(model){ {address: model.address} }
|
86
|
+
```
|
87
|
+
You can customize hash.
|
63
88
|
```
|
64
89
|
User#hash_store_key_address # => "users:address:8"
|
65
90
|
User#set_hash_address!
|
@@ -68,9 +93,18 @@ User#get_hash_address # => GET command for this instance.
|
|
68
93
|
return hash(like {"address"=>"Nagoya, Japan"}
|
69
94
|
User#del_hash_address!
|
70
95
|
User#exists_hash_address?
|
96
|
+
|
97
|
+
User.hash_store_key_address
|
98
|
+
User.get_hash_address("users:address:8") # => returns same hash as User#get_hash_address
|
99
|
+
|
71
100
|
```
|
72
101
|
|
73
|
-
####
|
102
|
+
#### Customize Key and Hash
|
103
|
+
```ruby
|
104
|
+
hash_store :for_name, key: ->(model){ "hoge:#{model.id}" }, hash: ->(model){ {id: model.id, name: model.name } }`
|
105
|
+
```
|
106
|
+
|
107
|
+
And you can specify key for redis.
|
74
108
|
```
|
75
109
|
User#hash_store_key_for_name # => "hoge:10"
|
76
110
|
User#set_hash_for_name!
|
@@ -79,6 +113,9 @@ User#get_hash_for_name # => GET command for this instance.
|
|
79
113
|
return hash(like {"id"=>16, "name"=>"Hoge Foo"})
|
80
114
|
User#del_hash_for_name!
|
81
115
|
User#exists_hash_for_name?
|
116
|
+
|
117
|
+
User.hash_store_key_for_name
|
118
|
+
User.get_hash_for_name("hoge:10") # => returns same hash as User#get_hash_for_name
|
82
119
|
```
|
83
120
|
|
84
121
|
|
@@ -95,3 +132,5 @@ class Player
|
|
95
132
|
hash: ->(ins){ {name: ins.name} }
|
96
133
|
```
|
97
134
|
(same as above)
|
135
|
+
|
136
|
+
But, you must pass name, options(:key and :hash) arguments to hash_store.
|
data/Rakefile
CHANGED
data/hash_store.gemspec
CHANGED
data/lib/hash_store/base.rb
CHANGED
@@ -9,14 +9,14 @@ module HashStore::Base
|
|
9
9
|
raise unless options[:key].present?
|
10
10
|
rescue => e
|
11
11
|
print <<-EOS
|
12
|
-
When using hash_store on Non-ActiveRecord class, you MUST pass name and options arguments.
|
12
|
+
When using hash_store on Non-ActiveRecord class, you MUST pass name and options(:key and :hash) arguments.
|
13
13
|
ex.) hash_store :name, key: ->(ins){ "name_of_key:\#{ins.id}"}, hash: ->(ins){ {address: ins.address} }
|
14
14
|
EOS
|
15
15
|
return
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
method_suffix = name.nil? ? '' : "_#{name}"
|
19
|
+
method_suffix = (name.nil? || name == '') ? '' : "_#{name}"
|
20
20
|
|
21
21
|
if options[:hash].present?
|
22
22
|
hs_hash_proc = options[:hash]
|
@@ -38,10 +38,8 @@ EOS
|
|
38
38
|
hs_redis.set(hs_key_proc.call(self), json)
|
39
39
|
end
|
40
40
|
|
41
|
-
define_method "get_hash#{method_suffix}" do
|
42
|
-
|
43
|
-
return nil if json.nil?
|
44
|
-
MultiJson.decode(json)
|
41
|
+
define_method "get_hash#{method_suffix}" do |options=nil|
|
42
|
+
self.class.send("get_hash#{method_suffix}", hs_key_proc.call(self), options)
|
45
43
|
end
|
46
44
|
|
47
45
|
define_method "del_hash#{method_suffix}!" do
|
@@ -60,8 +58,19 @@ EOS
|
|
60
58
|
HashStore::Config.redis
|
61
59
|
end
|
62
60
|
private :hs_redis
|
61
|
+
|
62
|
+
define_singleton_method "get_hash#{method_suffix}" do |key, options=nil|
|
63
|
+
json = HashStore::Config.redis.get(key)
|
64
|
+
return nil if json.nil?
|
65
|
+
return json if options.present? && options[:json]
|
66
|
+
MultiJson.decode(json)
|
67
|
+
end
|
68
|
+
|
69
|
+
define_singleton_method "hash_store_key#{method_suffix}" do
|
70
|
+
hs_key_proc
|
71
|
+
end
|
63
72
|
end
|
64
|
-
end
|
65
73
|
|
74
|
+
end
|
66
75
|
end
|
67
76
|
end
|
data/lib/hash_store/version.rb
CHANGED
@@ -3,9 +3,23 @@ require 'spec_helper'
|
|
3
3
|
|
4
4
|
describe HashStore::Base do
|
5
5
|
|
6
|
+
|
6
7
|
context "hash_store on User Model without options" do
|
7
8
|
let!(:user){ create(:user) }
|
8
9
|
|
10
|
+
context "class methods on User" do
|
11
|
+
it{ User.hash_store_key.should be_instance_of(Proc) }
|
12
|
+
|
13
|
+
describe ".get_hash" do
|
14
|
+
it { User.get_hash("users:#{user.id}").should be_nil }
|
15
|
+
|
16
|
+
context "After SET" do
|
17
|
+
before { user.set_hash! }
|
18
|
+
it{ User.get_hash("users:#{user.id}").should == user.as_json(root: false, except: [:created_at, :updated_at]) }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
9
23
|
it { user.hash_store_key.should == "users:#{user.id}" }
|
10
24
|
|
11
25
|
describe "#set_hash!" do
|
@@ -21,6 +35,7 @@ describe HashStore::Base do
|
|
21
35
|
context "After SET" do
|
22
36
|
before { user.set_hash! }
|
23
37
|
it{ user.get_hash.should == user.as_json(root: false, except: [:created_at, :updated_at]) }
|
38
|
+
it{ user.get_hash(json: true).should == user.to_json(root: false, except: [:created_at, :updated_at]) }
|
24
39
|
end
|
25
40
|
end
|
26
41
|
|
@@ -45,6 +60,19 @@ describe HashStore::Base do
|
|
45
60
|
context "hash_store on User Model with options" do
|
46
61
|
let!(:user){ create(:user) }
|
47
62
|
|
63
|
+
context "class methods on User" do
|
64
|
+
it{ User.hash_store_key_for_name.should be_instance_of(Proc) }
|
65
|
+
|
66
|
+
describe ".get_hash_for_name" do
|
67
|
+
it { User.get_hash_for_name("hoge:#{user.id}").should be_nil }
|
68
|
+
|
69
|
+
context "After SET" do
|
70
|
+
before { user.set_hash_for_name! }
|
71
|
+
it{ User.get_hash_for_name("hoge:#{user.id}").should == user.as_json(root: false, only:[:id, ], methods: ["name"]) }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
48
76
|
it { user.hash_store_key_for_name.should == "hoge:#{user.id}" }
|
49
77
|
|
50
78
|
describe "#set_hash_for_name!" do
|
@@ -115,6 +143,7 @@ describe HashStore::Base do
|
|
115
143
|
}
|
116
144
|
it { player.hash_store_key_for_name.should == 'player:Curi:1' }
|
117
145
|
end
|
118
|
-
|
119
146
|
end
|
147
|
+
|
148
|
+
|
120
149
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
|
+
require 'coveralls'
|
2
3
|
require 'pry'
|
3
4
|
require 'factory_girl'
|
4
5
|
require 'active_record'
|
5
6
|
require 'mock_redis'
|
6
7
|
require 'hash_store'
|
8
|
+
Coveralls.wear!
|
7
9
|
|
8
10
|
ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(File.dirname(__FILE__) + "/db/database.yml")).result)
|
9
11
|
ActiveRecord::Base.establish_connection("sqlite3")
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- M.Iwasaki(Curi)
|
@@ -164,6 +164,20 @@ dependencies:
|
|
164
164
|
- - '>='
|
165
165
|
- !ruby/object:Gem::Version
|
166
166
|
version: '0'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: coveralls
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - '>='
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - '>='
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
167
181
|
description: |2
|
168
182
|
HashStore store RubyHash into Redis as JSON.
|
169
183
|
Automatically add redis commands(GET,SET,DEL,EXITS) methods to your class.
|
@@ -176,6 +190,7 @@ extra_rdoc_files: []
|
|
176
190
|
files:
|
177
191
|
- .gitignore
|
178
192
|
- .rspec
|
193
|
+
- .travis.yml
|
179
194
|
- Gemfile
|
180
195
|
- Guardfile
|
181
196
|
- LICENSE.txt
|