module AgnosticBackend::Indexable::ClassMethods

Public Instance Methods

_index_content_managers() click to toggle source
# File lib/agnostic_backend/indexable/indexable.rb, line 35
def _index_content_managers
  @__index_content_managers ||= {}
end
_index_root_notifiers() click to toggle source
# File lib/agnostic_backend/indexable/indexable.rb, line 43
def _index_root_notifiers
  @__index_root_notifiers ||= {}
end
create_index() click to toggle source
# File lib/agnostic_backend/indexable/indexable.rb, line 21
def create_index
  AgnosticBackend::Indexable::Config.create_index_for(self)
end
create_indices(include_primary: true) click to toggle source
# File lib/agnostic_backend/indexable/indexable.rb, line 25
def create_indices(include_primary: true)
  AgnosticBackend::Indexable::Config.create_indices_for(self,
                                                        include_primary: include_primary)
end
define_index_fields(owner: nil, &block) click to toggle source

specifies which fields should be indexed for a given index_name also sets up the manager for the specified index_name

# File lib/agnostic_backend/indexable/indexable.rb, line 71
def define_index_fields(owner: nil, &block)
  return unless block_given?
  _index_content_managers[index_name(owner)] ||= ContentManager.new
  _index_content_managers[index_name(owner)].add_definitions &block
  unless instance_methods(false).include? :_index_content_managers
    define_method(:_index_content_managers) { self.class._index_content_managers }
  end
end
define_index_notifier(target: nil, &block) click to toggle source

specifies who should be notified when this object is saved

# File lib/agnostic_backend/indexable/indexable.rb, line 81
def define_index_notifier(target: nil, &block)
  return unless block_given?
  _index_root_notifiers[index_name(target)] = block
  unless instance_methods(false).include? :_index_root_notifiers
    define_method(:_index_root_notifiers) { self.class._index_root_notifiers }
  end
end
index_content_manager(index_name) click to toggle source
# File lib/agnostic_backend/indexable/indexable.rb, line 39
def index_content_manager(index_name)
  _index_content_managers[index_name.to_s]
end
index_name(source=nil) click to toggle source

establishes the convention for determining the index name from the class name

# File lib/agnostic_backend/indexable/indexable.rb, line 31
def index_name(source=nil)
  (source.nil? ? name : source.to_s).split('::').last.underscore.pluralize
end
index_root_notifier(index_name) click to toggle source
# File lib/agnostic_backend/indexable/indexable.rb, line 47
def index_root_notifier(index_name)
  _index_root_notifiers[index_name.to_s]
end
schema(for_index: nil) { |type| ... } click to toggle source
# File lib/agnostic_backend/indexable/indexable.rb, line 51
def schema(for_index: nil, &block)
  index_name = for_index.nil? ? self.index_name : for_index
  manager = index_content_manager(index_name)
  raise "Index #{index_name} has not been defined for #{name}" if manager.nil?
  kv_pairs = manager.contents.map do |field_name, field|
    schema =
        if field.type.nested?
          field.from.map { |klass| klass.schema(for_index: index_name, &block) }.reduce(&:merge)
        elsif block_given?
          yield field.type
        else
          field.type.type
        end
    [field_name, schema]
  end
  Hash[kv_pairs]
end