Class: Ixtlan::Babel::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/ixtlan/babel/factory.rb

Constant Summary collapse

NANOSECONDS_IN_DAY =
86400*10**6
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 ) )[0..6] + dt.strftime('%z')
end
DATE_TO_S =
Proc.new do |d|
  d.strftime('%Y-%m-%d' )
end
DEFAULT_MAP =
{
  'DateTime' => DATE_TIME_TO_S,
  'Date' => DATE_TO_S,
  'ActiveSupport::TimeWithZone' => TIME_TO_S,
  'Time' => TIME_TO_S
}

Instance Method Summary collapse

Constructor Details

#initialize(custom_serializers = {}) ⇒ Factory

Returns a new instance of Factory.



29
30
31
32
# File 'lib/ixtlan/babel/factory.rb', line 29

def initialize( custom_serializers = {} )
  @map = DEFAULT_MAP.dup
  @map.merge!( custom_serializers )
end

Instance Method Details

#serializer(resource, context = nil) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/ixtlan/babel/factory.rb', line 34

def serializer( resource, context = nil )
  if model = to_model( resource )
    clazz = '::' + model.to_s
    to_class( clazz, 'Serializer', context ).new( resource, @map )
  else
    []
  end
end