Class: Knish::Builder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection_name, &block) ⇒ Builder

Returns a new instance of Builder.



5
6
7
8
# File 'lib/knish/builder.rb', line 5

def initialize(collection_name, &block)
  @collection_name = collection_name
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



3
4
5
# File 'lib/knish/builder.rb', line 3

def block
  @block
end

#collection_nameObject (readonly)

Returns the value of attribute collection_name.



3
4
5
# File 'lib/knish/builder.rb', line 3

def collection_name
  @collection_name
end

Instance Method Details

#add_collections(klass, collections) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/knish/builder.rb', line 19

def add_collections(klass, collections)
  collections.each do |collection|
    klass.class_eval <<-RUBY, __FILE__, __LINE__
      def #{collection}
        @#{collection} ||= Collection.new('#{collection}', config)
      end
    RUBY
  end
end

#configObject



29
30
31
32
33
# File 'lib/knish/builder.rb', line 29

def config
  model_config = ModelConfig.new(Knish.config.clone, collection_name)
  block.call(model_config)
  model_config
end

#make_modelObject



10
11
12
13
14
15
16
17
# File 'lib/knish/builder.rb', line 10

def make_model
  klass = Class.new(Model)
  klass.config = config
  klass.send(:attr_accessor, *config.all_attributes)
  klass.send(:attr_writer, *config.collections)
  add_collections(klass, config.collections)
  klass
end