Sha256: 5c081ca96f576aa9d22b3eb3b42b18a5c4e4e6a45a5994b65c82fe6a0fa3e36a

Contents?: true

Size: 872 Bytes

Versions: 3

Compression:

Stored size: 872 Bytes

Contents

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vellam-0.1.2 lib/babel/factory.rb~
vellam-0.1.1 lib/babel/factory.rb~
vellam-0.1 lib/babel/factory.rb~