Class: Fixjour::Builder

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

Instance Method Summary collapse

Constructor Details

#initialize(model, options, &block) ⇒ Builder

Returns a new instance of Builder.



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

def initialize(model, options, &block)
  @model, @options, @block = model, options, block
end

Instance Method Details

#defineObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fixjour/builder.rb', line 7

def define
  block = @block
  klass = find_class
  klass_name = klass.model_name.singular

  Fixjour.module_eval do
    define_method("new_" + klass_name) do |*overrides|
      instance = klass.new
      block.call(instance)
      overrides.first && overrides.first.each do |key, val|
        instance.send("#{key}=", val)
      end
      instance
    end

    define_method("create_" + klass_name) do |*overrides|
      instance = send("new_#{klass_name}", *overrides)
      instance.save!
      instance
    end
  end
end