Permalink
Browse files

Class typecast method available for special properties

  • Loading branch information...
samlown committed Jun 15, 2012
1 parent e221051 commit ab1be6c337d59b1d3aadc5fe3291be9440b2fa10
View
@@ -6,6 +6,7 @@
* Rake tasks available for migrations
* Rails config option now available: `config.couchrest_model.auto_update_design_docs = false`
* Skipping 1.2 version due to design doc API changes
+ * Added 'couchrest_typecast' class method support for typecasting with special classes.
## 1.2.0.beta - 2012-06-08
@@ -45,7 +45,7 @@ def cast(parent, value)
# Cast an individual value
def cast_value(parent, value)
- value = typecast_value(value, self)
+ value = typecast_value(parent, self, value)
associate_casted_value_to_parent(parent, value)
end
@@ -64,6 +64,7 @@ def default_value
# a normal call to the class.
def build(*args)
raise StandardError, "Cannot build property without a class" if @type_class.nil?
+
if @init_method.is_a?(Proc)
@init_method.call(*args)
else
@@ -106,7 +107,7 @@ def parse_options(options)
@read_only = options.delete(:read_only) if options[:read_only]
@alias = options.delete(:alias) if options[:alias]
@default = options.delete(:default) unless options[:default].nil?
- @init_method = options[:init_method] ? options.delete(:init_method) : 'new'
+ @init_method = options.delete(:init_method) || 'new'
@options = options
end
@@ -2,7 +2,7 @@ module CouchRest
module Model
module Typecast
- def typecast_value(value, property) # klass, init_method)
+ def typecast_value(parent, property, value)
return nil if value.nil?
klass = property.type_class
if value.instance_of?(klass) || klass == Object
@@ -11,6 +11,8 @@ def typecast_value(value, property) # klass, init_method)
else
value
end
+ elsif klass.respond_to?(:couchrest_typecast)
+ klass.couchrest_typecast(parent, property, value)
elsif [String, TrueClass, Integer, Float, BigDecimal, DateTime, Time, Date, Class].include?(klass)
send('typecast_to_'+klass.to_s.downcase, value)
else
@@ -1,5 +1,6 @@
require 'question'
require 'person'
+require 'money'
class Course < CouchRest::Model::Base
use_database TEST_SERVER.default_database
@@ -17,6 +18,8 @@ class Course < CouchRest::Model::Base
property :active, :type => TrueClass
property :very_active, :type => TrueClass
property :klass, :type => Class
+ property :currency, String, :default => 'EUR'
+ property :price, Money
design do
view :by_title
@@ -0,0 +1,24 @@
+
+# Really simple money class for testing
+class Money
+
+ attr_accessor :cents, :currency
+
+ def initialize(cents, currency = nil)
+ self.cents = cents.to_i
+ self.currency = currency
+ end
+
+ def to_s
+ (self.cents.to_f / 100).to_s
+ end
+
+ def self.couchrest_typecast(parent, property, value)
+ if parent.respond_to?(:currency)
+ new(value, parent.currency)
+ else
+ new(value)
+ end
+ end
+
+end
@@ -26,6 +26,14 @@
end
end
+ describe "when class responds to .couchrest_typecast" do
+ it "should accept call" do
+ @course.price = "1299"
+ @course.price.cents.should eql(1299)
+ @course.price.currency.should eql('EUR')
+ end
+ end
+
describe "when type primitive is a String" do
it "keeps string value unchanged" do
value = "1.0"

0 comments on commit ab1be6c

Please sign in to comment.