Simplifying and moving CastedArray into own file
This commit is contained in:
parent
1b019fa3fe
commit
f196aacecc
|
@ -1,5 +1,6 @@
|
|||
require 'time'
|
||||
require File.join(File.dirname(__FILE__), '..', 'more', 'property')
|
||||
require File.join(File.dirname(__FILE__), '..', 'more', 'casted_array')
|
||||
require File.join(File.dirname(__FILE__), '..', 'more', 'typecast')
|
||||
|
||||
module CouchRest
|
||||
|
@ -58,7 +59,7 @@ module CouchRest
|
|||
value
|
||||
end
|
||||
# allow casted_by calls to be passed up chain by wrapping in CastedArray
|
||||
self[key] = klass != String ? CastedArray.new(arr) : arr
|
||||
self[key] = klass != String ? ::CouchRest::CastedArray.new(arr) : arr
|
||||
self[key].casted_by = self if self[key].respond_to?(:casted_by)
|
||||
else
|
||||
self[key] = typecast_value(self[key], property.type, property.init_method)
|
||||
|
|
25
lib/couchrest/more/casted_array.rb
Normal file
25
lib/couchrest/more/casted_array.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
#
|
||||
# Wrapper around Array so that the casted_by attribute is set in all
|
||||
# elements of the array.
|
||||
#
|
||||
|
||||
module CouchRest
|
||||
class CastedArray < Array
|
||||
attr_accessor :casted_by
|
||||
|
||||
def << obj
|
||||
obj.casted_by = self.casted_by if obj.respond_to?(:casted_by)
|
||||
super(obj)
|
||||
end
|
||||
|
||||
def push(obj)
|
||||
obj.casted_by = self.casted_by if obj.respond_to?(:casted_by)
|
||||
super(obj)
|
||||
end
|
||||
|
||||
def []= index, obj
|
||||
obj.casted_by = self.casted_by if obj.respond_to?(:casted_by)
|
||||
super(index, obj)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -22,10 +22,13 @@ module CouchRest
|
|||
else
|
||||
base_type = type.is_a?(Array) ? type.first : type
|
||||
if base_type.is_a?(String)
|
||||
base_type = TrueClass if base_type.downcase == 'boolean'
|
||||
if base_type.downcase == 'boolean'
|
||||
base_type = TrueClass
|
||||
else
|
||||
begin
|
||||
base_type = ::CouchRest.constantize(base_type) unless base_type.is_a?(Class)
|
||||
rescue # leave base type as is and convert in more/typecast
|
||||
base_type = ::CouchRest.constantize(base_type)
|
||||
rescue # leave base type as a string and convert in more/typecast
|
||||
end
|
||||
end
|
||||
end
|
||||
@type = type.is_a?(Array) ? [base_type] : base_type
|
||||
|
@ -45,22 +48,3 @@ module CouchRest
|
|||
|
||||
end
|
||||
end
|
||||
|
||||
class CastedArray < Array
|
||||
attr_accessor :casted_by
|
||||
|
||||
def << obj
|
||||
obj.casted_by = self.casted_by if obj.respond_to?(:casted_by)
|
||||
super(obj)
|
||||
end
|
||||
|
||||
def push(obj)
|
||||
obj.casted_by = self.casted_by if obj.respond_to?(:casted_by)
|
||||
super(obj)
|
||||
end
|
||||
|
||||
def []= index, obj
|
||||
obj.casted_by = self.casted_by if obj.respond_to?(:casted_by)
|
||||
super(index, obj)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -210,7 +210,7 @@ describe CouchRest::CastedModel do
|
|||
@cat.toys.push(toy)
|
||||
@cat.save.should be_true
|
||||
@cat = Cat.get @cat.id
|
||||
@cat.toys.class.should == CastedArray
|
||||
@cat.toys.class.should == CouchRest::CastedArray
|
||||
@cat.toys.first.class.should == CatToy
|
||||
@cat.toys.first.should === toy
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue