Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

example is working, now for more specs

  • Loading branch information...
commit 253c80d268700732af81e02cfff910fafcd1863f 1 parent 887f1ee
@BrianTheCoder authored
View
16 example/types.rb
@@ -18,9 +18,12 @@ class User
u = User.new(:id => 1)
-# u.destroy
-u.name = 'Joe'
-u.created = DateTime.now
+u.destroy
+p 'name'
+u.name = 'Joe'
+p 'created'
+u.created = DateTime.now
+p 'profile'
u.profile = {
:age => 23,
:sex => 'M',
@@ -33,10 +36,11 @@ class User
u.followers << 2
-
+p 'printing'
u = User.new(:id => 1)
-p u.name
-p u.created.strftime('%m/%d/%Y')
+p u.name.get
+p u.created.get.strftime('%m/%d/%Y')
+p u.profile.get
p u.posts[0,20]
p u.posts[0]
p u.followers.has_key?(2)
View
2  lib/redis/data_types.rb
@@ -33,7 +33,7 @@ def self.load(value); value && ::DateTime.strptime(value, '%FT%T%z'); end
class Json
def self.dump(value); Yajl::Encoder.encode(value); end
- def self.load(value); v && Yajl::Parser.parse(value); end
+ def self.load(value); value && Yajl::Parser.parse(value); end
end
end
end
View
28 lib/redis/list.rb
@@ -1,42 +1,40 @@
class Redis::List
include Redis::FieldProxy #:nodoc:
- def <<(v)
- @redis.rpush @key, @marshal.dump(v)
- end
- def push_head(v); @redis.lpush @key, @marshal.dump(v); end
+ def <<(value) redis.rpush key, marshal.dump(value); end
+
+ def push_head(value); redis.lpush key, marshal.dump(value); end
- def pop_tail; @marshal.load(@redis.rpop(@key)); end
+ def pop_tail; marshal.load(redis.rpop(key)); end
- def pop_head; @marshal.load(@redis.lpop(@key)); end
+ def pop_head; marshal.load(redis.lpop(key)); end
def [](from, to = nil)
if to.nil?
- @marshal.load(@redis.lindex(@key, from))
+ marshal.load(redis.lindex(key, from))
else
- @redis.lrange(@key, from, to).map! { |v| @marshal.load(v) }
+ redis.lrange(key, from, to).map!{|value| marshal.load(value) }
end
end
- def []=(index, v); @redis.lset(@key, index, @marshal.dump(v)); end
-
+ def []=(index, value); redis.lset(key, index, marshal.dump(value)); end
alias_method :range, :[]
alias_method :push_tail, :<<
alias_method :set, :[]=
- def include?(v); @redis.exists(@key, @marshal.dump(v)); end
+ def include?(value); redis.exists(key, marshal.dump(value)); end
- def remove(count, v); @redis.lrem(@key, count, @marshal.dump(v)); end
+ def remove(count, value); redis.lrem(key, count, marshal.dump(value)); end
- def length; @redis.llen(@key); end
+ def length; redis.llen(key); end
- def trim(from, to); @redis.ltrim(@key, from, to); end
+ def trim(from, to); redis.ltrim(key, from, to); end
def to_s; range(0, 100).join(', '); end
def set(value)
-
+
end
protected
View
18 lib/redis/set.rb
@@ -1,4 +1,4 @@
-class Redis::Set < Set
+class Redis::Set
include Redis::FieldProxy #:nodoc:
COMMANDS = {
:intersect_store => "sinterstore",
@@ -7,11 +7,11 @@ class Redis::Set < Set
:move => "smove",
}
- def <<(v); @redis.sadd @key, @marshal.dump(v); end
+ def <<(value); redis.sadd key, marshal.dump(value); end
- def delete(v); @redis.srem @key, @marshal.dump(v); end
+ def delete(value); redis.srem key, marshal.dump(value); end
- def include?(v); @redis.sismember @key, @marshal.dump(v); end
+ def include?(value); redis.sismember(key, marshal.dump(value)) == 1; end
alias_method :add, :<<
alias_method :remove, :delete
@@ -19,22 +19,22 @@ def include?(v); @redis.sismember @key, @marshal.dump(v); end
alias_method :member?, :include?
def members
- @redis.smembers(@key).map { |v| @marshal.load(v) }
+ redis.smembers(key).map{|value| marshal.load(value) }
end
def intersect(*keys)
- @redis.sinter(@key, *keys).map { |v| @marshal.load(v) }
+ redis.sinter(key, *keys).map{|value| marshal.load(value) }
end
def union(*keys)
- @redis.sunion(@key, *keys).map { |v| @marshal.load(v) }
+ redis.sunion(@key, *keys).map{|value| marshal.load(value) }
end
def diff(*keys)
- @redis.sdiff(@key, *keys).map { |v| @marshal.load(v) }
+ redis.sdiff(key, *keys).map{|value| marshal.load(value) }
end
- def length; @redis.llen(@key); end
+ def length; redis.llen(key); end
def to_s; members.join(', '); end
View
14 lib/redis/types.rb
@@ -14,6 +14,8 @@ class Redis
autoload :DataTypes, dir / 'data_types'
module Types
+ class InvalidDataType < StandardError; end
+
def self.included(model)
extend_core_types
model.send :include, Extlib::Hook
@@ -55,10 +57,16 @@ def const_missing(name)
private
def redis_field(name, type, redis_type)
- p type
- redis_fields[name.to_s] = redis_type.new(redis, type)
+ redis_fields[name.to_s] = redis_type.new(redis, get_field_class(type))
field_methods name, redis_type.name.split('::').last.downcase
end
+
+ def get_field_class(klass)
+ return klass if klass.name =~ /Redis::DataTypes/
+ name = klass.name.split('::').last
+ raise InvalidDataType unless Redis::DataTypes.const_defined?(name)
+ Redis::DataTypes.const_get(name)
+ end
def field_methods(name, type) #:nodoc:
class_eval <<-RUBY, __FILE__, __LINE__ + 1
@@ -120,7 +128,7 @@ def destroy(name = nil)
if name
redis.delete field_key(name.to_s)
else
- self.class.redis_fields.each do |field|
+ self.class.redis_fields.each do |field, klass|
redis.delete field_key(field)
end
end
View
4 lib/redis/value.rb
@@ -1,3 +1,7 @@
class Redis::Value
include Redis::FieldProxy
+
+ def set(value); redis[key] = marshal.dump(value); end
+
+ def get; marshal.load(redis[key]); end
end
View
BIN  spec/dump.rdb
Binary file not shown
View
16 spec/redis/types_spec.rb
@@ -34,13 +34,17 @@ class Commit
end
describe 'fields' do
+ before do
+ @project = Project.new
+ @_redis_mock = Spec::Mocks::Mock.new
+ @project.stub!(:redis).and_return(@_redis_mock)
+ end
+
{
:name => :value,
:users => :list,
:commits => :set
- }.each do |method, type|
- before{ @project = Project.new }
-
+ }.each do |method, type|
it "defines a getter method for #{method}" do
@project.should respond_to(method)
end
@@ -71,14 +75,14 @@ class Commit
Project.redis_fields['name'].marshal.should be_kind_of(String)
end
- describe 'a getter method' do
- end
-
describe 'a setter method' do
end
describe 'a bool method' do
end
+
+ describe 'a getter method' do
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.