ssdb-attr 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +15 -24
- data/lib/ssdb-attr/version.rb +1 -1
- data/lib/ssdb/attr.rb +69 -11
- data/spec/spec_helper.rb +7 -2
- data/spec/ssdb/attr_spec.rb +58 -21
- data/ssdb-attr.gemspec +2 -3
- metadata +5 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dedea1c81f2baa61f35d107b89e9e88494c96f40
|
4
|
+
data.tar.gz: 8777a84c9a061a041a93deb7e5dc2dce4a742836
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6c1f09802b2a1a9efe05f8efc51f853806a554240740baf7f4a33628245889936e5afe485b4879201baed7fe65cc2f5fc1b2dc57112c633918bc7958da31831
|
7
|
+
data.tar.gz: 9f1c8f50009b59d7727531dc0c354e259f825df0967ad68f265b39c3d5d32da2f351eed6e719148bd48006b195ad6148fa3c3a48318c6cf4d7966d47560d3b06
|
data/Gemfile.lock
CHANGED
@@ -1,46 +1,38 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ssdb-attr (0.0.
|
5
|
-
activesupport (~> 4.1
|
4
|
+
ssdb-attr (0.0.3)
|
5
|
+
activesupport (~> 4.2.1)
|
6
6
|
connection_pool (~> 2.1.0)
|
7
7
|
redis (~> 3.1.0)
|
8
8
|
|
9
9
|
GEM
|
10
10
|
remote: http://ruby.taobao.org/
|
11
11
|
specs:
|
12
|
-
activemodel (4.1
|
13
|
-
activesupport (= 4.1
|
12
|
+
activemodel (4.2.1)
|
13
|
+
activesupport (= 4.2.1)
|
14
14
|
builder (~> 3.1)
|
15
|
-
activerecord (4.1
|
16
|
-
activemodel (= 4.1
|
17
|
-
activesupport (= 4.1
|
18
|
-
arel (~>
|
15
|
+
activerecord (4.2.1)
|
16
|
+
activemodel (= 4.2.1)
|
17
|
+
activesupport (= 4.2.1)
|
18
|
+
arel (~> 6.0)
|
19
19
|
activerecord-nulldb-adapter (0.3.1)
|
20
20
|
activerecord (>= 2.0.0)
|
21
|
-
activesupport (4.1
|
22
|
-
i18n (~> 0.
|
21
|
+
activesupport (4.2.1)
|
22
|
+
i18n (~> 0.7)
|
23
23
|
json (~> 1.7, >= 1.7.7)
|
24
24
|
minitest (~> 5.1)
|
25
|
-
thread_safe (~> 0.
|
25
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
26
26
|
tzinfo (~> 1.1)
|
27
|
-
arel (
|
27
|
+
arel (6.0.0)
|
28
28
|
builder (3.2.2)
|
29
|
-
coderay (1.1.0)
|
30
29
|
connection_pool (2.1.3)
|
31
30
|
i18n (0.7.0)
|
32
31
|
json (1.8.2)
|
33
|
-
method_source (0.8.2)
|
34
32
|
minitest (5.5.1)
|
35
|
-
pry (0.9.12.6)
|
36
|
-
coderay (~> 1.0)
|
37
|
-
method_source (~> 0.8)
|
38
|
-
slop (~> 3.4)
|
39
|
-
pry-nav (0.2.3)
|
40
|
-
pry (~> 0.9.10)
|
41
33
|
rake (10.4.2)
|
42
34
|
redis (3.1.0)
|
43
|
-
|
35
|
+
sqlite3 (1.3.10)
|
44
36
|
thread_safe (0.3.5)
|
45
37
|
tzinfo (1.2.2)
|
46
38
|
thread_safe (~> 0.1)
|
@@ -49,11 +41,10 @@ PLATFORMS
|
|
49
41
|
ruby
|
50
42
|
|
51
43
|
DEPENDENCIES
|
52
|
-
activerecord (~> 4.1
|
44
|
+
activerecord (~> 4.2.1)
|
53
45
|
activerecord-nulldb-adapter
|
54
46
|
bundler (~> 1.5)
|
55
47
|
minitest (~> 5.5.1)
|
56
|
-
pry
|
57
|
-
pry-nav
|
58
48
|
rake
|
49
|
+
sqlite3
|
59
50
|
ssdb-attr!
|
data/lib/ssdb-attr/version.rb
CHANGED
data/lib/ssdb/attr.rb
CHANGED
@@ -2,11 +2,53 @@ module SSDB
|
|
2
2
|
module Attr
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
+
included do
|
6
|
+
# puts 'called'
|
7
|
+
define_model_callbacks :update_ssdb_attrs, only: [:before, :after]
|
8
|
+
end
|
9
|
+
|
5
10
|
def to_ssdb_attr_key(name)
|
6
11
|
"#{self.class.to_s.underscore.pluralize}:#{self.id}:#{name}"
|
7
12
|
end
|
8
13
|
|
14
|
+
def update_ssdb_attrs(data={})
|
15
|
+
|
16
|
+
# Determine what attrs are requested to be updated
|
17
|
+
attr_names = data.keys & self.class.ssdb_attr_names
|
18
|
+
if attr_names.empty?
|
19
|
+
return
|
20
|
+
end
|
21
|
+
|
22
|
+
# Determine dirty fields
|
23
|
+
attr_names.each do |name|
|
24
|
+
if self.send(name.to_sym) != data[name].to_s
|
25
|
+
attribute_will_change! name.to_sym
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# trigger_ssdb_attr_before_callbacks if self.respond_to? :trigger_ssdb_attr_before_callbacks
|
30
|
+
run_callbacks :update_ssdb_attrs do
|
31
|
+
SSDBAttr.pool.with do |conn|
|
32
|
+
attr_names.each do |name|
|
33
|
+
conn.set("#{self.to_ssdb_attr_key(name)}", data[name])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# trigger_ssdb_attr_after_callbacks if self.respond_to? :trigger_ssdb_attr_after_callbacks
|
39
|
+
|
40
|
+
# Clear dirty fields
|
41
|
+
attr_names.each do |name|
|
42
|
+
clear_attribute_changes name.to_sym
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
9
46
|
module ClassMethods
|
47
|
+
def ssdb_attr_names
|
48
|
+
@ssdb_attr_names = [] if @ssdb_attr_names.nil?
|
49
|
+
@ssdb_attr_names
|
50
|
+
end
|
51
|
+
|
10
52
|
# ssdb_attr :content, :string, default: 0, touch: true
|
11
53
|
# ssdb_attr :writer_version, :integer, default: 0, touch: [:field1, :field2, :field3]
|
12
54
|
#
|
@@ -23,6 +65,9 @@ module SSDB
|
|
23
65
|
raise "Type not supported, only `:string` and `:integer` are supported now."
|
24
66
|
end
|
25
67
|
|
68
|
+
self.ssdb_attr_names ||= []
|
69
|
+
self.ssdb_attr_names << name
|
70
|
+
|
26
71
|
define_method(name) do
|
27
72
|
conversion = type == :string ? :to_s : :to_i
|
28
73
|
|
@@ -36,28 +81,41 @@ module SSDB
|
|
36
81
|
define_method("#{name}=") do |v|
|
37
82
|
SSDBAttr.pool.with do |conn|
|
38
83
|
conn.set("#{self.to_ssdb_attr_key(name)}", v)
|
39
|
-
self.send("touch_ssdb_attr_#{name}".to_sym) if self.respond_to?("touch_ssdb_attr_#{name}".to_sym)
|
40
84
|
end
|
85
|
+
|
86
|
+
self.send("touch_ssdb_attr_#{name}".to_sym) if self.respond_to?("touch_ssdb_attr_#{name}".to_sym)
|
41
87
|
end
|
42
88
|
|
43
89
|
if options[:touch].present?
|
44
90
|
define_method("touch_ssdb_attr_#{name}") do
|
45
|
-
t = Time.now
|
46
|
-
|
47
91
|
if options[:touch].kind_of?(Array)
|
48
|
-
|
49
|
-
|
50
|
-
options[:touch].each do |field|
|
51
|
-
data[field.to_sym] = t if ActiveRecord::Base.connection.column_exists?(self.class.table_name, field.to_sym, :datetime)
|
52
|
-
end
|
53
|
-
|
54
|
-
self.update(data) if !data.empty?
|
92
|
+
touch(*options[:touch])
|
55
93
|
else
|
56
|
-
|
94
|
+
touch(:updated_at)
|
57
95
|
end
|
58
96
|
end
|
59
97
|
end
|
98
|
+
|
99
|
+
define_method("#{name}_changed?") do
|
100
|
+
attribute_changed?(name.to_sym)
|
101
|
+
end
|
60
102
|
end
|
103
|
+
|
104
|
+
# def after_update_ssdb_attrs(*args)
|
105
|
+
# define_method("trigger_ssdb_attr_after_callbacks") do
|
106
|
+
# args.each do |arg|
|
107
|
+
# self.send(arg.to_sym)
|
108
|
+
# end
|
109
|
+
# end
|
110
|
+
# end
|
111
|
+
#
|
112
|
+
# def before_update_ssdb_attrs(*args)
|
113
|
+
# define_method("trigger_ssdb_attr_before_callbacks") do
|
114
|
+
# args.each do |arg|
|
115
|
+
# self.send(arg.to_sym)
|
116
|
+
# end
|
117
|
+
# end
|
118
|
+
# end
|
61
119
|
end
|
62
120
|
end
|
63
121
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -6,6 +6,11 @@ require 'minitest/autorun'
|
|
6
6
|
require 'minitest/mock'
|
7
7
|
require 'minitest/pride'
|
8
8
|
|
9
|
-
SSDBAttr.setup url: '
|
9
|
+
SSDBAttr.setup url: 'redis://localhost:6379/15'
|
10
10
|
|
11
|
-
|
11
|
+
# Setup activerecord
|
12
|
+
ActiveRecord::Base.raise_in_transactional_callbacks = true if ActiveRecord::VERSION::STRING >= '4.2'
|
13
|
+
|
14
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', database: ':memory:'
|
15
|
+
|
16
|
+
ActiveRecord::Base.connection.execute "CREATE TABLE posts (id INTEGER NOT NULL PRIMARY KEY, updated_at DATETIME, content_updated_at DATETIME, title_updated_at DATETIME)"
|
data/spec/ssdb/attr_spec.rb
CHANGED
@@ -1,49 +1,86 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
class
|
3
|
+
class Post < ActiveRecord::Base
|
4
4
|
include SSDB::Attr
|
5
5
|
|
6
|
-
|
6
|
+
ssdb_attr :title, :string, touch: [:content_updated_at]
|
7
|
+
ssdb_attr :content, :string, touch: true
|
8
|
+
ssdb_attr :version, :integer, default: 22
|
9
|
+
ssdb_attr :counter, :integer, default: 4
|
7
10
|
|
8
|
-
|
9
|
-
|
10
|
-
ssdb_attr :content, :string, touch: true
|
11
|
+
before_update_ssdb_attrs :before_callback1, :before_callback2
|
12
|
+
after_update_ssdb_attrs :after_callback1, :after_callback2
|
11
13
|
|
12
|
-
def
|
13
|
-
|
14
|
+
def before_callback1
|
15
|
+
puts "before callback 1"
|
16
|
+
|
17
|
+
if title_changed?
|
18
|
+
puts "title changed detected."
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def before_callback2
|
23
|
+
puts "before callback 2"
|
14
24
|
end
|
15
25
|
|
16
|
-
def
|
17
|
-
|
26
|
+
def after_callback1
|
27
|
+
puts "after callback 1"
|
18
28
|
end
|
29
|
+
|
30
|
+
def after_callback2
|
31
|
+
puts "after callback 2"
|
32
|
+
end
|
33
|
+
|
34
|
+
# touch_ssdb_attr [:title, :content], touch: [:content_at]
|
35
|
+
#
|
36
|
+
# dummy.update_ssdb_attrs(title: 'abc', content: 'cbd')
|
37
|
+
#
|
38
|
+
# before_update_ssdb_attributes args*
|
39
|
+
# after_update_ssdb_attributes :touch_updated_at
|
19
40
|
end
|
20
41
|
|
21
42
|
describe SSDB::Attr do
|
22
|
-
describe "method definition" do
|
23
43
|
|
44
|
+
describe "method created" do
|
24
45
|
end
|
25
46
|
|
26
|
-
describe "
|
27
|
-
|
28
|
-
|
47
|
+
describe "#update_ssdb_attrs" do
|
48
|
+
if 'should correctly set values'
|
49
|
+
post = Post.create
|
50
|
+
|
51
|
+
post.update_ssdb_attrs(title: 'bar2')
|
52
|
+
post.update_ssdb_attrs(title: 'bar2')
|
29
53
|
|
30
|
-
|
54
|
+
# title = 'foo'
|
55
|
+
# content = 'bar'
|
56
|
+
#
|
57
|
+
# title.must_equal 'foo'
|
58
|
+
# content.must_equal 'bar'
|
59
|
+
#
|
60
|
+
#post.title.must_equal 'foo'
|
61
|
+
#post.content.must_equal 'bar'
|
31
62
|
end
|
32
63
|
end
|
33
64
|
|
34
|
-
describe "
|
65
|
+
describe "#ssdb_attr" do
|
66
|
+
it 'should set default value correctly' do
|
67
|
+
post = Post.create
|
68
|
+
|
69
|
+
post.counter.must_equal 4
|
70
|
+
end
|
71
|
+
|
35
72
|
it "should convert to string correctly" do
|
36
|
-
|
73
|
+
post = Post.create
|
37
74
|
|
38
|
-
|
39
|
-
|
75
|
+
post.title = 120
|
76
|
+
post.title.must_equal '120'
|
40
77
|
end
|
41
78
|
|
42
79
|
it "should convert to integer correctly" do
|
43
|
-
|
80
|
+
post = Post.create
|
44
81
|
|
45
|
-
|
46
|
-
|
82
|
+
post.version = '120'
|
83
|
+
post.version.must_equal 120
|
47
84
|
end
|
48
85
|
end
|
49
86
|
end
|
data/ssdb-attr.gemspec
CHANGED
@@ -24,9 +24,8 @@ Gem::Specification.new do |spec|
|
|
24
24
|
|
25
25
|
spec.add_development_dependency "bundler", "~> 1.5"
|
26
26
|
spec.add_development_dependency "rake"
|
27
|
-
spec.add_development_dependency "
|
28
|
-
spec.add_development_dependency "pry-nav"
|
27
|
+
spec.add_development_dependency "sqlite3"
|
29
28
|
spec.add_development_dependency "minitest", "~> 5.5.1"
|
30
|
-
spec.add_development_dependency "activerecord", "~> 4.1
|
29
|
+
spec.add_development_dependency "activerecord", "~> 4.2.1"
|
31
30
|
spec.add_development_dependency "activerecord-nulldb-adapter"
|
32
31
|
end
|
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.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Larry Zhao
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -81,21 +81,7 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
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: pry-nav
|
84
|
+
name: sqlite3
|
99
85
|
requirement: !ruby/object:Gem::Requirement
|
100
86
|
requirements:
|
101
87
|
- - ">="
|
@@ -128,14 +114,14 @@ dependencies:
|
|
128
114
|
requirements:
|
129
115
|
- - "~>"
|
130
116
|
- !ruby/object:Gem::Version
|
131
|
-
version: 4.1
|
117
|
+
version: 4.2.1
|
132
118
|
type: :development
|
133
119
|
prerelease: false
|
134
120
|
version_requirements: !ruby/object:Gem::Requirement
|
135
121
|
requirements:
|
136
122
|
- - "~>"
|
137
123
|
- !ruby/object:Gem::Version
|
138
|
-
version: 4.1
|
124
|
+
version: 4.2.1
|
139
125
|
- !ruby/object:Gem::Dependency
|
140
126
|
name: activerecord-nulldb-adapter
|
141
127
|
requirement: !ruby/object:Gem::Requirement
|