Class: ElasticRecord::SearchesMany::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_record/searches_many/builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, options) ⇒ Builder

Returns a new instance of Builder.



9
10
11
# File 'lib/elastic_record/searches_many/builder.rb', line 9

def initialize(model, name, options)
  @model, @name, @options = model, name, options
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



8
9
10
# File 'lib/elastic_record/searches_many/builder.rb', line 8

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/elastic_record/searches_many/builder.rb', line 8

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/elastic_record/searches_many/builder.rb', line 8

def options
  @options
end

Class Method Details

.build(model, name, options) ⇒ Object



4
5
6
# File 'lib/elastic_record/searches_many/builder.rb', line 4

def self.build(model, name, options)
  new(model, name, options).build
end

Instance Method Details

#buildObject



13
14
15
16
17
18
19
20
21
# File 'lib/elastic_record/searches_many/builder.rb', line 13

def build        
  define_writer
  define_reader

  reflection = ElasticRecord::SearchesMany::Reflection.new(model, name, options)
  model.searches_many_reflections = model.searches_many_reflections.merge(name => reflection)

  model.add_autosave_callbacks(reflection) # if options[:autosave]
end

#define_readerObject



34
35
36
37
38
39
# File 'lib/elastic_record/searches_many/builder.rb', line 34

def define_reader
  name = self.name
  mixin.redefine_method(name) do
    searches_many_association(name).reader
  end
end

#define_writerObject



27
28
29
30
31
32
# File 'lib/elastic_record/searches_many/builder.rb', line 27

def define_writer
  name = self.name
  mixin.redefine_method("#{name}=") do |records|
    searches_many_association(name).writer(records)
  end
end

#mixinObject



23
24
25
# File 'lib/elastic_record/searches_many/builder.rb', line 23

def mixin
  model.generated_searches_many_methods
end