Module: AgnosticBackend::Indexable::ClassMethods

Defined in:
lib/agnostic_backend/indexable/indexable.rb

Instance Method Summary collapse

Instance Method Details

#_index_content_managersObject



35
36
37
# File 'lib/agnostic_backend/indexable/indexable.rb', line 35

def _index_content_managers
  @__index_content_managers ||= {}
end

#_index_root_notifiersObject



43
44
45
# File 'lib/agnostic_backend/indexable/indexable.rb', line 43

def _index_root_notifiers
  @__index_root_notifiers ||= {}
end

#create_indexObject



21
22
23
# 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) ⇒ Object



25
26
27
28
# 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) ⇒ Object

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



71
72
73
74
75
76
77
78
# 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) ⇒ Object

specifies who should be notified when this object is saved



81
82
83
84
85
86
87
# 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) ⇒ Object



39
40
41
# 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) ⇒ Object

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



31
32
33
# 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) ⇒ Object



47
48
49
# 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, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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