Sha256: 68207e22e2d21a1e8140d8c78ad033bb93b5fb2fd0c0d0e795c0f8ca06f6ffdb

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

module Representable
  class Binding
    class FragmentNotFound
    end
    
    
    attr_reader :definition # TODO: merge Binding and Definition.
  
    def initialize(definition)
      @definition = definition
    end
    
    # Main entry point for rendering/parsing a property object.
    module Hooks
      def serialize(value)
        value
      end
      
      def deserialize(fragment)
        fragment
      end
    end
    
    include Hooks
    
    
    # Hooks into #serialize and #deserialize to extend typed properties
    # at runtime.
    module Extend
      # Extends the object with its representer before serialization.
      def serialize(object)
        extend_for(super)
      end
      
      def deserialize(*)
        extend_for(super)
      end
      
      def extend_for(object)
        if mod = definition.representer_module
          object.extend(*mod)
        end
        
        object
      end
    end
    
    module Object
      include Binding::Extend  # provides #serialize/#deserialize with extend.
      
      def serialize(object)
        return object if object.nil?
        
        super(object).send(serialize_method, :wrap => false)
      end
      
      def deserialize(data) 
        # DISCUSS: does it make sense to skip deserialization of nil-values here?
        super(create_object).send(deserialize_method, data)
      end
      
      def create_object
        definition.sought_type.new
      end
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
representable-1.2.5 lib/representable/binding.rb
representable-1.2.4 lib/representable/binding.rb