Sha256: 6755c7f97d6f044030c3eb7a9828aa86d8d1c6ecd7fc6ade9a6c6de957d781ef

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

require 'delegate'

module Representable
  # The Binding wraps the Definition instance for this property and provides methods to read/write fragments.
  class Binding < SimpleDelegator
    class FragmentNotFound
    end
    
    def definition  # TODO: remove in 1.4.
      raise "Binding#definition is no longer supported as all Definition methods are now delegated automatically."
    end
    
    # Main entry point for rendering/parsing a property object.
    def serialize(value)
      value
    end
    
    def deserialize(fragment)
      fragment
    end
    
    
    # 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 = 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
        sought_type.new
      end
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
representable-1.2.8 lib/representable/binding.rb