Sha256: fd3deef0a4086c52104f5c6efcd2a41768c441a6821452f40e60f7decaca7fb5

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

module Shogun
  module Denormalizer
    META_KEY = "meta"
    LINKS_KEY = "links"
    LINKED_KEY = "linked"

    def initialize(object:, fields:)
      @object = object
      @fields = fields
      @meta = object.meta
      @links = object.links
      @linked = object.linked
    end

    def as_document
      {
        document_key => document,
        meta_key => @meta,
        links_key => @links,
        linked_key => @linked
      }
    end

    private def document
      if @fields.one?
        @object.public_send(@fields.first)
      else
        @fields.inject({}) do |hash, field|
          hash.merge(field => @object.public_send(mapping[field]))
        end
      end
    end

    private def document_key
      self.class.const_get("DOCUMENT_KEY")
    end

    private def meta_key
      self.class.const_get("META_KEY")
    end

    private def links_key
      self.class.const_get("LINKS_KEY")
    end

    private def linked_key
      self.class.const_get("LINKED_KEY")
    end

    private def mapping
      self.class.const_get("MAPPING")
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shogun-2.1.1 lib/shogun/denormalizer.rb
shogun-2.1.0 lib/shogun/denormalizer.rb
shogun-2.0.0 lib/shogun/denormalizer.rb
shogun-1.0.0 lib/shogun/denormalizer.rb