Sha256: 1f3d87ca0e9bba4dfbf9fb748bf78506edb5437c17d2408d9ae73c4eeeeb04c6

Contents?: true

Size: 1.5 KB

Versions: 9

Compression:

Stored size: 1.5 KB

Contents

require 'representable'
require 'representable/hash/binding'

module Representable
  # The generic representer. Brings #to_hash and #from_hash to your object.
  # If you plan to write your own representer for a new media type, try to use this module (e.g., check how JSON reuses Hash's internal
  # architecture).
  module Hash
    def self.included(base)
      base.class_eval do
        include Representable # either in Hero or HeroRepresentation.
        extend ClassMethods
        register_feature Representable::Hash
      end
    end

    module ClassMethods
      def format_engine
        Representable::Hash
      end

      def collection_representer_class
        Collection
      end
    end

    def from_hash(data, options={}, binding_builder=Binding)
      data = filter_wrap(data, options)

      update_properties_from(data, options, binding_builder)
    end

    def to_hash(options={}, binding_builder=Binding)
      hash = create_representation_with({}, options, binding_builder)

      return hash if options[:wrap] == false
      return hash unless wrap = options[:wrap] || representation_wrap(options)

      {wrap => hash}
    end

  private
    def filter_wrap(data, options)
      return data if options[:wrap] == false
      return data unless wrap = options[:wrap] || representation_wrap(options)
      filter_wrap_for(data, wrap)
    end

    def filter_wrap_for(data, wrap)
      data[wrap.to_s] || {} # DISCUSS: don't initialize this more than once. # TODO: this should be done with #read.
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
representable-3.0.1 lib/representable/hash.rb
representable-3.0.0 lib/representable/hash.rb
representable-2.4.1 lib/representable/hash.rb
representable-2.4.0 lib/representable/hash.rb
representable-2.4.0.rc5 lib/representable/hash.rb
representable-2.4.0.rc4 lib/representable/hash.rb
representable-2.4.0.rc3 lib/representable/hash.rb
representable-2.4.0.rc2 lib/representable/hash.rb
representable-2.4.0.rc1 lib/representable/hash.rb