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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ac95003b61d541bf721bc6bb6c118de0f56a0ea7
4
- data.tar.gz: bc63c6e292764a7d95efed92e2d5d4e2d3c4d8ae
3
+ metadata.gz: d20653833590acd7440f21df77f05a4534c37989
4
+ data.tar.gz: bb346d67ded438d67c61977136aa8f2ab85dbcf4
5
5
  SHA512:
6
- metadata.gz: 256273408ebee938bd6d4714b2ab9597af758c22b389b9ffcaa9d01aa1d08d239deca0b35f0bd38b57573fc24ad8e36521f26e1cd2ada5dbcc16e37cd4de6e32
7
- data.tar.gz: 7e3fa18a5b7c7f9bc24902cdee9cfa8ab487c6696bd00e678888f6aad2d130de27c74d086c3c4c608932269b59e1c6c7429e030b66d9d738f6845501a41ae802
6
+ metadata.gz: 396256c0ff8ffb3aae139e0087a251abfea1bb796667894ccc2c056e1ea170de4d02e8551f0358912d328200be4bad3f5ed22b1e2c6284b46f34879a3023b5a0
7
+ data.tar.gz: a83cec0b8eb9fb016e76b251747a13b72717d0c4f13856366900f9d37fd26b46bb1d5ce75d0b3f8eb62079feb71b41e494ebd357b81501693be3ed552dfaaba6
data/.gitignore CHANGED
@@ -3,6 +3,7 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
+ .ruby-version
6
7
  Gemfile.lock
7
8
  InstalledFiles
8
9
  _yardoc
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
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
- Automatically add redis commands(GET,SET,DEL,EXITS) methods to your class.
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
- User instance will gain theses methods:
49
-
50
- #### hash_store
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
- #### hash_store :address
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
- #### hash_store :for_name
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
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new
4
+
5
+ task default: :spec
6
+ task test: :spec
@@ -36,4 +36,5 @@ Gem::Specification.new do |spec|
36
36
  spec.add_development_dependency 'factory_girl'
37
37
  spec.add_development_dependency 'sqlite3'
38
38
  spec.add_development_dependency 'activerecord'
39
+ spec.add_development_dependency 'coveralls'
39
40
  end
@@ -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
- json = hs_redis.get(hs_key_proc.call(self))
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
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module HashStore
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
@@ -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
@@ -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.1
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