class Babel::Factory def initialize(options = {}) @options = options end def serializer(model_class, serializer_class = nil) @serializers[model_class] = serializer_class if serializer_class @serializers[model_class] ||= ("#{model_class}Serializer".to_const rescue Babel::Serializer) end def deserializer(model_class, deserializer_class = nil) @deserializers[model_class] = deserializer_class if deserializer_class @serializers[model_class] ||= Deserializer.new(model_class) end def to_json(model_or_models, options = {}) if model_or_models.is_a? Array json = model_or_models.collect do |m| serializer(m.class).new(m).to_json(@options.merge(options)) end "[#{json.join(',')}]" else serializer(model_or_models.class).new(model_or_models).to_json(@options.merge(options)) end end end