Sha256: aecb0dd8faa716f2a91be40626a3685ac38d0710f9520b4a6ee5cf81587820c8

Contents?: true

Size: 901 Bytes

Versions: 3

Compression:

Stored size: 901 Bytes

Contents

module SearchKit
  module Models
    class Document
      class AttributeNotFound < StandardError; end

      include Virtus.model

      attribute :id
      attribute :source
      attribute :score

      def initialize(document_data = {})
        super(
          source: document_data.fetch(:source, {}),
          id:     document_data.fetch(:id, nil),
          score:  document_data.fetch(:score, nil)
        )
      end

      def get(field)
        source.fetch(field.to_sym)
      rescue KeyError
        fail AttributeNotFound, field
      end

      def respond_to_missing?(method_name, include_private = false)
        source.has_key?(method_name) || super(method_name, include_private)
      end

      def method_missing(method_name, *arguments, &block)
        get(method_name)
      rescue AttributeNotFound
        super(method_name, *arguments, &block)
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
search-kit-0.0.10 lib/search_kit/models/document.rb
search-kit-0.0.9 lib/search_kit/models/document.rb
search-kit-0.0.8 lib/search_kit/models/document.rb