Sha256: c377b69d10dfde4a7bef884b771aa630b3019c87c1ec712f10b92e6d6727aff0

Contents?: true

Size: 1.04 KB

Versions: 16

Compression:

Stored size: 1.04 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
      {
        namespace => 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 namespace
      self.class.const_get("NAMESPACE")
    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

16 entries across 16 versions & 1 rubygems

Version Path
shogun-6.4.2 lib/shogun/denormalizer.rb
shogun-6.4.1 lib/shogun/denormalizer.rb
shogun-6.4.0 lib/shogun/denormalizer.rb
shogun-6.3.0 lib/shogun/denormalizer.rb
shogun-6.2.3 lib/shogun/denormalizer.rb
shogun-6.2.2 lib/shogun/denormalizer.rb
shogun-6.2.1 lib/shogun/denormalizer.rb
shogun-6.2.0 lib/shogun/denormalizer.rb
shogun-6.1.0 lib/shogun/denormalizer.rb
shogun-6.0.0 lib/shogun/denormalizer.rb
shogun-4.0.1 lib/shogun/denormalizer.rb
shogun-4.0.0 lib/shogun/denormalizer.rb
shogun-3.4.0 lib/shogun/denormalizer.rb
shogun-3.2.0 lib/shogun/denormalizer.rb
shogun-3.1.0 lib/shogun/denormalizer.rb
shogun-3.0.0 lib/shogun/denormalizer.rb