Sha256: 3454789bcfd64e15dc7f80b6c738accff58489fdd1da5ec70fa356f290bb3f05

Contents?: true

Size: 1.1 KB

Versions: 5

Compression:

Stored size: 1.1 KB

Contents

module Representable
  module HashMethods
    def create_representation_with(doc, options, format)
      hash  = filter_keys_for!(represented, options) # FIXME: this modifies options and replicates logic from Representable.
      bin   = representable_mapper(format, options).bindings(represented, options).first

      bin.render_fragment(hash, doc) # TODO: Use something along Populator, which does
    end

    def update_properties_from(doc, options, format)
      hash  = filter_keys_for!(doc, options)
      bin   = representable_mapper(format, options).bindings(represented, options).first

      value = Deserializer::Hash.new(bin).call(hash)
      # value = bin.deserialize_from(hash)
      represented.replace(value)
    end

  private
    def filter_keys_for!(hash, options)
      excluding = options[:exclude]
      # TODO: use same filtering method as in normal representer in Representable#create_representation_with.
      return hash unless props = options.delete(:exclude) || options.delete(:include)
      hash.reject { |k,v| excluding ? props.include?(k.to_sym) : !props.include?(k.to_sym) }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
representable-2.3.0 lib/representable/hash_methods.rb
representable-2.2.3 lib/representable/hash_methods.rb
representable-2.2.2 lib/representable/hash_methods.rb
representable-2.2.1 lib/representable/hash_methods.rb
representable-2.2.0 lib/representable/hash_methods.rb