Sha256: b400222518fbd09c959efa990d317333874598aad0e81f832325425a014d4412

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

module Representable::Hash
  module Collection
    include Representable::Hash

    def self.included(base)
      base.class_eval do
        include Representable
        extend ClassMethods
        extend Representable::Hash::ClassMethods # ::representer_engine.
      end
    end


    module ClassMethods
      def items(options, &block)
        collection(:_self, options.merge(:getter => lambda { |*| self }), &block)
      end
    end


    def create_representation_with(doc, options, format)
      bin   = representable_mapper(format, options).bindings.first
      bin.write(doc, represented)
    end

    def update_properties_from(doc, options, format)
      bin   = representable_mapper(format, options).bindings.first
      value = bin.deserialize_from(doc)
      represented.replace(value)
    end

    # FIXME: refactor Definition so we can simply add options in #items to existing definition.
    def representable_attrs
      attrs = super
      attrs << Definition.new(:_self, :collection => true) if attrs.size == 0
      attrs
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
representable-1.8.5 lib/representable/hash/collection.rb
representable-1.8.4 lib/representable/hash/collection.rb
representable-1.8.3 lib/representable/hash/collection.rb