Permalink
Browse files

better type handling, extending class if exists, changed serializatio…

…n method names so they will not conflict with existing orms
  • Loading branch information...
1 parent 253c80d commit 8244e5c55b8af8748f8ad13bcae9bf64c0ec9568 @BrianTheCoder committed Nov 2, 2009
Showing with 69 additions and 67 deletions.
  1. +1 −1 README.rdoc
  2. +0 −4 example/types.rb
  3. +37 −23 lib/redis/data_types.rb
  4. +12 −12 lib/redis/list.rb
  5. +7 −7 lib/redis/set.rb
  6. +2 −18 lib/redis/types.rb
  7. +2 −2 lib/redis/value.rb
  8. BIN spec/dump.rdb
  9. +8 −0 spec/redis/types_spec.rb
View
@@ -1,6 +1,6 @@
= redis-types
-Description goes here.
+Right now this is crazy alpha, use at risk of death and destruction(or epic fail). The example from redis model passes, but I need to write a ton more specs before I'm comfortable promoting
== Note on Patches/Pull Requests
View
@@ -19,11 +19,8 @@ class User
u = User.new(:id => 1)
u.destroy
-p 'name'
u.name = 'Joe'
-p 'created'
u.created = DateTime.now
-p 'profile'
u.profile = {
:age => 23,
:sex => 'M',
@@ -36,7 +33,6 @@ class User
u.followers << 2
-p 'printing'
u = User.new(:id => 1)
p u.name.get
p u.created.get.strftime('%m/%d/%Y')
View
@@ -1,39 +1,53 @@
class Redis
- class DataTypes
- class String
- def self.dump(value); value; end
- def self.load(value); value; end
+ module DataTypes
+ TYPES = %w(String Integer Float EpochTime DateTime Json Yaml)
+ def self.define_data_types
+ TYPES.each do |data_type|
+ if Object.const_defined?(data_type)
+ klass = Object.const_get(data_type)
+ else
+ klass = Object.const_set(data_type, Class.new)
+ end
+ klass.extend const_get(data_type)
+ end
+ end
+
+ module String
+ def to_redis(value); value; end
+ def from_redis(value); value; end
end
- class Integer
- def self.dump(value); value.to_s; end
- def self.load(value); value && value.to_i; end
+ module Integer
+ def to_redis(value); value.to_s; end
+ def from_redis(value); value && value.to_i; end
end
- class Float
- def self.dump(value); value.to_s; end
- def self.load(value); value && value.to_f; end
+ module Float
+ def to_redis(value); value.to_s; end
+ def from_redis(value); value && value.to_f; end
end
- class EpochTime < Time
- def self.dump(value)
- value.is_a?(Integer) ? Time.at(value) : value
- end
+ module EpochTime
+ def to_redis(value) value.is_a?(Integer) ? Time.at(value) : value end
- def self.load(value)
- return value.to_time.to_i if value.is_a? DateTime
- value.to_i
+ def from_redis(value)
+ value.is_a?(DateTime) ? value.to_time.to_i : value.to_i
end
end
- class DateTime
- def self.dump(value); value.strftime('%FT%T%z'); end
- def self.load(value); value && ::DateTime.strptime(value, '%FT%T%z'); end
+ module DateTime
+ def to_redis(value); value.strftime('%FT%T%z'); end
+ def from_redis(value); value && ::DateTime.strptime(value, '%FT%T%z'); end
end
- class Json
- def self.dump(value); Yajl::Encoder.encode(value); end
- def self.load(value); value && Yajl::Parser.parse(value); end
+ module Json
+ def to_redis(value); Yajl::Encoder.encode(value); end
+ def from_redis(value); value && Yajl::Parser.parse(value); end
+ end
+
+ module Yaml
+ def to_redis(value); Yaml.dump(value); end
+ def from_redis(value); Yaml.load(value); end
end
end
end
View
@@ -1,37 +1,37 @@
class Redis::List
include Redis::FieldProxy #:nodoc:
- def <<(value) redis.rpush key, marshal.dump(value); end
+ def <<(value) redis.rpush key, marshal.to_redis(value); end
- def push_head(value); redis.lpush key, marshal.dump(value); end
+ def push_head(value); redis.lpush key, marshal.to_redis(value); end
- def pop_tail; marshal.load(redis.rpop(key)); end
+ def pop_tail; marshal.from_redis(redis.rpop(key)); end
- def pop_head; marshal.load(redis.lpop(key)); end
+ def pop_head; marshal.from_redis(redis.lpop(key)); end
def [](from, to = nil)
if to.nil?
- marshal.load(redis.lindex(key, from))
+ marshal.from_redis(redis.lindex(key, from))
else
- redis.lrange(key, from, to).map!{|value| marshal.load(value) }
+ redis.lrange(key, from, to).map!{|value| marshal.from_redis(value) }
end
end
- def []=(index, value); redis.lset(key, index, marshal.dump(value)); end
+ def []=(index, value); redis.lset(key, index, marshal.to_redis(value)); end
alias_method :range, :[]
alias_method :push_tail, :<<
alias_method :set, :[]=
- def include?(value); redis.exists(key, marshal.dump(value)); end
+ def include?(value); redis.exists(key, marshal.to_redis(value)); end
- def remove(count, value); redis.lrem(key, count, marshal.dump(value)); end
+ def remove(count, value); redis.lrem(key, count, marshal.to_redis(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 to_s; range(0, 100).join(', '); end
def set(value)
View
@@ -7,31 +7,31 @@ class Redis::Set
:move => "smove",
}
- def <<(value); redis.sadd key, marshal.dump(value); end
+ def <<(value); redis.sadd key, marshal.to_redis(value); end
- def delete(value); redis.srem key, marshal.dump(value); end
+ def delete(value); redis.srem key, marshal.to_redis(value); end
- def include?(value); redis.sismember(key, marshal.dump(value)) == 1; end
+ def include?(value); redis.sismember(key, marshal.to_redis(value)) == 1; end
alias_method :add, :<<
alias_method :remove, :delete
alias_method :has_key?, :include?
alias_method :member?, :include?
def members
- redis.smembers(key).map{|value| marshal.load(value) }
+ redis.smembers(key).map{|value| marshal.from_redis(value) }
end
def intersect(*keys)
- redis.sinter(key, *keys).map{|value| marshal.load(value) }
+ redis.sinter(key, *keys).map{|value| marshal.from_redis(value) }
end
def union(*keys)
- redis.sunion(@key, *keys).map{|value| marshal.load(value) }
+ redis.sunion(@key, *keys).map{|value| marshal.from_redis(value) }
end
def diff(*keys)
- redis.sdiff(key, *keys).map{|value| marshal.load(value) }
+ redis.sdiff(key, *keys).map{|value| marshal.from_redis(value) }
end
def length; redis.llen(key); end
View
@@ -17,15 +17,11 @@ module Types
class InvalidDataType < StandardError; end
def self.included(model)
- extend_core_types
+ Redis::DataTypes.define_data_types
model.send :include, Extlib::Hook
model.extend ClassMethods
end
- def self.extend_core_types
-
- end
-
module ClassMethods
attr_accessor :prefix
@@ -48,25 +44,13 @@ def redis(opts = {}); @@_redis ||= Redis.new(opts); end
def delete(id); self.find(id).destroy; end
def find(id); self.new(:id => id); end
-
- def const_missing(name)
- return Redis::DataTypes.const_get(name) if Redis::DataTypes.const_defined?(name)
- super
- end
private
def redis_field(name, type, redis_type)
- redis_fields[name.to_s] = redis_type.new(redis, get_field_class(type))
+ redis_fields[name.to_s] = redis_type.new(redis, 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
View
@@ -1,7 +1,7 @@
class Redis::Value
include Redis::FieldProxy
- def set(value); redis[key] = marshal.dump(value); end
+ def set(value); redis[key] = marshal.to_redis(value); end
- def get; marshal.load(redis[key]); end
+ def get; marshal.from_redis(redis[key]); end
end
View
Binary file not shown.
View
@@ -69,6 +69,14 @@ class Commit
it "sets the key #{method} in redis_fields" do
Project.redis_fields.should have_key(method.to_s)
end
+
+ it "should dump the datatype to a string" do
+
+ end
+
+ it "should load the datatype to the class" do
+
+ end
end
it "should default to String type" do

0 comments on commit 8244e5c

Please sign in to comment.