Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add more serialization types and made it have defaults for *any* class

  • Loading branch information...
commit e5549bfcedebb810e8671a41ad9c0f15698353fa 1 parent 8244e5c
@BrianTheCoder authored
View
3  .gitignore
@@ -20,4 +20,5 @@ pkg
## PROJECT::SPECIFIC
.gitignore
-tmp/*
+tmp/*
+*.gem
View
6 Rakefile
@@ -5,13 +5,13 @@ begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "redis-types"
- gem.summary = %Q{TODO: a port of redis-model to a module for composition}
- gem.description = %Q{TODO: this is my port of redis-module because I prefer composition over inheritance, cause then you can combine it with other orms. I also changed json parsing to yajl-ruby and tweaked a few other things to my preferences}
+ gem.summary = %Q{a way of modeling data to redis}
+ gem.description = %Q{this is so awesome it makes grown men cry}
gem.email = "wbsmith83@gmail.com"
gem.homepage = "http://github.com/BrianTheCoder/redis-types"
gem.authors = ["brianthecoder"]
gem.add_development_dependency "rspec", ">= 0"
- gem.add_dependency "redis-rb"
+ gem.add_dependency "redis"
gem.add_dependency "yajl-ruby"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
View
63 lib/redis/data_types.rb
@@ -1,6 +1,6 @@
class Redis
module DataTypes
- TYPES = %w(String Integer Float EpochTime DateTime Json Yaml)
+ TYPES = %w(String Integer Float EpochTime DateTime Json Yaml IPAddress FilePath Uri Slug)
def self.define_data_types
TYPES.each do |data_type|
if Object.const_defined?(data_type)
@@ -12,23 +12,20 @@ def self.define_data_types
end
end
- module String
- def to_redis(value); value; end
- def from_redis(value); value; end
- end
-
+ FROM = Proc.new{|value| value }
+
+ TO = Proc.new{|value| value.to_s }
+
module Integer
- def to_redis(value); value.to_s; end
- def from_redis(value); value && value.to_i; end
+ def from_redis(value); value && value.to_i end
end
module Float
- def to_redis(value); value.to_s; end
- def from_redis(value); value && value.to_f; end
+ def from_redis(value); value && value.to_f end
end
module EpochTime
- def to_redis(value) value.is_a?(Integer) ? Time.at(value) : value end
+ def to_redis(value) Time.at(value.to_i) end
def from_redis(value)
value.is_a?(DateTime) ? value.to_time.to_i : value.to_i
@@ -36,18 +33,50 @@ def from_redis(value)
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
+ def to_redis(value); value.strftime('%FT%T%z') end
+ def from_redis(value); value && ::DateTime.strptime(value, '%FT%T%z') end
end
module Json
- def to_redis(value); Yajl::Encoder.encode(value); end
- def from_redis(value); value && Yajl::Parser.parse(value); end
+ 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
+ def to_redis(value); Yaml.dump(value) end
+ def from_redis(value); Yaml.load(value) end
+ end
+
+ module IPAddress
+ def from_redis(value)
+ return nil if value.nil?
+ if value.is_a?(String)
+ IPAddr.new(value.empty? ? '0.0.0.0' : value)
+ else
+ raise "+value+ must be nil or a String"
+ end
+ end
+ end
+
+ module FilePath
+ require 'pathname'
+ def from_redis(value)
+ value.blank? ? nil : Pathname.new(value)
+ end
+ end
+
+ module Uri
+ require 'addressable/uri'
+ def from_redis(value)
+ Addressable::URI.parse(value)
+ end
+ end
+
+ module Slug
+ require 'stringex'
+ def to_redis(value)
+ value.to_url
+ end
end
end
end
View
6 lib/redis/field_proxy.rb
@@ -6,6 +6,7 @@ def initialize(redis, marshal)
@redis = redis
@key = key
@marshal = marshal
+ validate_marshal
end
def method_missing(method, *argv)
@@ -15,6 +16,11 @@ def method_missing(method, *argv)
end
protected
+
+ def validate_mashal
+ @marshal.extend Redis::DataTypes::TO unless @marshal.responds_to?(:to_redis)
+ @marshal.extend Redis::DataTypes::FROM unless @marshal.responds_to?(:from_redis)
+ end
def translate_method_name(m); m; end
end
View
4 lib/redis/list.rb
@@ -33,8 +33,10 @@ def trim(from, to); redis.ltrim(key, from, to); end
def to_s; range(0, 100).join(', '); end
- def set(value)
+ def get; self end
+ def set(value)
+ value.each{|item| redis.rpush(marshal.to_redis(item)) }
end
protected
View
16 lib/redis/set.rb
@@ -7,11 +7,11 @@ class Redis::Set
:move => "smove",
}
- def <<(value); redis.sadd key, marshal.to_redis(value); end
+ def <<(value); redis.sadd key, marshal.to_redis(value) end
- def delete(value); redis.srem key, marshal.to_redis(value); end
+ def delete(value); redis.srem key, marshal.to_redis(value) end
- def include?(value); redis.sismember(key, marshal.to_redis(value)) == 1; end
+ def include?(value); redis.sismember(key, marshal.to_redis(value)) == 1 end
alias_method :add, :<<
alias_method :remove, :delete
@@ -34,15 +34,17 @@ def diff(*keys)
redis.sdiff(key, *keys).map{|value| marshal.from_redis(value) }
end
- def length; redis.llen(key); end
+ def length; redis.llen(key) end
- def to_s; members.join(', '); end
+ def to_s; members.join(', ') end
+
+ def get; self end
def set(value)
-
+ value.each{|item| redis.sadd(marshal.to_redis(item)) }
end
protected
- def translate_method_name(m); COMMANDS[m]; end
+ def translate_method_name(m); COMMANDS[m] end
end
View
14 lib/redis/types.rb
@@ -39,11 +39,11 @@ def value(name, type = 'String', opts = {})
redis_field(name, type, ::Redis::Value)
end
- def redis(opts = {}); @@_redis ||= Redis.new(opts); end
+ def redis(opts = {}); @@_redis ||= Redis.new(opts) end
- def delete(id); self.find(id).destroy; end
+ def delete(id); self.find(id).destroy end
- def find(id); self.new(:id => id); end
+ def find(id); self.new(:id => id) end
private
@@ -64,7 +64,7 @@ def redis_#{type}_#{name}
end
def define_getter(name, type) #:nodoc:
- class_eval "def #{name}; redis_#{type}_#{name}; end"
+ class_eval "def #{name}; redis_#{type}_#{name}.get; end"
end
def define_setter(name, type) #:nodoc:
@@ -84,7 +84,7 @@ def initialize(attrs = {})
end
end
- def field_key(name); "#{prefix}:#{id}:#{name}"; end
+ def field_key(name); "#{prefix}:#{id}:#{name}" end
def prefix #:nodoc:
@prefix ||= self.class.prefix || self.class.to_s.
@@ -94,9 +94,7 @@ def prefix #:nodoc:
downcase
end
- def redis(opts = {}) #:nodoc:
- self.class.redis(opts)
- end
+ def redis(opts = {}); self.class.redis(opts) end
def [](method)
raise NoMethodError, "method is undefined: #{method}" unless respond_to?(method)
View
4 lib/redis/value.rb
@@ -1,7 +1,7 @@
class Redis::Value
include Redis::FieldProxy
- def set(value); redis[key] = marshal.to_redis(value); end
+ def set(value); redis[key] = marshal.to_redis(value) end
- def get; marshal.from_redis(redis[key]); end
+ def get; marshal.from_redis(redis[key]) end
end
View
73 redis-types.gemspec
@@ -0,0 +1,73 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{redis-types}
+ s.version = "0.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["brianthecoder"]
+ s.date = %q{2009-11-13}
+ s.description = %q{this is so awesome it makes grown men cry}
+ s.email = %q{wbsmith83@gmail.com}
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.rdoc"
+ ]
+ s.files = [
+ ".document",
+ ".gitignore",
+ "LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "TODO",
+ "VERSION",
+ "example/types.rb",
+ "lib/redis/data_types.rb",
+ "lib/redis/field_proxy.rb",
+ "lib/redis/list.rb",
+ "lib/redis/set.rb",
+ "lib/redis/types.rb",
+ "lib/redis/value.rb",
+ "spec/dump.rdb",
+ "spec/redis/types_spec.rb",
+ "spec/spec.opts",
+ "spec/spec_helper.rb",
+ "spec/test.conf",
+ "test/helper.rb",
+ "test/test_redis-types.rb"
+ ]
+ s.homepage = %q{http://github.com/BrianTheCoder/redis-types}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.5}
+ s.summary = %q{a way of modeling data to redis}
+ s.test_files = [
+ "spec/redis/types_spec.rb",
+ "spec/spec_helper.rb",
+ "test/helper.rb",
+ "test/test_redis-types.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_development_dependency(%q<rspec>, [">= 0"])
+ s.add_runtime_dependency(%q<redis>, [">= 0"])
+ s.add_runtime_dependency(%q<yajl-ruby>, [">= 0"])
+ else
+ s.add_dependency(%q<rspec>, [">= 0"])
+ s.add_dependency(%q<redis>, [">= 0"])
+ s.add_dependency(%q<yajl-ruby>, [">= 0"])
+ end
+ else
+ s.add_dependency(%q<rspec>, [">= 0"])
+ s.add_dependency(%q<redis>, [">= 0"])
+ s.add_dependency(%q<yajl-ruby>, [">= 0"])
+ end
+end
+
View
14 spec/redis/types_spec.rb
@@ -69,27 +69,19 @@ 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
Project.redis_fields['name'].marshal.should be_kind_of(String)
end
- describe 'a setter method' do
+ describe 'a value' do
end
- describe 'a bool method' do
+ describe 'a list' do
end
- describe 'a getter method' do
+ describe 'a set' do
end
end

0 comments on commit e5549bf

Please sign in to comment.
Something went wrong with that request. Please try again.