ssdb-attr 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +22 -0
- data/Gemfile.lock +2 -2
- data/README.md +8 -0
- data/lib/ssdb-attr/version.rb +1 -1
- data/lib/ssdb/attr.rb +30 -7
- data/spec/ssdb/attr_spec.rb +26 -7
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42862ff9c47b1a76de97dad8d083f9f600f59899
|
4
|
+
data.tar.gz: 29d3a85d0b87e99d2b392d571a705c8c0bf31b9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 015184c70c5f94521c89f9e25f7a6d3c1d4c695b96ffbc70d239b8a706ccd87b968cf7f0240af359066eed9c86983fae5d5c3db20c7f6a0b8d86e605cf3bea65
|
7
|
+
data.tar.gz: bcf2ca89d68a6fe73ff897b8300e825ac2de3973c08a85a270e64a89ea8d22239304167004ce0398a3471d6a014df15bc3f96056641cf4abccc97a113fcd73e2
|
data/.travis.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
language: ruby
|
2
|
+
|
3
|
+
rvm:
|
4
|
+
- 2.3.0
|
5
|
+
|
6
|
+
before_install:
|
7
|
+
- git clone -b stable-1.9.2 git://github.com/ideawu/ssdb.git
|
8
|
+
- cd ssdb
|
9
|
+
- make
|
10
|
+
- cd ..
|
11
|
+
|
12
|
+
before_script:
|
13
|
+
- ./ssdb/ssdb-server -d ./ssdb/ssdb.conf
|
14
|
+
|
15
|
+
after_script:
|
16
|
+
- killall -w ssdb-server
|
17
|
+
|
18
|
+
script:
|
19
|
+
- bundle exec rake
|
20
|
+
|
21
|
+
notifications:
|
22
|
+
email: false
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
1
|
# SSDB Attr
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/jianshucom/ssdb-attr.svg?branch=master)](https://travis-ci.org/jianshucom/ssdb-attr)
|
4
|
+
|
3
5
|
This gem provides an intuitive interface to define attributes on your ActiveModel class and save them in SSDB server.
|
6
|
+
|
7
|
+
# Changelog
|
8
|
+
|
9
|
+
### 0.1.3
|
10
|
+
|
11
|
+
- Add `load_ssdb_attrs` to get multiple values from SSDB once to avoid multiple SSDB calls.
|
data/lib/ssdb-attr/version.rb
CHANGED
data/lib/ssdb/attr.rb
CHANGED
@@ -3,14 +3,14 @@ module SSDB
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
instance_variable_set(:@
|
6
|
+
instance_variable_set(:@ssdb_attr_definition, {})
|
7
7
|
|
8
8
|
after_commit :save_ssdb_attrs, on: %i(create update)
|
9
9
|
after_commit :clear_ssdb_attrs, on: :destroy
|
10
10
|
end
|
11
11
|
|
12
12
|
module ClassMethods
|
13
|
-
attr_reader :
|
13
|
+
attr_reader :ssdb_attr_definition
|
14
14
|
attr_reader :ssdb_attr_id_field
|
15
15
|
attr_reader :ssdb_attr_pool_name
|
16
16
|
|
@@ -37,6 +37,10 @@ module SSDB
|
|
37
37
|
@ssdb_attr_pool_name = pool_name
|
38
38
|
end
|
39
39
|
|
40
|
+
def ssdb_attr_names
|
41
|
+
@ssdb_attr_definition.keys
|
42
|
+
end
|
43
|
+
|
40
44
|
#
|
41
45
|
# Method to define a SSDB attribute in a Ruby Class
|
42
46
|
#
|
@@ -51,14 +55,13 @@ module SSDB
|
|
51
55
|
raise "Type not supported, only `:string` and `:integer` are supported now."
|
52
56
|
end
|
53
57
|
|
54
|
-
@
|
58
|
+
@ssdb_attr_definition[name.to_s] = type.to_s
|
55
59
|
|
56
60
|
define_method(name) do
|
57
61
|
instance_variable_get("@#{name}") || begin
|
58
62
|
val = ssdb_attr_pool.with { |conn| conn.get(ssdb_attr_key(name)) } || options[:default]
|
59
|
-
instance_variable_set("@#{name}", val)
|
63
|
+
instance_variable_set("@#{name}", typecaster(val, type))
|
60
64
|
end
|
61
|
-
typecaster(instance_variable_get("@#{name}"), type)
|
62
65
|
end
|
63
66
|
|
64
67
|
define_method("#{name}=") do |val|
|
@@ -91,6 +94,25 @@ module SSDB
|
|
91
94
|
end
|
92
95
|
end
|
93
96
|
|
97
|
+
#
|
98
|
+
# Load the values of all specified attrs.
|
99
|
+
#
|
100
|
+
#
|
101
|
+
# @return [void]
|
102
|
+
#
|
103
|
+
def load_ssdb_attrs(*fields)
|
104
|
+
fields = (fields.map(&:to_s) & self.class.ssdb_attr_names)
|
105
|
+
|
106
|
+
values = ssdb_attr_pool.with do |conn|
|
107
|
+
conn.mget(fields.map { |name| ssdb_attr_key(name) })
|
108
|
+
end
|
109
|
+
|
110
|
+
fields.each_with_index do |attr, index|
|
111
|
+
value = typecaster(values[index], self.class.ssdb_attr_definition[attr])
|
112
|
+
instance_variable_set("@#{attr}", value)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
94
116
|
private
|
95
117
|
|
96
118
|
#
|
@@ -173,10 +195,11 @@ module SSDB
|
|
173
195
|
# @return [void]
|
174
196
|
#
|
175
197
|
def reload_ssdb_attrs
|
176
|
-
|
198
|
+
keys = self.class.ssdb_attr_names.map { |name| ssdb_attr_key(name) }
|
199
|
+
values = ssdb_attr_pool.with { |conn| conn.mget(keys) }
|
177
200
|
|
178
201
|
self.class.ssdb_attr_names.each_with_index do |attr, index|
|
179
|
-
instance_variable_set("@#{attr}", values[index])
|
202
|
+
instance_variable_set("@#{attr}", typecaster(values[index], self.class.ssdb_attr_definition[attr]))
|
180
203
|
end
|
181
204
|
end
|
182
205
|
end
|
data/spec/ssdb/attr_spec.rb
CHANGED
@@ -54,11 +54,18 @@ describe SSDB::Attr do
|
|
54
54
|
let(:post) { Post.create(updated_at: 1.day.ago, saved_at: 1.day.ago, changed_at: 1.day.ago) }
|
55
55
|
let(:redis) { Redis.new(:url => 'redis://localhost:8888') }
|
56
56
|
|
57
|
-
describe "@
|
58
|
-
it "should set `@
|
59
|
-
|
60
|
-
|
61
|
-
|
57
|
+
describe "@ssdb_attr_definition" do
|
58
|
+
it "should set `@ssdb_attr_definition` correctly" do
|
59
|
+
ssdb_attr_definition = Post.instance_variable_get(:@ssdb_attr_definition)
|
60
|
+
|
61
|
+
expect(ssdb_attr_definition["name"]).to eq("string")
|
62
|
+
expect(ssdb_attr_definition["int_version"]).to eq("integer")
|
63
|
+
expect(ssdb_attr_definition["default_title"]).to eq("string")
|
64
|
+
expect(ssdb_attr_definition["title"]).to eq("string")
|
65
|
+
expect(ssdb_attr_definition["content"]).to eq("string")
|
66
|
+
expect(ssdb_attr_definition["version"]).to eq("integer")
|
67
|
+
expect(ssdb_attr_definition["default_version"]).to eq("integer")
|
68
|
+
expect(ssdb_attr_definition["field_with_validation"]).to eq("string")
|
62
69
|
end
|
63
70
|
end
|
64
71
|
|
@@ -101,8 +108,8 @@ describe SSDB::Attr do
|
|
101
108
|
post.version = 3
|
102
109
|
|
103
110
|
post.send(:reload_ssdb_attrs)
|
104
|
-
expect(post.title).to eq("foobar")
|
105
|
-
expect(post.version).to eq(4)
|
111
|
+
expect(post.instance_variable_get(:@title)).to eq("foobar")
|
112
|
+
expect(post.instance_variable_get(:@version)).to eq(4)
|
106
113
|
end
|
107
114
|
end
|
108
115
|
|
@@ -125,6 +132,18 @@ describe SSDB::Attr do
|
|
125
132
|
end
|
126
133
|
end
|
127
134
|
|
135
|
+
describe "#load_ssdb_attrs" do
|
136
|
+
it "loads the values of all specified attrs" do
|
137
|
+
post = Post.create(title: "foobar", version: 4)
|
138
|
+
post = Post.find(post.id)
|
139
|
+
expect(post.instance_variable_get(:@title)).to be_nil
|
140
|
+
|
141
|
+
post.load_ssdb_attrs(:title, :version)
|
142
|
+
expect(post.instance_variable_get(:@title)).to eq("foobar")
|
143
|
+
expect(post.instance_variable_get(:@version)).to eq(4)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
128
147
|
describe "#ssdb_attr_key" do
|
129
148
|
it "should return correct key" do
|
130
149
|
expect(post.send(:ssdb_attr_key, "name")).to eq("posts:#{post.id}:name")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ssdb-attr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Larry Zhao
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-07-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -161,6 +161,7 @@ files:
|
|
161
161
|
- ".rspec"
|
162
162
|
- ".ruby-gemset"
|
163
163
|
- ".ruby-version"
|
164
|
+
- ".travis.yml"
|
164
165
|
- Gemfile
|
165
166
|
- Gemfile.lock
|
166
167
|
- README.md
|
@@ -192,7 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
192
193
|
version: '0'
|
193
194
|
requirements: []
|
194
195
|
rubyforge_project:
|
195
|
-
rubygems_version: 2.6.
|
196
|
+
rubygems_version: 2.6.11
|
196
197
|
signing_key:
|
197
198
|
specification_version: 4
|
198
199
|
summary: Provide simple helpers on persist values in redis for performance. Works
|