Simplifying and moving CastedArray into own file
This commit is contained in:
parent
1b019fa3fe
commit
f196aacecc
4 changed files with 35 additions and 25 deletions
|
@ -1,5 +1,6 @@
|
||||||
require 'time'
|
require 'time'
|
||||||
require File.join(File.dirname(__FILE__), '..', 'more', 'property')
|
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')
|
require File.join(File.dirname(__FILE__), '..', 'more', 'typecast')
|
||||||
|
|
||||||
module CouchRest
|
module CouchRest
|
||||||
|
@ -58,7 +59,7 @@ module CouchRest
|
||||||
value
|
value
|
||||||
end
|
end
|
||||||
# allow casted_by calls to be passed up chain by wrapping in CastedArray
|
# 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)
|
self[key].casted_by = self if self[key].respond_to?(:casted_by)
|
||||||
else
|
else
|
||||||
self[key] = typecast_value(self[key], property.type, property.init_method)
|
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
|
else
|
||||||
base_type = type.is_a?(Array) ? type.first : type
|
base_type = type.is_a?(Array) ? type.first : type
|
||||||
if base_type.is_a?(String)
|
if base_type.is_a?(String)
|
||||||
base_type = TrueClass if base_type.downcase == 'boolean'
|
if base_type.downcase == 'boolean'
|
||||||
begin
|
base_type = TrueClass
|
||||||
base_type = ::CouchRest.constantize(base_type) unless base_type.is_a?(Class)
|
else
|
||||||
rescue # leave base type as is and convert in more/typecast
|
begin
|
||||||
|
base_type = ::CouchRest.constantize(base_type)
|
||||||
|
rescue # leave base type as a string and convert in more/typecast
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@type = type.is_a?(Array) ? [base_type] : base_type
|
@type = type.is_a?(Array) ? [base_type] : base_type
|
||||||
|
@ -45,22 +48,3 @@ module CouchRest
|
||||||
|
|
||||||
end
|
end
|
||||||
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.toys.push(toy)
|
||||||
@cat.save.should be_true
|
@cat.save.should be_true
|
||||||
@cat = Cat.get @cat.id
|
@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.class.should == CatToy
|
||||||
@cat.toys.first.should === toy
|
@cat.toys.first.should === toy
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue