Class: AgnosticBackend::Indexable::ContentManager

Inherits:
Object
  • Object
show all
Defined in:
lib/agnostic_backend/indexable/content_manager.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, **kwargs) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/agnostic_backend/indexable/content_manager.rb', line 15

def method_missing(sym, *args, **kwargs)
  if FieldType.exists? sym
    kwargs[:type] = sym
    field(*args, **kwargs)
  else
    super
  end
end

Instance Method Details

#add_definitions(&block) ⇒ Object



6
7
8
9
# File 'lib/agnostic_backend/indexable/content_manager.rb', line 6

def add_definitions &block
  return unless block_given?
  instance_eval &block
end

#contentsObject



11
12
13
# File 'lib/agnostic_backend/indexable/content_manager.rb', line 11

def contents
  @contents ||= {}
end

#extract_contents_from(object, index_name, observer:) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/agnostic_backend/indexable/content_manager.rb', line 33

def extract_contents_from(object, index_name, observer:)
  kv_pairs = contents.map do |field_name, field|
    field_value = field.evaluate(context: object)
    if field.type.nested?
      if field_value.respond_to? :generate_document
        observer.add(field_value)
        field_value = field_value.generate_document(for_index: index_name, observer: observer)
      elsif field_value.present?
        field_name = nil
      end
    end
    [field_name, field_value]
  end
  kv_pairs.reject! { |attr_name, _| attr_name.nil? }
  Hash[kv_pairs]
end

#field(field_name, value: nil, type:, from: nil, **options) ⇒ Object



28
29
30
31
# File 'lib/agnostic_backend/indexable/content_manager.rb', line 28

def field(field_name, value: nil, type:, from: nil, **options)
  contents[field_name.to_s] = Field.new(value.present? ? value : field_name, type,
                                        from: from, **options)
end

#respond_to?(sym, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/agnostic_backend/indexable/content_manager.rb', line 24

def respond_to?(sym, include_private=false)
  FieldType.exists?(sym) || super
end