module Gorillib::Model

Public Instance Methods

congruent?(klass, *others) click to toggle source
# File lib/gorillib/model/elasticsearch_ext.rb, line 76
def congruent?(klass, *others)
  others.any?{ |o| o.ancestors.include? klass }
end
define_collection_single_receiver(field) click to toggle source
# File lib/gorillib/model/elasticsearch_ext.rb, line 62
def define_collection_single_receiver field
  collection_single_field_name = field.singular_name
  field_type                   = field.item_type
  define_meta_module_method("receive_#{collection_single_field_name}", true) do |attrs, &block|
    begin
      field_type.receive(attrs, &block)
    rescue StandardError => err ; err.polish("#{self.class}.#{field_name} type #{type} on #{val}") rescue nil ; raise ; end
  end
end
receive_as_type(factory, params) click to toggle source
# File lib/gorillib/model/elasticsearch_ext.rb, line 80
def receive_as_type(factory, params)
  products = Array[factory.try(:product) || factory].flatten
  case 
  when congruent?(Integer, *products)
    EsInteger.receive(params)
  when congruent?(Float, *products)
    EsFloat.receive(params)
  when congruent?(Date, *products) || congruent?(Time, *products)
    EsDate.receive(params)
  when congruent?(TrueClass, *products) || congruent?(FalseClass, *products)
    EsBoolean.receive(params)
  when congruent?(Array, *products)
    receive_as_type(factory.items_factory, params)
  else
    EsString.receive(params)
  end
end
to_mapping() click to toggle source
# File lib/gorillib/model/elasticsearch_ext.rb, line 52
def to_mapping
  { 
    properties: fields.inject({}) do |mapping, (name, field)|
      info = field.type.respond_to?(:to_mapping) ? field.type.to_mapping : field.to_mapping
      mapping[name] = info
      mapping
    end
  }
end