Module: AgnosticBackend::Indexable::ClassMethods
- Defined in:
- lib/agnostic_backend/indexable/indexable.rb
Instance Method Summary collapse
- #_index_content_managers ⇒ Object
- #_index_root_notifiers ⇒ Object
- #create_index ⇒ Object
-
#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.
-
#define_index_notifier(target: nil, &block) ⇒ Object
specifies who should be notified when this object is saved.
- #index_content_manager(index_name) ⇒ Object
-
#index_name(source = nil) ⇒ Object
establishes the convention for determining the index name from the class name.
- #index_root_notifier(index_name) ⇒ Object
- #schema(for_index: nil, &block) ⇒ Object
Instance Method Details
#_index_content_managers ⇒ Object
30 31 32 |
# File 'lib/agnostic_backend/indexable/indexable.rb', line 30 def _index_content_managers @__index_content_managers ||= {} end |
#_index_root_notifiers ⇒ Object
38 39 40 |
# File 'lib/agnostic_backend/indexable/indexable.rb', line 38 def _index_root_notifiers @__index_root_notifiers ||= {} end |
#create_index ⇒ Object
21 22 23 |
# File 'lib/agnostic_backend/indexable/indexable.rb', line 21 def create_index AgnosticBackend::Indexable::Config.create_index_for(self) 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
66 67 68 69 70 71 72 73 |
# File 'lib/agnostic_backend/indexable/indexable.rb', line 66 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
76 77 78 79 80 81 82 |
# File 'lib/agnostic_backend/indexable/indexable.rb', line 76 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
34 35 36 |
# File 'lib/agnostic_backend/indexable/indexable.rb', line 34 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
26 27 28 |
# File 'lib/agnostic_backend/indexable/indexable.rb', line 26 def index_name(source=nil) (source.nil? ? name : source.to_s).split('::').last.underscore.pluralize end |
#index_root_notifier(index_name) ⇒ Object
42 43 44 |
# File 'lib/agnostic_backend/indexable/indexable.rb', line 42 def index_root_notifier(index_name) _index_root_notifiers[index_name.to_s] end |
#schema(for_index: nil, &block) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/agnostic_backend/indexable/indexable.rb', line 46 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 |