Permalink
Browse files

allow namespaces when determine class name

  • Loading branch information...
1 parent e272906 commit 48ea2a524a8d0006ddfc8538a63b63227ef1b13a @mkristian committed Dec 24, 2012
Showing with 25 additions and 12 deletions.
  1. +25 −12 lib/ixtlan/babel/factory.rb
@@ -4,23 +4,28 @@ class Factory
NANOSECONDS_IN_DAY = Rational(1, 86400*10**9)
+ TIME_TO_S = Proc.new do |t|
+ t.strftime('%Y-%m-%dT%H:%M:%S.') + ("%06d" % t.usec) + t.strftime('%z')
+ end
+
+ DATE_TIME_TO_S = Proc.new do |dt|
+ dt.strftime('%Y-%m-%dT%H:%M:%S.') + ("%06d" % (dt.sec_fraction / NANOSECONDS_IN_DAY / 1000)) + dt.strftime('%z')
+ end
+
+ DEFAULT_MAP = {
+ 'DateTime' => DATE_TIME_TO_S,
+ 'ActiveSupport::TimeWithZone' => TIME_TO_S,
+ 'Time' => TIME_TO_S
+ }
+
class EmptyArraySerializer < Array
def use(arg)
self
end
end
def initialize(custom_serializers = {})
- @map = {}
- add('DateTime') do |dt|
- dt.strftime('%Y-%m-%dT%H:%M:%S.') + ("%06d" % (dt.sec_fraction / NANOSECONDS_IN_DAY / 1000)) + dt.strftime('%z')
- end
- add('ActiveSupport::TimeWithZone') do |tz|
- tz.strftime('%Y-%m-%dT%H:%M:%S.') + ("%06d" % tz.usec) + tz.strftime('%z')
- end
- add('Time') do |t|
- t.strftime('%Y-%m-%dT%H:%M:%S.') + ("%06d" % t.usec) + t.strftime('%z')
- end
+ @map = DEFAULT_MAP.dup
@map.merge!(custom_serializers)
end
@@ -31,7 +36,7 @@ def add(clazz, &block)
def new(resource)
if resource.respond_to?(:model)
model = resource.model
- elsif resource.is_a? Array
+ elsif resource.respond_to? :collect
if resource.empty?
return EmptyArraySerializer.new
else
@@ -41,10 +46,18 @@ def new(resource)
else
model = resource.class
end
- ser = Object.const_get("#{model}Serializer").new(resource)
+ ser = const_retrieve("#{model}Serializer").new(resource)
ser.add_custom_serializers(@map)
ser
end
+
+ def const_retrieve( const )
+ obj = Object
+ const.split(/::/).each do |part|
+ obj = obj.const_get( part )
+ end
+ obj
+ end
end
end
end

0 comments on commit 48ea2a5

Please sign in to comment.