hash_store 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Guardfile +12 -0
- data/LICENSE.txt +22 -0
- data/README.md +97 -0
- data/Rakefile +1 -0
- data/hash_store.gemspec +39 -0
- data/lib/hash_store.rb +33 -0
- data/lib/hash_store/base.rb +67 -0
- data/lib/hash_store/version.rb +5 -0
- data/spec/db/database.yml +3 -0
- data/spec/db/schema.rb +15 -0
- data/spec/factories/models.rb +15 -0
- data/spec/lib/hash_store/base_spec.rb +120 -0
- data/spec/lib/hash_store_spec.rb +56 -0
- data/spec/spec_helper.rb +32 -0
- data/spec/support/models.rb +63 -0
- metadata +226 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ac95003b61d541bf721bc6bb6c118de0f56a0ea7
|
4
|
+
data.tar.gz: bc63c6e292764a7d95efed92e2d5d4e2d3c4d8ae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 256273408ebee938bd6d4714b2ab9597af758c22b389b9ffcaa9d01aa1d08d239deca0b35f0bd38b57573fc24ad8e36521f26e1cd2ada5dbcc16e37cd4de6e32
|
7
|
+
data.tar.gz: 7e3fa18a5b7c7f9bc24902cdee9cfa8ab487c6696bd00e678888f6aad2d130de27c74d086c3c4c608932269b59e1c6c7429e030b66d9d738f6845501a41ae802
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard :rspec, cli: "--color --format nested" do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
#watch(%r{^spec/support/(.+)\.rb$'}) { |m| p m }
|
7
|
+
watch('spec/support/models.rb') { "spec" }
|
8
|
+
watch(%r{^lib/(.+)\.rb$}) { |m|
|
9
|
+
"spec/lib/#{m[1]}_spec.rb" }
|
10
|
+
watch('spec/spec_helper.rb') { "spec" }
|
11
|
+
|
12
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 curi
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
# HashStore
|
2
|
+
|
3
|
+
HashStore store RubyHash into Redis as JSON.
|
4
|
+
Automatically add redis commands(GET,SET,DEL,EXITS) methods to your class.
|
5
|
+
HashStore was designed to work with ActiveRecord, but also work with Non-ActiveRecord Class.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'hash_store'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install hash_store
|
20
|
+
|
21
|
+
## Getting Start
|
22
|
+
|
23
|
+
Create redis connection, and pass it to HashStore.
|
24
|
+
|
25
|
+
For Rails apps example:
|
26
|
+
|
27
|
+
config/initializers/redis.rb
|
28
|
+
```ruby
|
29
|
+
redis = Redis.new(host: "192.168.1.1", port: 6380)
|
30
|
+
HashStore::Config.redis = redis
|
31
|
+
```
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
### For Rails example:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
class User < ActiveRecord::Base
|
39
|
+
hash_store
|
40
|
+
hash_store :address,
|
41
|
+
hash: ->(model){ {address: model.address} }
|
42
|
+
|
43
|
+
hash_store :for_name,
|
44
|
+
key: ->(model){ "hoge:#{model.id}" },
|
45
|
+
hash: ->(model){ {id: model.id, name: model.name } }
|
46
|
+
```
|
47
|
+
|
48
|
+
User instance will gain theses methods:
|
49
|
+
|
50
|
+
#### hash_store
|
51
|
+
|
52
|
+
```
|
53
|
+
User#hash_store_key # => "users:1"
|
54
|
+
User#set_hash! # => SET command for this instance.
|
55
|
+
User#get_hash # => GET command for this instance.
|
56
|
+
nil if key not found.
|
57
|
+
return hash(like {"address"=>"Nagoya, Japan", "first_name"=>"Hoge", "id"=>7, "last_name"=>"Foo"})
|
58
|
+
User#del_hash! # => DEL command for this instance.
|
59
|
+
User#exists_hash? # => true if key present on redis, false otherwise.
|
60
|
+
```
|
61
|
+
|
62
|
+
#### hash_store :address
|
63
|
+
```
|
64
|
+
User#hash_store_key_address # => "users:address:8"
|
65
|
+
User#set_hash_address!
|
66
|
+
User#get_hash_address # => GET command for this instance.
|
67
|
+
nil if key not found.
|
68
|
+
return hash(like {"address"=>"Nagoya, Japan"}
|
69
|
+
User#del_hash_address!
|
70
|
+
User#exists_hash_address?
|
71
|
+
```
|
72
|
+
|
73
|
+
#### hash_store :for_name
|
74
|
+
```
|
75
|
+
User#hash_store_key_for_name # => "hoge:10"
|
76
|
+
User#set_hash_for_name!
|
77
|
+
User#get_hash_for_name # => GET command for this instance.
|
78
|
+
nil if key not found.
|
79
|
+
return hash(like {"id"=>16, "name"=>"Hoge Foo"})
|
80
|
+
User#del_hash_for_name!
|
81
|
+
User#exists_hash_for_name?
|
82
|
+
```
|
83
|
+
|
84
|
+
|
85
|
+
### For Non-ActiveRecord class example:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
class Player
|
89
|
+
include HashStore
|
90
|
+
hash_store nil,
|
91
|
+
key: ->(ins){ "player:#{ins.id}" },
|
92
|
+
hash: ->(ins){ {body: ins.body} }
|
93
|
+
hash_store :for_name,
|
94
|
+
key: ->(ins){ "player:#{ins.name}:#{ins.id}" },
|
95
|
+
hash: ->(ins){ {name: ins.name} }
|
96
|
+
```
|
97
|
+
(same as above)
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/hash_store.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hash_store/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hash_store"
|
8
|
+
spec.version = HashStore::VERSION
|
9
|
+
spec.authors = ["M.Iwasaki(Curi)"]
|
10
|
+
spec.email = ["curi1119@gmail.com"]
|
11
|
+
spec.description = <<-EOS
|
12
|
+
HashStore store RubyHash into Redis as JSON.
|
13
|
+
Automatically add redis commands(GET,SET,DEL,EXITS) methods to your class.
|
14
|
+
HashStore was designed to work with ActiveRecord, but also work with Non-ActiveRecord Class.
|
15
|
+
EOS
|
16
|
+
|
17
|
+
spec.summary = %q{HashStore store RubyHash into Redis as JSON.}
|
18
|
+
spec.homepage = "https://github.com/curi1119/hash_store"
|
19
|
+
spec.license = "MIT"
|
20
|
+
|
21
|
+
spec.files = `git ls-files`.split($/)
|
22
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
23
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
|
27
|
+
spec.add_runtime_dependency 'redis'
|
28
|
+
spec.add_runtime_dependency 'multi_json'
|
29
|
+
|
30
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
31
|
+
spec.add_development_dependency 'rake'
|
32
|
+
spec.add_development_dependency 'pry'
|
33
|
+
spec.add_development_dependency 'rspec'
|
34
|
+
spec.add_development_dependency 'guard-rspec'
|
35
|
+
spec.add_development_dependency 'mock_redis'
|
36
|
+
spec.add_development_dependency 'factory_girl'
|
37
|
+
spec.add_development_dependency 'sqlite3'
|
38
|
+
spec.add_development_dependency 'activerecord'
|
39
|
+
end
|
data/lib/hash_store.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require "redis"
|
3
|
+
require "multi_json"
|
4
|
+
require "hash_store/version"
|
5
|
+
require "hash_store/base"
|
6
|
+
|
7
|
+
module HashStore
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.send :extend, HashStore::Base::ClassMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
module Config
|
14
|
+
class << self
|
15
|
+
|
16
|
+
def redis=(redis)
|
17
|
+
@@redis = redis
|
18
|
+
@@redis.ping
|
19
|
+
end
|
20
|
+
|
21
|
+
def redis
|
22
|
+
if !defined?(@@redis) || @@redis.nil?
|
23
|
+
raise 'no redis'
|
24
|
+
end
|
25
|
+
@@redis
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
if defined?(ActiveRecord::Base)
|
32
|
+
ActiveRecord::Base.send :include, HashStore
|
33
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
module HashStore::Base
|
4
|
+
module ClassMethods
|
5
|
+
def hash_store(name=nil, options={})
|
6
|
+
unless self.ancestors.include?(ActiveRecord::Base)
|
7
|
+
begin
|
8
|
+
raise unless options[:hash].present?
|
9
|
+
raise unless options[:key].present?
|
10
|
+
rescue => e
|
11
|
+
print <<-EOS
|
12
|
+
When using hash_store on Non-ActiveRecord class, you MUST pass name and options arguments.
|
13
|
+
ex.) hash_store :name, key: ->(ins){ "name_of_key:\#{ins.id}"}, hash: ->(ins){ {address: ins.address} }
|
14
|
+
EOS
|
15
|
+
return
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
method_suffix = name.nil? ? '' : "_#{name}"
|
20
|
+
|
21
|
+
if options[:hash].present?
|
22
|
+
hs_hash_proc = options[:hash]
|
23
|
+
else
|
24
|
+
hs_hash_proc = ->(model){ model.as_json(root: false, except: [:created_at, :updated_at]) }
|
25
|
+
end
|
26
|
+
|
27
|
+
if options[:key].present?
|
28
|
+
hs_key_proc = options[:key]
|
29
|
+
elsif name.present? && options[:key].nil?
|
30
|
+
hs_key_proc = ->(model){ "#{model.class.table_name}:#{name}:#{model.id}" }
|
31
|
+
else
|
32
|
+
hs_key_proc = ->(model){ "#{model.class.table_name}:#{model.id}" }
|
33
|
+
end
|
34
|
+
|
35
|
+
class_eval do
|
36
|
+
define_method "set_hash#{method_suffix}!" do
|
37
|
+
json = MultiJson.encode(hs_hash_proc.call(self))
|
38
|
+
hs_redis.set(hs_key_proc.call(self), json)
|
39
|
+
end
|
40
|
+
|
41
|
+
define_method "get_hash#{method_suffix}" do
|
42
|
+
json = hs_redis.get(hs_key_proc.call(self))
|
43
|
+
return nil if json.nil?
|
44
|
+
MultiJson.decode(json)
|
45
|
+
end
|
46
|
+
|
47
|
+
define_method "del_hash#{method_suffix}!" do
|
48
|
+
hs_redis.del(hs_key_proc.call(self))
|
49
|
+
end
|
50
|
+
|
51
|
+
define_method "exists_hash#{method_suffix}?" do
|
52
|
+
hs_redis.exists(hs_key_proc.call(self))
|
53
|
+
end
|
54
|
+
|
55
|
+
define_method "hash_store_key#{method_suffix}" do
|
56
|
+
hs_key_proc.call(self)
|
57
|
+
end
|
58
|
+
|
59
|
+
def hs_redis
|
60
|
+
HashStore::Config.redis
|
61
|
+
end
|
62
|
+
private :hs_redis
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
data/spec/db/schema.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
ActiveRecord::Schema.define(version: 0) do
|
3
|
+
|
4
|
+
create_table :users do |t|
|
5
|
+
t.string :first_name, null: false
|
6
|
+
t.string :last_name, null: false
|
7
|
+
t.text :address
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
|
11
|
+
create_table :cats do |t|
|
12
|
+
t.string :name, null: false
|
13
|
+
t.timestamps
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe HashStore::Base do
|
5
|
+
|
6
|
+
context "hash_store on User Model without options" do
|
7
|
+
let!(:user){ create(:user) }
|
8
|
+
|
9
|
+
it { user.hash_store_key.should == "users:#{user.id}" }
|
10
|
+
|
11
|
+
describe "#set_hash!" do
|
12
|
+
before { user.set_hash! }
|
13
|
+
subject { $redis.get("users:#{user.id}") }
|
14
|
+
it {
|
15
|
+
should == user.to_json(root: false, except: [:created_at, :updated_at]) }
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#get_hash" do
|
19
|
+
it { user.get_hash.should be_nil }
|
20
|
+
|
21
|
+
context "After SET" do
|
22
|
+
before { user.set_hash! }
|
23
|
+
it{ user.get_hash.should == user.as_json(root: false, except: [:created_at, :updated_at]) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#del_hash!" do
|
28
|
+
before {
|
29
|
+
user.set_hash!
|
30
|
+
user.del_hash!
|
31
|
+
}
|
32
|
+
it { $redis.get("users:#{user.id}").should be_nil }
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#exists_hash?" do
|
36
|
+
it { user.exists_hash?.should be_false }
|
37
|
+
|
38
|
+
context "After SET" do
|
39
|
+
before { user.set_hash! }
|
40
|
+
it { user.exists_hash?.should be_true }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "hash_store on User Model with options" do
|
46
|
+
let!(:user){ create(:user) }
|
47
|
+
|
48
|
+
it { user.hash_store_key_for_name.should == "hoge:#{user.id}" }
|
49
|
+
|
50
|
+
describe "#set_hash_for_name!" do
|
51
|
+
before { user.set_hash_for_name! }
|
52
|
+
subject { $redis.get("hoge:#{user.id}") }
|
53
|
+
it { should == user.to_json(root: false, only:[:id], methods: [:name]) }
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#get_hash" do
|
57
|
+
it { user.get_hash_for_name.should be_nil }
|
58
|
+
|
59
|
+
context "After SET" do
|
60
|
+
before { user.set_hash_for_name! }
|
61
|
+
it{ user.get_hash_for_name.should == user.as_json(root: false, only:[:id, ], methods: ["name"]) }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#del_hash!" do
|
66
|
+
before {
|
67
|
+
user.set_hash_for_name!
|
68
|
+
user.del_hash_for_name!
|
69
|
+
}
|
70
|
+
it { $redis.get("users:#{user.id}").should be_nil }
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "#exists_hash_for_name?" do
|
74
|
+
it { user.exists_hash_for_name?.should be_false }
|
75
|
+
|
76
|
+
context "After SET" do
|
77
|
+
before { user.set_hash_for_name! }
|
78
|
+
it { user.exists_hash_for_name?.should be_true }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context "hash_store on User Model with name, witout key options" do
|
84
|
+
let!(:user){ create(:user) }
|
85
|
+
|
86
|
+
context "when name is passed, and key option hadn't passed" do
|
87
|
+
it { user.hash_store_key_address.should == "users:address:#{user.id}" }
|
88
|
+
end
|
89
|
+
|
90
|
+
context "hash option should work fine" do
|
91
|
+
before { user.set_hash_address! }
|
92
|
+
it{ user.get_hash_address.should == { 'address' => user.address } }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "hash_store on Player Model without options" do
|
97
|
+
let!(:player){ Player.new }
|
98
|
+
|
99
|
+
context "when name is nil" do
|
100
|
+
before { player.set_hash! }
|
101
|
+
it { player.get_hash.should == {'body' => 'hello world'} }
|
102
|
+
it {
|
103
|
+
player.del_hash!
|
104
|
+
player.get_hash.should be_nil
|
105
|
+
}
|
106
|
+
it { player.hash_store_key.should == 'player:1' }
|
107
|
+
end
|
108
|
+
|
109
|
+
describe "#set_hash_for_name!" do
|
110
|
+
before { player.set_hash_for_name! }
|
111
|
+
it { player.get_hash_for_name.should == {'name' => 'Curi'} }
|
112
|
+
it {
|
113
|
+
player.del_hash_for_name!
|
114
|
+
player.get_hash_for_name.should be_nil
|
115
|
+
}
|
116
|
+
it { player.hash_store_key_for_name.should == 'player:Curi:1' }
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe HashStore do
|
5
|
+
|
6
|
+
context "ActiveRecord Model with hash_store" do
|
7
|
+
let!(:user){ create(:user) }
|
8
|
+
|
9
|
+
subject{ user }
|
10
|
+
it{ should respond_to(:set_hash!) }
|
11
|
+
it{ should respond_to(:get_hash) }
|
12
|
+
it{ should respond_to(:del_hash!) }
|
13
|
+
it{ should respond_to(:exists_hash?) }
|
14
|
+
|
15
|
+
it{ should respond_to(:set_hash_for_name!) }
|
16
|
+
it{ should respond_to(:get_hash_for_name) }
|
17
|
+
it{ should respond_to(:del_hash_for_name!) }
|
18
|
+
it{ should respond_to(:exists_hash_for_name?) }
|
19
|
+
end
|
20
|
+
|
21
|
+
context "ActiveRecord Model without hash_store" do
|
22
|
+
let!(:cat){ create(:cat) }
|
23
|
+
|
24
|
+
subject{ cat }
|
25
|
+
it{ should_not respond_to(:set_hash!) }
|
26
|
+
it{ should_not respond_to(:get_hash) }
|
27
|
+
it{ should_not respond_to(:del_hash!) }
|
28
|
+
it{ should_not respond_to(:exists_hash?) }
|
29
|
+
end
|
30
|
+
|
31
|
+
context "Regular Class with hash_store" do
|
32
|
+
let!(:player){ Player.new }
|
33
|
+
|
34
|
+
subject{ player }
|
35
|
+
it{ should respond_to(:set_hash!) }
|
36
|
+
it{ should respond_to(:get_hash) }
|
37
|
+
it{ should respond_to(:del_hash!) }
|
38
|
+
it{ should respond_to(:exists_hash?) }
|
39
|
+
|
40
|
+
it{ should respond_to(:set_hash_for_name!) }
|
41
|
+
it{ should respond_to(:get_hash_for_name) }
|
42
|
+
it{ should respond_to(:del_hash_for_name!) }
|
43
|
+
it{ should respond_to(:exists_hash_for_name?) }
|
44
|
+
end
|
45
|
+
|
46
|
+
context "Regular Class with invalid hash_store arguments" do
|
47
|
+
[ThisIsErrro1, ThisIsErrro2, ThisIsErrro3, ThisIsErrro4].each do |klass|
|
48
|
+
subject{ klass.new }
|
49
|
+
it{ should_not respond_to(:set_hash!) }
|
50
|
+
it{ should_not respond_to(:get_hash) }
|
51
|
+
it{ should_not respond_to(:del_hash!) }
|
52
|
+
it{ should_not respond_to(:exists_hash?) }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'pry'
|
3
|
+
require 'factory_girl'
|
4
|
+
require 'active_record'
|
5
|
+
require 'mock_redis'
|
6
|
+
require 'hash_store'
|
7
|
+
|
8
|
+
ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(File.dirname(__FILE__) + "/db/database.yml")).result)
|
9
|
+
ActiveRecord::Base.establish_connection("sqlite3")
|
10
|
+
ActiveRecord::Migration.verbose = false
|
11
|
+
load(File.join(File.dirname(__FILE__), "db", "schema.rb"))
|
12
|
+
|
13
|
+
require 'support/models'
|
14
|
+
require 'factories/models'
|
15
|
+
|
16
|
+
RSpec.configure do |config|
|
17
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
18
|
+
config.run_all_when_everything_filtered = true
|
19
|
+
config.filter_run :focus
|
20
|
+
|
21
|
+
config.include FactoryGirl::Syntax::Methods
|
22
|
+
# Run specs in random order to surface order dependencies. If you find an
|
23
|
+
# order dependency and want to debug it, you can fix the order by providing
|
24
|
+
# the seed, which is printed after each run.
|
25
|
+
# --seed 1234
|
26
|
+
config.order = 'random'
|
27
|
+
|
28
|
+
|
29
|
+
config.before(:each) { HashStore::Config.redis.flushdb }
|
30
|
+
end
|
31
|
+
$redis = MockRedis.new
|
32
|
+
HashStore::Config.redis = $redis
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
class User < ActiveRecord::Base
|
4
|
+
hash_store
|
5
|
+
hash_store :address,
|
6
|
+
hash: ->(model){ {address: model.address} }
|
7
|
+
|
8
|
+
hash_store :for_name,
|
9
|
+
key: ->(model){ "hoge:#{model.id}" },
|
10
|
+
hash: ->(model){ {id: model.id, name: model.name } }
|
11
|
+
def name
|
12
|
+
"#{self.first_name} #{self.last_name}"
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
class Cat < ActiveRecord::Base
|
18
|
+
end
|
19
|
+
|
20
|
+
class Player
|
21
|
+
include HashStore
|
22
|
+
hash_store nil,
|
23
|
+
key: ->(ins){ "player:#{ins.id}" },
|
24
|
+
hash: ->(ins){ {body: ins.body} }
|
25
|
+
hash_store :for_name,
|
26
|
+
key: ->(ins){ "player:#{ins.name}:#{ins.id}" },
|
27
|
+
hash: ->(ins){ {name: ins.name} }
|
28
|
+
|
29
|
+
def id
|
30
|
+
1
|
31
|
+
end
|
32
|
+
|
33
|
+
def name
|
34
|
+
"Curi"
|
35
|
+
end
|
36
|
+
|
37
|
+
def body
|
38
|
+
"hello world"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class ThisIsErrro1
|
43
|
+
include HashStore
|
44
|
+
hash_store
|
45
|
+
end
|
46
|
+
class ThisIsErrro2
|
47
|
+
include HashStore
|
48
|
+
hash_store :name
|
49
|
+
end
|
50
|
+
class ThisIsErrro3
|
51
|
+
include HashStore
|
52
|
+
hash_store :address, hash: ->(ins){ ins.address }
|
53
|
+
def address
|
54
|
+
"Tokyo, Japan"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
class ThisIsErrro4
|
58
|
+
include HashStore
|
59
|
+
hash_store :address, key: ->(ins){ "error:#{ins.id}" }
|
60
|
+
def id
|
61
|
+
1
|
62
|
+
end
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,226 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_store
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- M.Iwasaki(Curi)
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: redis
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: multi_json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: guard-rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: mock_redis
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
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
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: factory_girl
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: sqlite3
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - '>='
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - '>='
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: activerecord
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - '>='
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - '>='
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
description: |2
|
168
|
+
HashStore store RubyHash into Redis as JSON.
|
169
|
+
Automatically add redis commands(GET,SET,DEL,EXITS) methods to your class.
|
170
|
+
HashStore was designed to work with ActiveRecord, but also work with Non-ActiveRecord Class.
|
171
|
+
email:
|
172
|
+
- curi1119@gmail.com
|
173
|
+
executables: []
|
174
|
+
extensions: []
|
175
|
+
extra_rdoc_files: []
|
176
|
+
files:
|
177
|
+
- .gitignore
|
178
|
+
- .rspec
|
179
|
+
- Gemfile
|
180
|
+
- Guardfile
|
181
|
+
- LICENSE.txt
|
182
|
+
- README.md
|
183
|
+
- Rakefile
|
184
|
+
- hash_store.gemspec
|
185
|
+
- lib/hash_store.rb
|
186
|
+
- lib/hash_store/base.rb
|
187
|
+
- lib/hash_store/version.rb
|
188
|
+
- spec/db/database.yml
|
189
|
+
- spec/db/schema.rb
|
190
|
+
- spec/factories/models.rb
|
191
|
+
- spec/lib/hash_store/base_spec.rb
|
192
|
+
- spec/lib/hash_store_spec.rb
|
193
|
+
- spec/spec_helper.rb
|
194
|
+
- spec/support/models.rb
|
195
|
+
homepage: https://github.com/curi1119/hash_store
|
196
|
+
licenses:
|
197
|
+
- MIT
|
198
|
+
metadata: {}
|
199
|
+
post_install_message:
|
200
|
+
rdoc_options: []
|
201
|
+
require_paths:
|
202
|
+
- lib
|
203
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
204
|
+
requirements:
|
205
|
+
- - '>='
|
206
|
+
- !ruby/object:Gem::Version
|
207
|
+
version: '0'
|
208
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
209
|
+
requirements:
|
210
|
+
- - '>='
|
211
|
+
- !ruby/object:Gem::Version
|
212
|
+
version: '0'
|
213
|
+
requirements: []
|
214
|
+
rubyforge_project:
|
215
|
+
rubygems_version: 2.0.3
|
216
|
+
signing_key:
|
217
|
+
specification_version: 4
|
218
|
+
summary: HashStore store RubyHash into Redis as JSON.
|
219
|
+
test_files:
|
220
|
+
- spec/db/database.yml
|
221
|
+
- spec/db/schema.rb
|
222
|
+
- spec/factories/models.rb
|
223
|
+
- spec/lib/hash_store/base_spec.rb
|
224
|
+
- spec/lib/hash_store_spec.rb
|
225
|
+
- spec/spec_helper.rb
|
226
|
+
- spec/support/models.rb
|