activeobject 0.0.3
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.
- data/CHANGE +10 -0
- data/Interface_desc +21 -0
- data/MIT-LICENSE +20 -0
- data/README +72 -0
- data/Rakefile.rb +9 -0
- data/active-object.gemspec +50 -0
- data/examples/account.rb +69 -0
- data/examples/data.tch +0 -0
- data/examples/light_cloud.yml +18 -0
- data/examples/test.rb +3 -0
- data/examples/user.rb +112 -0
- data/init.rb +4 -0
- data/lib/active-object.rb +23 -0
- data/lib/active_object/adapters/light_cloud.rb +40 -0
- data/lib/active_object/adapters/tokyo_cabinet.rb +48 -0
- data/lib/active_object/adapters/tokyo_tyrant.rb +14 -0
- data/lib/active_object/associations.rb +200 -0
- data/lib/active_object/base.rb +415 -0
- data/lib/active_object/callbacks.rb +180 -0
- data/lib/active_object/observer.rb +180 -0
- data/lib/active_object/serialization.rb +99 -0
- data/lib/active_object/serializers/json_serializer.rb +75 -0
- data/lib/active_object/serializers/xml_serializer.rb +325 -0
- data/lib/active_object/validations.rb +687 -0
- data/lib/active_support/callbacks.rb +303 -0
- data/lib/active_support/core_ext/array/access.rb +53 -0
- data/lib/active_support/core_ext/array/conversions.rb +183 -0
- data/lib/active_support/core_ext/array/extract_options.rb +20 -0
- data/lib/active_support/core_ext/array/grouping.rb +106 -0
- data/lib/active_support/core_ext/array/random_access.rb +12 -0
- data/lib/active_support/core_ext/array.rb +13 -0
- data/lib/active_support/core_ext/blank.rb +58 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +54 -0
- data/lib/active_support/core_ext/class/inheritable_attributes.rb +140 -0
- data/lib/active_support/core_ext/class/removal.rb +50 -0
- data/lib/active_support/core_ext/class.rb +3 -0
- data/lib/active_support/core_ext/duplicable.rb +43 -0
- data/lib/active_support/core_ext/enumerable.rb +72 -0
- data/lib/active_support/core_ext/hash/conversions.rb +259 -0
- data/lib/active_support/core_ext/hash/keys.rb +52 -0
- data/lib/active_support/core_ext/hash.rb +8 -0
- data/lib/active_support/core_ext/module/aliasing.rb +74 -0
- data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +31 -0
- data/lib/active_support/core_ext/module/attribute_accessors.rb +58 -0
- data/lib/active_support/core_ext/module.rb +16 -0
- data/lib/active_support/core_ext/object/conversions.rb +14 -0
- data/lib/active_support/core_ext/object/extending.rb +80 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +74 -0
- data/lib/active_support/core_ext/object/metaclass.rb +13 -0
- data/lib/active_support/core_ext/object/misc.rb +43 -0
- data/lib/active_support/core_ext/object.rb +5 -0
- data/lib/active_support/core_ext/string/inflections.rb +167 -0
- data/lib/active_support/core_ext/string.rb +7 -0
- data/lib/active_support/core_ext.rb +4 -0
- data/lib/active_support/inflections.rb +55 -0
- data/lib/active_support/inflector.rb +348 -0
- data/lib/active_support/vendor/builder-2.1.2/blankslate.rb +113 -0
- data/lib/active_support/vendor/builder-2.1.2/builder/blankslate.rb +20 -0
- data/lib/active_support/vendor/builder-2.1.2/builder/css.rb +250 -0
- data/lib/active_support/vendor/builder-2.1.2/builder/xchar.rb +115 -0
- data/lib/active_support/vendor/builder-2.1.2/builder/xmlbase.rb +139 -0
- data/lib/active_support/vendor/builder-2.1.2/builder/xmlevents.rb +63 -0
- data/lib/active_support/vendor/builder-2.1.2/builder/xmlmarkup.rb +328 -0
- data/lib/active_support/vendor/builder-2.1.2/builder.rb +13 -0
- data/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb +1021 -0
- data/lib/active_support/vendor.rb +14 -0
- data/lib/active_support.rb +6 -0
- data/spec/case/association_test.rb +97 -0
- data/spec/case/base_test.rb +74 -0
- data/spec/case/callbacks_observers_test.rb +38 -0
- data/spec/case/callbacks_test.rb +424 -0
- data/spec/case/serialization_test.rb +87 -0
- data/spec/case/validations_test.rb +1482 -0
- data/spec/data.tch +0 -0
- data/spec/helper.rb +15 -0
- data/spec/light_cloud.yml +18 -0
- data/spec/model/account.rb +4 -0
- data/spec/model/topic.rb +26 -0
- data/spec/model/user.rb +8 -0
- metadata +173 -0
data/CHANGE
ADDED
data/Interface_desc
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
ActiveRecord的功能分析:
|
2
|
+
|
3
|
+
1、 CRUD --> 序列化 --> 跟踪变更
|
4
|
+
2、 验证
|
5
|
+
3、 回调 --> 观察器
|
6
|
+
4、 关联
|
7
|
+
5、 集合
|
8
|
+
6、 事务
|
9
|
+
|
10
|
+
|
11
|
+
OKM(对象-key/value映射)要完成的事项:
|
12
|
+
1、实现对象的持久化。这里面包含以下几部分:
|
13
|
+
* 对象的序列化与反序列化: 通过序列化和反序列化,使得对象能作为value被保存。
|
14
|
+
* 对象的CRUD: 通过CRUD能将序列化的对象保存、读取序列化的对象以及删除已经保存的对象。
|
15
|
+
* 对象的变更跟踪: 通过对对象的变更跟踪,在save时能确定是否需要进行保存操作。对于没有变更的对象不需要执行保存操作。
|
16
|
+
|
17
|
+
2、实现对象的验证。
|
18
|
+
|
19
|
+
2、实现对象的回调和观察器模式。
|
20
|
+
|
21
|
+
3、实现对象的关联。
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Aaron Zhang
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
= Active Object -- 基于Ruby的对象-key/value映射
|
2
|
+
|
3
|
+
Active Object是用来访问LightCloud/TokyoCabinet/TokyoTyrant的工具,实现了持久化数据与对象的映射。
|
4
|
+
|
5
|
+
== 主要特性
|
6
|
+
|
7
|
+
* 自定义持久化属性.
|
8
|
+
|
9
|
+
class User < ActiveObject::Base
|
10
|
+
attribute :name,:email
|
11
|
+
end
|
12
|
+
|
13
|
+
* 建立对象引用,消除冗余.
|
14
|
+
|
15
|
+
class User < ActiveObject::Base
|
16
|
+
attribute :name,:email
|
17
|
+
|
18
|
+
has_one :icon #=> 表示每个人有一张肖像
|
19
|
+
has_many :friends #=> 表示每个人可以有多个朋友
|
20
|
+
end
|
21
|
+
|
22
|
+
class Icon < ActiveObject::Base
|
23
|
+
attribute :content
|
24
|
+
end
|
25
|
+
|
26
|
+
class Friend < ActiveObject::Base
|
27
|
+
attribute :friend_name,:remark,:created_at
|
28
|
+
has_one :user
|
29
|
+
end
|
30
|
+
|
31
|
+
* 可以对对象应用验证规则,而且对新对象或存在的对象应用不同的验证规则.
|
32
|
+
|
33
|
+
class User < ActiveObject::Base
|
34
|
+
attribute :name,:email
|
35
|
+
|
36
|
+
validates_presence_of :name,:email
|
37
|
+
end
|
38
|
+
|
39
|
+
* 回调.
|
40
|
+
|
41
|
+
class User < ActiveObject::Base
|
42
|
+
before_destroy do |object|
|
43
|
+
puts object
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
* 观察器.
|
48
|
+
class UserObserver < ActiveObject::Observer
|
49
|
+
def after_create(user)
|
50
|
+
puts user
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
* 多级继承
|
55
|
+
class User < ActiveObject::Base;end
|
56
|
+
class Account < User;end
|
57
|
+
|
58
|
+
|
59
|
+
== 示例
|
60
|
+
|
61
|
+
在Rails中应用Active Object
|
62
|
+
|
63
|
+
在环境文件的最后加入下列代码(假设LightCloud配置文件与环境文件在同一个目录下):
|
64
|
+
|
65
|
+
require 'activeobject'
|
66
|
+
ActiveObject::Base.configure :LC,File.join(File.dirname(__FILE__),'light_cloud.yml')
|
67
|
+
|
68
|
+
在项目中使用观察器
|
69
|
+
|
70
|
+
在环境文件的最后加入下列代码:
|
71
|
+
ActiveObject::Base.observers = :account_observer
|
72
|
+
ActiveObject::Base.instantiate_observers
|
data/Rakefile.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
module DirExt
|
3
|
+
# 根据指定的目录获取该目录下所有的文件路径(不包含隐藏文件)
|
4
|
+
# 例如:
|
5
|
+
# files_path_to_s('/home/Aaron/sample') => 会列出sample目录下所有文件的路径
|
6
|
+
#
|
7
|
+
def files_path_to_s(dirname)
|
8
|
+
path_array = files_path(dirname)
|
9
|
+
path_array.flatten!
|
10
|
+
end
|
11
|
+
|
12
|
+
protected
|
13
|
+
def files_path(dirname,path_array=[])
|
14
|
+
files = Dir.entries(dirname)
|
15
|
+
files.delete('.')
|
16
|
+
files.delete('..')
|
17
|
+
files.delete_if{|file| file =~ /^\./}
|
18
|
+
files.each do |f|
|
19
|
+
if File.directory?(File.join(dirname,f))
|
20
|
+
path_array << files_path(File.join(dirname,f))
|
21
|
+
else
|
22
|
+
path_array << File.join(dirname,f)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
path_array
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
Gem::Specification::Class.send :include,DirExt
|
30
|
+
|
31
|
+
Gem::Specification.new do |s|
|
32
|
+
s.name = %{activeobject}
|
33
|
+
s.version = '0.0.3'
|
34
|
+
s.description = 'Active Object是用来访问LightCloud/TokyoCabinet/TokyoTyrant的工具,实现了持久化数据与对象的映射。 它类似于ActiveRecord,提供一组访问LightCloud/TokyoCabinet/TokyoTyrant的方法以及验证规则、回调函数和观察器。'
|
35
|
+
s.homepage = "http://www.tokyocabinet.com"
|
36
|
+
s.rubyforge_project = %q{activeobject}
|
37
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
38
|
+
s.summary = 'Active Object是用来访问LightCloud/TokyoCabinet/TokyoTyrant的工具,实现了持久化数据与对象的映射。 它类似于ActiveRecord,提供一组访问LightCloud/TokyoCabinet/TokyoTyrant的方法以及验证规则、回调函数和观察器。'
|
39
|
+
|
40
|
+
s.email = ["yalong1976@gmail.com"]
|
41
|
+
s.authors = ["yalong zhang"]
|
42
|
+
s.files = files_path_to_s(File.dirname(__FILE__)).delete_if{|path| path =~ /.gem$/}
|
43
|
+
s.require_paths = ["lib"]
|
44
|
+
s.required_ruby_version = Gem::Requirement.new(">= 1.8.6")
|
45
|
+
s.rubygems_version = %q{1.3.4}
|
46
|
+
s.add_dependency(%q<lightcloud>)
|
47
|
+
s.add_dependency(%q<rufus-tokyo>)
|
48
|
+
s.add_dependency(%q<uuid>)
|
49
|
+
s.add_dependency(%q<json>)
|
50
|
+
end
|
data/examples/account.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
class Account < ActiveObject::Base
|
2
|
+
|
3
|
+
set_attributes :email,:name,:encrypted_password,:salt
|
4
|
+
|
5
|
+
attr_accessor :password
|
6
|
+
|
7
|
+
validates_presence_of :email, :message=>'电子邮箱不能为空!'
|
8
|
+
validates_format_of :email, :with => /(\A(\s*)\Z)|(\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z)/i, :message => '无效的电子邮件格式'
|
9
|
+
validates_presence_of :password,:message =>'密码不能为空'
|
10
|
+
validates_length_of :password, :within => 6..40, :too_short => '密码太短', :too_long =>'密码过长'
|
11
|
+
validates_confirmation_of :password, :message => '两次输入的密码不匹配!'
|
12
|
+
|
13
|
+
|
14
|
+
before_save :encrypt_password
|
15
|
+
before_save :default_name
|
16
|
+
|
17
|
+
# 通过账号和未加密的密码验证用户. 返回用户或者nil.
|
18
|
+
def self.authenticate(email, password)
|
19
|
+
u = find(email)
|
20
|
+
u && u.authenticated?(password) ? u : nil
|
21
|
+
end
|
22
|
+
|
23
|
+
# 根据salt加密数据.
|
24
|
+
def self.encrypt(password, salt)
|
25
|
+
Digest::SHA1.hexdigest("--#{salt}--#{password}--")
|
26
|
+
end
|
27
|
+
|
28
|
+
# 根据用户salt加密用户的密码
|
29
|
+
def encrypt(password)
|
30
|
+
self.class.encrypt(password, salt)
|
31
|
+
end
|
32
|
+
|
33
|
+
# 校验密码认证是否通过
|
34
|
+
def authenticated?(password)
|
35
|
+
encrypted_password == encrypt(password)
|
36
|
+
end
|
37
|
+
|
38
|
+
def change_password(password,password_confirmation)
|
39
|
+
self.password= password
|
40
|
+
self.password_confirmation = password_confirmation
|
41
|
+
self.save
|
42
|
+
end
|
43
|
+
|
44
|
+
# 让系统记住当前账号,使下次登录是不需要输入账号和密码。
|
45
|
+
def remember_me
|
46
|
+
remember_me_for 2.weeks
|
47
|
+
end
|
48
|
+
|
49
|
+
def remember_me_for(time)
|
50
|
+
remember_me_until time.from_now.utc
|
51
|
+
end
|
52
|
+
|
53
|
+
def remember_me_until(time)
|
54
|
+
Token.create(:email=>self.email,:token=>encrypt("#{email}--#{time}"),:expires_at=>time)
|
55
|
+
end
|
56
|
+
|
57
|
+
protected
|
58
|
+
# before filter
|
59
|
+
def encrypt_password
|
60
|
+
return if password.blank?
|
61
|
+
self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{email}--") if new_record?
|
62
|
+
self.encrypted_password = encrypt(password)
|
63
|
+
end
|
64
|
+
|
65
|
+
def default_name
|
66
|
+
return if email.blank?
|
67
|
+
self.name = email[0,email.index('@')] if self.name.blank? && email.index('@')
|
68
|
+
end
|
69
|
+
end
|
data/examples/data.tch
ADDED
Binary file
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# LightCloud的配置文件,用于初始化LightCloud
|
2
|
+
# 示例
|
3
|
+
# user:
|
4
|
+
# lookup_one:
|
5
|
+
# - 127.0.0.1:41401
|
6
|
+
# - 127.0.0.1:41402
|
7
|
+
# storage_one:
|
8
|
+
# - 127.0.0.1:44401
|
9
|
+
# - 127.0.0.1:44402
|
10
|
+
#
|
11
|
+
|
12
|
+
user:
|
13
|
+
lookup_one:
|
14
|
+
- 127.0.0.1:30001
|
15
|
+
- 127.0.0.1:33001
|
16
|
+
storage_one:
|
17
|
+
- 127.0.0.1:20001
|
18
|
+
- 127.0.0.1:24001
|
data/examples/test.rb
ADDED
data/examples/user.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require File.join(File.dirname(__FILE__),'..','lib','active-object')
|
3
|
+
|
4
|
+
#ActiveObject::Base.configurations = File.join(File.dirname(__FILE__),'light_cloud.yml')
|
5
|
+
ActiveObject::Base.configure :TC,File.join(File.dirname(Pathname.new(__FILE__).realpath),'data.tch')
|
6
|
+
#ActiveObject::Base.configure :LC,File.join(File.dirname(Pathname.new(__FILE__).realpath),'light_cloud.yml')
|
7
|
+
#ActiveObject::Base.configure :TT,'http://127.0.0.1:55555'
|
8
|
+
|
9
|
+
class Icon < ActiveObject::Base
|
10
|
+
attribute :path
|
11
|
+
end
|
12
|
+
|
13
|
+
class Friend < ActiveObject::Base
|
14
|
+
attribute :name
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
class Account < ActiveObject::Base
|
19
|
+
primary_key :email
|
20
|
+
attribute :email,:name,:encrypted_password,:salt
|
21
|
+
|
22
|
+
has_one :icon
|
23
|
+
|
24
|
+
has_many :friends
|
25
|
+
|
26
|
+
attr_accessor :password
|
27
|
+
|
28
|
+
validates_presence_of :email, :message=>'电子邮箱不能为空!'
|
29
|
+
validates_format_of :email, :with => /(\A(\s*)\Z)|(\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z)/i, :message => '无效的电子邮件格式'
|
30
|
+
validates_presence_of :password,:message =>'密码不能为空'
|
31
|
+
validates_length_of :password, :within => 6..40, :too_short => '密码太短', :too_long =>'密码过长'
|
32
|
+
validates_confirmation_of :password, :message => '两次输入的密码不匹配!'
|
33
|
+
|
34
|
+
|
35
|
+
before_save :encrypt_password
|
36
|
+
before_save :default_name
|
37
|
+
|
38
|
+
# 通过账号和未加密的密码验证用户. 返回用户或者nil.
|
39
|
+
def self.authenticate(email, password)
|
40
|
+
u = find(email)
|
41
|
+
u && u.authenticated?(password) ? u : nil
|
42
|
+
end
|
43
|
+
|
44
|
+
# 根据salt加密数据.
|
45
|
+
def self.encrypt(password, salt)
|
46
|
+
Digest::SHA1.hexdigest("--#{salt}--#{password}--")
|
47
|
+
end
|
48
|
+
|
49
|
+
# 根据用户salt加密用户的密码
|
50
|
+
def encrypt(password)
|
51
|
+
self.class.encrypt(password, salt)
|
52
|
+
end
|
53
|
+
|
54
|
+
# 校验密码认证是否通过
|
55
|
+
def authenticated?(password)
|
56
|
+
encrypted_password == encrypt(password)
|
57
|
+
end
|
58
|
+
|
59
|
+
def change_password(password,password_confirmation)
|
60
|
+
self.password= password
|
61
|
+
self.password_confirmation = password_confirmation
|
62
|
+
self.save
|
63
|
+
end
|
64
|
+
|
65
|
+
# 让系统记住当前账号,使下次登录是不需要输入账号和密码。
|
66
|
+
def remember_me
|
67
|
+
remember_me_for 2.weeks
|
68
|
+
end
|
69
|
+
|
70
|
+
def remember_me_for(time)
|
71
|
+
remember_me_until time.from_now.utc
|
72
|
+
end
|
73
|
+
|
74
|
+
def remember_me_until(time)
|
75
|
+
Token.create(:email=>self.email,:token=>encrypt("#{email}--#{time}"),:expires_at=>time)
|
76
|
+
end
|
77
|
+
|
78
|
+
protected
|
79
|
+
# before filter
|
80
|
+
def encrypt_password
|
81
|
+
return if password.blank?
|
82
|
+
self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{email}--") if new_record?
|
83
|
+
self.encrypted_password = encrypt(password)
|
84
|
+
end
|
85
|
+
|
86
|
+
def default_name
|
87
|
+
return if email.blank?
|
88
|
+
self.name = email[0,email.index('@')] if self.name.blank? && email.index('@')
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
u = Account.new(:email=>'aaron@nonobo.com',:password=>'111111',:password_confirmation=>'111111')
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
icon = Icon.create(:path=>"/home/Aaron")
|
97
|
+
|
98
|
+
|
99
|
+
u.icon = icon
|
100
|
+
|
101
|
+
friend = Friend.create(:name=>'kame')
|
102
|
+
|
103
|
+
u.friends.append(friend)
|
104
|
+
|
105
|
+
u.friends.append(Friend.create(:name=>'jim'))
|
106
|
+
|
107
|
+
u.save
|
108
|
+
|
109
|
+
|
110
|
+
a = Account.find(u.id)
|
111
|
+
|
112
|
+
p a
|
data/init.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'lightcloud'
|
8
|
+
require 'active_support'
|
9
|
+
require 'active_object/base'
|
10
|
+
require 'active_object/validations'
|
11
|
+
require 'active_object/callbacks'
|
12
|
+
require 'active_object/observer'
|
13
|
+
require 'active_object/associations'
|
14
|
+
require 'active_object/serialization'
|
15
|
+
|
16
|
+
|
17
|
+
ActiveObject::Base.class_eval do
|
18
|
+
include ActiveObject::Validations
|
19
|
+
include ActiveObject::Callbacks
|
20
|
+
include ActiveObject::Observing
|
21
|
+
include ActiveObject::Associations
|
22
|
+
include ActiveObject::Serialization
|
23
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# 访问LightCloud的加载模块
|
2
|
+
module ActiveObjectLightCloud
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
require 'lightcloud'
|
6
|
+
base.send "configurations=", YAML.load_file(base.configurations) if base.configurations.is_a?(String)
|
7
|
+
base.configurations.each do |key,value|
|
8
|
+
lookup_nodes, storage_nodes = LightCloud.generate_nodes(value)
|
9
|
+
LightCloud.init(lookup_nodes, storage_nodes,key)
|
10
|
+
end
|
11
|
+
|
12
|
+
base.extend ClassMethods
|
13
|
+
base.send :include, InstanceMethods
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def delete(id)
|
18
|
+
LightCloud.delete(self.id_to_key(id))
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def load(id)
|
23
|
+
raw_record = LightCloud.get(self.id_to_key(id)) || raise(ActiveObject::ObjectNotFound)
|
24
|
+
deserialize_attributes(raw_record)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module InstanceMethods
|
29
|
+
private
|
30
|
+
def set
|
31
|
+
begin
|
32
|
+
LightCloud.set(@key,serialize_attributes)
|
33
|
+
true
|
34
|
+
rescue
|
35
|
+
false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
|
2
|
+
# 访问TokyoCabinet的加载模块
|
3
|
+
module ActiveObjectTokyoCabinet
|
4
|
+
def self.included(base)
|
5
|
+
require 'rufus/tokyo'
|
6
|
+
|
7
|
+
base.extend ClassMethods
|
8
|
+
base.send :include, InstanceMethods
|
9
|
+
|
10
|
+
base.send :adb=, Rufus::Tokyo::Cabinet.new(base.configurations)
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
mattr_accessor :adb
|
15
|
+
def delete(id)
|
16
|
+
adb.delete(self.id_to_key(id))
|
17
|
+
end
|
18
|
+
|
19
|
+
def all
|
20
|
+
keys = adb.keys(:prefix=>"#{self.to_s}_")
|
21
|
+
records = []
|
22
|
+
keys.each do |key|
|
23
|
+
records << find(key_to_id(key))
|
24
|
+
end
|
25
|
+
records
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def load(id)
|
31
|
+
raw_record = adb[self.id_to_key(id)] || raise(ActiveObject::ObjectNotFound)
|
32
|
+
deserialize_attributes(raw_record)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
module InstanceMethods
|
37
|
+
private
|
38
|
+
def set
|
39
|
+
begin
|
40
|
+
self.class.adb[@key]= serialize_attributes
|
41
|
+
true
|
42
|
+
rescue
|
43
|
+
|
44
|
+
false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# 访问TokyoTyrant的加载模块
|
2
|
+
module ActiveObjectTokyoTyrant
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
require 'uri'
|
6
|
+
require 'rufus/tokyo'
|
7
|
+
uri = URI.parse(base.configurations)
|
8
|
+
|
9
|
+
base.extend ActiveObjectTokyoCabinet::ClassMethods
|
10
|
+
base.send :include, ActiveObjectTokyoCabinet::InstanceMethods
|
11
|
+
|
12
|
+
base.send :adb=, Rufus::Tokyo::Tyrant.new(uri.host,uri.port)
|
13
|
+
end
|
14
|
+
end
|