Sha256: 9a03d3f0a21f140fdbebddcd57b8dfd8bbe35837322735a765646b137d2c4b58

Contents?: true

Size: 971 Bytes

Versions: 10

Compression:

Stored size: 971 Bytes

Contents

module Representable
  module HashMethods
    # FIXME: refactor Definition so we can simply add options in #items to existing definition.
    def representable_attrs
      attrs = super
      attrs << Definition.new(*definition_opts) if attrs.size == 0
      attrs
    end
    
    def create_representation_with(doc, options, format)
      bin   = representable_bindings_for(format, options).first
      hash  = filter_keys_for(self, options)
      bin.write(doc, hash)
    end
    
    def update_properties_from(doc, options, format)
      bin   = representable_bindings_for(format, options).first
      hash  = filter_keys_for(doc, options)
      value = bin.deserialize_from(hash)
      replace(value)
      self
    end
    
  private
    def filter_keys_for(hash, options)
      return hash unless props = options[:exclude] || options[:include]
      hash.reject { |k,v| options[:exclude] ? props.include?(k.to_sym) : !props.include?(k.to_sym) }
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
representable-1.5.0 lib/representable/hash_methods.rb
representable-1.4.2 lib/representable/hash_methods.rb
representable-1.4.1 lib/representable/hash_methods.rb
representable-1.4.0 lib/representable/hash_methods.rb
representable-1.3.5 lib/representable/hash_methods.rb
representable-1.3.4 lib/representable/hash_methods.rb
representable-1.3.3 lib/representable/hash_methods.rb
representable-1.3.2 lib/representable/hash_methods.rb
representable-1.3.1 lib/representable/hash_methods.rb
representable-1.3.0 lib/representable/hash_methods.rb