ssdb-attr 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +2 -2
- data/lib/ssdb-attr/version.rb +1 -1
- data/lib/ssdb/attr.rb +46 -64
- data/test/ssdb_attr_test.rb +145 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12d34fe49dea6f34f53f02d705685b0b378624b3
|
4
|
+
data.tar.gz: 971ad112430c9f856ec117b8492f39f0384ac504
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4403f533f66cae6458d5ee9c8c6b8cc5146efb4eb07587e893f70ad345d2b6a0f7ca7252b4b58e604c38eb8156f636c995485001b0b9ac830fa1d8493e4a0e0a
|
7
|
+
data.tar.gz: 80e4d601f98c269d45b2438788544b6e79927f0e5ca604162aabf55789245af11db31a997a294dc02b79ca9b5c257ce33c364eb7274c25fffe1c3f894f06fa39
|
data/Rakefile
CHANGED
data/lib/ssdb-attr/version.rb
CHANGED
data/lib/ssdb/attr.rb
CHANGED
@@ -3,52 +3,65 @@ module SSDB
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
# puts 'called'
|
7
6
|
define_model_callbacks :update_ssdb_attrs, only: [:before, :after]
|
7
|
+
after_create :init_ssdb_attrs
|
8
|
+
after_destroy :clear_ssdb_attrs
|
8
9
|
end
|
9
10
|
|
10
|
-
def
|
11
|
-
"#{self.class.to_s.underscore.pluralize}:#{self.id}:#{name}"
|
12
|
-
end
|
13
|
-
|
14
|
-
def update_ssdb_attrs(data={})
|
15
|
-
|
11
|
+
def update_ssdb_attrs(attributes)
|
16
12
|
# Determine what attrs are requested to be updated
|
17
|
-
|
18
|
-
|
19
|
-
return
|
20
|
-
end
|
13
|
+
attributes = attributes.symbolize_keys
|
14
|
+
attr_names = attributes.keys & self.class.ssdb_attr_names
|
21
15
|
|
22
16
|
# Determine dirty fields
|
23
17
|
attr_names.each do |name|
|
24
|
-
|
25
|
-
attribute_will_change! name.to_sym
|
26
|
-
end
|
18
|
+
send("#{name}_will_change!") unless attributes[name] == send(name)
|
27
19
|
end
|
28
20
|
|
29
|
-
# trigger_ssdb_attr_before_callbacks if self.respond_to? :trigger_ssdb_attr_before_callbacks
|
30
21
|
run_callbacks :update_ssdb_attrs do
|
31
22
|
SSDBAttr.pool.with do |conn|
|
32
|
-
attr_names.each
|
33
|
-
conn.set("#{self.to_ssdb_attr_key(name)}", data[name])
|
34
|
-
end
|
23
|
+
attr_names.each { |name| send("#{name}=", attributes[name]) }
|
35
24
|
end
|
36
25
|
end
|
37
26
|
|
38
|
-
# trigger_ssdb_attr_after_callbacks if self.respond_to? :trigger_ssdb_attr_after_callbacks
|
39
|
-
|
40
27
|
# Clear dirty fields
|
41
|
-
attr_names
|
42
|
-
|
28
|
+
clear_attribute_changes(attr_names)
|
29
|
+
|
30
|
+
true # always return true
|
31
|
+
end
|
32
|
+
|
33
|
+
def init_ssdb_attrs
|
34
|
+
self.class.ssdb_attr_names.each do |attribute|
|
35
|
+
SSDBAttr.pool.with { |conn| conn.set(to_ssdb_attr_key(attribute), self.send(attribute)) }
|
43
36
|
end
|
44
37
|
end
|
45
38
|
|
39
|
+
def clear_ssdb_attrs
|
40
|
+
self.class.ssdb_attr_names.each do |attribute|
|
41
|
+
SSDBAttr.pool.with { |conn| conn.del(to_ssdb_attr_key(attribute)) }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_ssdb_attr_key(name)
|
46
|
+
self.class.to_ssdb_attr_key(name, id)
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def touch_db_column(names)
|
52
|
+
names == true ? touch : touch(*names)
|
53
|
+
end
|
54
|
+
|
46
55
|
module ClassMethods
|
47
56
|
def ssdb_attr_names
|
48
|
-
@ssdb_attr_names
|
49
|
-
|
57
|
+
@ssdb_attr_names ||= []
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_ssdb_attr_key(name, id)
|
61
|
+
"#{self.name.tableize}:#{id}:#{name}"
|
50
62
|
end
|
51
63
|
|
64
|
+
|
52
65
|
# ssdb_attr :content, :string, default: 0, touch: true
|
53
66
|
# ssdb_attr :writer_version, :integer, default: 0, touch: [:field1, :field2, :field3]
|
54
67
|
#
|
@@ -60,62 +73,31 @@ module SSDB
|
|
60
73
|
#
|
61
74
|
# @return [type] [description]
|
62
75
|
def ssdb_attr(name, type, options={})
|
63
|
-
|
64
|
-
if ![:string, :integer].include?(type)
|
76
|
+
unless %i(string integer).include?(type)
|
65
77
|
raise "Type not supported, only `:string` and `:integer` are supported now."
|
66
78
|
end
|
67
79
|
|
68
|
-
self.ssdb_attr_names ||= []
|
69
80
|
self.ssdb_attr_names << name
|
70
81
|
|
71
82
|
define_method(name) do
|
72
83
|
conversion = type == :string ? :to_s : :to_i
|
73
|
-
|
74
|
-
value
|
75
|
-
conn.get("#{self.to_ssdb_attr_key(name)}")
|
76
|
-
end
|
77
|
-
|
78
|
-
value.try(conversion) || options[:default]
|
84
|
+
value = SSDBAttr.pool.with { |conn| conn.get("#{to_ssdb_attr_key(name)}") }
|
85
|
+
(value || options[:default]).send(conversion)
|
79
86
|
end
|
80
87
|
|
81
|
-
define_method("#{name}=") do |
|
82
|
-
SSDBAttr.pool.with
|
83
|
-
|
84
|
-
end
|
85
|
-
|
86
|
-
self.send("touch_ssdb_attr_#{name}".to_sym) if self.respond_to?("touch_ssdb_attr_#{name}".to_sym)
|
88
|
+
define_method("#{name}=") do |val|
|
89
|
+
SSDBAttr.pool.with { |conn| conn.set("#{to_ssdb_attr_key(name)}", val) }
|
90
|
+
touch_db_column(options[:touch]) if options[:touch].present?
|
87
91
|
end
|
88
92
|
|
89
|
-
|
90
|
-
|
91
|
-
if options[:touch].kind_of?(Array)
|
92
|
-
touch(*options[:touch])
|
93
|
-
else
|
94
|
-
touch(:updated_at)
|
95
|
-
end
|
96
|
-
end
|
93
|
+
define_method("#{name}_will_change!") do
|
94
|
+
attribute_will_change!(name)
|
97
95
|
end
|
98
96
|
|
99
97
|
define_method("#{name}_changed?") do
|
100
|
-
attribute_changed?(name
|
98
|
+
attribute_changed?(name)
|
101
99
|
end
|
102
100
|
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
|
119
101
|
end
|
120
102
|
end
|
121
103
|
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
require 'ssdb-attr'
|
2
|
+
require 'active_record'
|
3
|
+
ActiveRecord::Base.raise_in_transactional_callbacks = true if ActiveRecord::VERSION::STRING >= '4.2'
|
4
|
+
|
5
|
+
require 'minitest/autorun'
|
6
|
+
require 'minitest/spec'
|
7
|
+
require 'minitest/pride'
|
8
|
+
test_framework = defined?(MiniTest::Test) ? MiniTest::Test : MiniTest::Unit::TestCase
|
9
|
+
|
10
|
+
require File.expand_path(File.dirname(__FILE__) + "/../lib/ssdb/attr")
|
11
|
+
|
12
|
+
def connect!
|
13
|
+
SSDBAttr.setup(url: 'redis://localhost:6379/15')
|
14
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
|
15
|
+
end
|
16
|
+
|
17
|
+
def setup!
|
18
|
+
connect!
|
19
|
+
{ 'posts' => 'updated_at DATETIME, saved_at DATETIME, changed_at DATETIME' }.each do |table_name, columns_as_sql_string|
|
20
|
+
ActiveRecord::Base.connection.execute "CREATE TABLE #{table_name} (id INTEGER NOT NULL PRIMARY KEY, #{columns_as_sql_string})"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
setup!
|
25
|
+
|
26
|
+
class Post < ActiveRecord::Base
|
27
|
+
include SSDB::Attr
|
28
|
+
|
29
|
+
ssdb_attr :name, :string
|
30
|
+
ssdb_attr :int_version, :integer
|
31
|
+
ssdb_attr :default_title, :string, default: "Untitled"
|
32
|
+
ssdb_attr :plain_touch, :string, touch: true
|
33
|
+
ssdb_attr :custom_touch_single_column, :string, touch: :saved_at
|
34
|
+
ssdb_attr :custom_touch_multiple_columns, :string, touch: [:saved_at, :changed_at]
|
35
|
+
ssdb_attr :title, :string
|
36
|
+
ssdb_attr :content, :string
|
37
|
+
ssdb_attr :version, :integer, default: 1
|
38
|
+
|
39
|
+
before_update_ssdb_attrs :before1, :before2
|
40
|
+
after_update_ssdb_attrs :after1, :after2
|
41
|
+
|
42
|
+
def callback_out
|
43
|
+
@callback_out ||= []
|
44
|
+
end
|
45
|
+
|
46
|
+
%i(before1 before2 after1 after2).each do |name|
|
47
|
+
define_method(name) do
|
48
|
+
callback_out << name
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class SsdbAttrTest < test_framework
|
54
|
+
def setup
|
55
|
+
SSDBAttr.pool.with { |conn| conn.flushdb }
|
56
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
57
|
+
ActiveRecord::Base.connection.execute "DELETE FROM #{table}"
|
58
|
+
end
|
59
|
+
@post = Post.create(updated_at: 1.day.ago, saved_at: 1.day.ago, changed_at: 1.day.ago)
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_respond_to_methods
|
63
|
+
assert_equal true, @post.respond_to?(:name)
|
64
|
+
assert_equal true, @post.respond_to?(:name=)
|
65
|
+
assert_equal true, @post.respond_to?(:name_changed?)
|
66
|
+
assert_equal true, @post.respond_to?(:name_will_change!)
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_integer_attribute
|
70
|
+
@post.int_version = "4"
|
71
|
+
assert_equal 4, @post.int_version
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_with_default_value
|
75
|
+
assert_equal "Untitled", @post.default_title
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_with_plain_touch
|
79
|
+
original_updated_at = @post.updated_at
|
80
|
+
@post.plain_touch = "something"
|
81
|
+
refute_equal original_updated_at, @post.updated_at
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_with_custom_touch_signle_column
|
85
|
+
original_saved_at = @post.saved_at
|
86
|
+
@post.custom_touch_single_column = "something"
|
87
|
+
refute_equal original_saved_at, @post.saved_at
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_with_custom_touch_multiple_columns
|
91
|
+
original_saved_at, original_changed_at = @post.saved_at, @post.changed_at
|
92
|
+
@post.custom_touch_multiple_columns = "something"
|
93
|
+
refute_equal original_saved_at, @post.saved_at
|
94
|
+
refute_equal original_changed_at, @post.changed_at
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_to_ssdb_attr_key
|
98
|
+
assert_equal "posts:#{@post.id}:name", Post.to_ssdb_attr_key("name", @post.id)
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_update_ssdb_attrs_with_symbolize_keys
|
102
|
+
@post.update_ssdb_attrs(title: "note one", content: "testing!!!", version: 1)
|
103
|
+
assert_equal "note one", @post.title
|
104
|
+
assert_equal "testing!!!", @post.content
|
105
|
+
assert_equal 1, @post.version
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_update_ssdb_attrs_with_string_keys
|
109
|
+
@post.update_ssdb_attrs("title" => "note one", "content" => "testing!!!", "version" => 1)
|
110
|
+
assert_equal "note one", @post.title
|
111
|
+
assert_equal "testing!!!", @post.content
|
112
|
+
assert_equal 1, @post.version
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_update_ssdb_attrs_on_object_return_true
|
116
|
+
assert_equal true, @post.update_ssdb_attrs(title: "note one", content: "testing!!!", version: 1)
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_update_ssdb_attrs_callbacks
|
120
|
+
@post.update_ssdb_attrs(title: "something")
|
121
|
+
assert_equal [:before1, :before2, :after1, :after2], @post.callback_out
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_object_destroy_callbacks
|
125
|
+
@post.update_ssdb_attrs(title: "note one", content: "nice job!")
|
126
|
+
ssdb_title_key = @post.to_ssdb_attr_key(:title)
|
127
|
+
ssdb_content_key = @post.to_ssdb_attr_key(:content)
|
128
|
+
assert_equal true, SSDBAttr.pool.with { |conn| conn.exists(ssdb_title_key) }
|
129
|
+
assert_equal true, SSDBAttr.pool.with { |conn| conn.exists(ssdb_content_key) }
|
130
|
+
@post.destroy
|
131
|
+
assert_equal false, SSDBAttr.pool.with { |conn| conn.exists(ssdb_title_key) }
|
132
|
+
assert_equal false, SSDBAttr.pool.with { |conn| conn.exists(ssdb_content_key) }
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_object_create_callbacks
|
136
|
+
title_key = @post.to_ssdb_attr_key(:title)
|
137
|
+
default_title_key = @post.to_ssdb_attr_key(:default_title)
|
138
|
+
version_key = @post.to_ssdb_attr_key(:version)
|
139
|
+
|
140
|
+
assert_equal 9, SSDBAttr.pool.with { |conn| conn.keys.count }
|
141
|
+
assert_equal true, SSDBAttr.pool.with { |conn| conn.exists(title_key) }
|
142
|
+
assert_equal "Untitled", SSDBAttr.pool.with { |conn| conn.get(default_title_key) }
|
143
|
+
assert_equal "1", SSDBAttr.pool.with { |conn| conn.get(version_key) }
|
144
|
+
end
|
145
|
+
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.5
|
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-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -157,6 +157,7 @@ files:
|
|
157
157
|
- spec/ssdb-attr_spec.rb
|
158
158
|
- spec/ssdb/attr_spec.rb
|
159
159
|
- ssdb-attr.gemspec
|
160
|
+
- test/ssdb_attr_test.rb
|
160
161
|
homepage: ''
|
161
162
|
licenses:
|
162
163
|
- MIT
|
@@ -186,3 +187,4 @@ test_files:
|
|
186
187
|
- spec/spec_helper.rb
|
187
188
|
- spec/ssdb-attr_spec.rb
|
188
189
|
- spec/ssdb/attr_spec.rb
|
190
|
+
- test/ssdb_attr_test.rb
|