Sha256: 497d6c2ab5d1c2913d162af6eb193be811b343573fbcb6052b1a356eed42fa57
Contents?: true
Size: 1.44 KB
Versions: 2
Compression:
Stored size: 1.44 KB
Contents
# frozen_string_literal: true module Esse class LazyDocumentHeader def self.coerce_each(values) arr = [] Array(values).map do |value| instance = coerce(value) arr << instance if instance&.valid? end arr end def self.coerce(value) return unless value if value.is_a?(Esse::LazyDocumentHeader) value elsif value.is_a?(Esse::Document) new(value.doc_header) elsif value.is_a?(Hash) resp = value.transform_keys do |key| case key when :_id, :id, '_id', 'id' :_id when :_routing, :routing, '_routing', 'routing' :_routing when :_type, :type, '_type', 'type' :_type else key.to_sym end end new(resp) elsif String === value || Integer === value new(_id: value) end end def initialize(attributes) @attributes = attributes end def valid? !@attributes[:_id].nil? end def to_h @attributes end def id @attributes.fetch(:_id) end def type @attributes[:_type] end def routing @attributes[:_routing] end def to_doc(source = {}) HashDocument.new(source.merge(@attributes)) end def eql?(other) self.class == other.class && @attributes == other.instance_variable_get(:@attributes) end alias_method :==, :eql? end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
esse-0.3.1 | lib/esse/lazy_document_header.rb |
esse-0.3.0 | lib/esse/lazy_document_header.rb |