Class: Fixjour::Generator

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

Overview

This generates a new instance of a model object for the new_ method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, block) ⇒ Generator

Returns a new instance of Generator.



7
8
9
# File 'lib/fixjour/generator.rb', line 7

def initialize(klass, block)
  @klass, @block = klass, block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



5
6
7
# File 'lib/fixjour/generator.rb', line 5

def block
  @block
end

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/fixjour/generator.rb', line 5

def klass
  @klass
end

Instance Method Details

#args(overrides) ⇒ Object



20
21
22
23
24
25
# File 'lib/fixjour/generator.rb', line 20

def args(overrides)
  case block.arity
  when 1 then [MergingProxy.new(klass, overrides)]
  when 2 then [MergingProxy.new(klass, overrides), overrides]
  end
end

#call(context, overrides = {}) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/fixjour/generator.rb', line 11

def call(context, overrides={})
  overrides = OverridesHash.new(overrides)
  result = block.bind(context).call(*args(overrides))
  case result
  when Hash then klass.new(result.merge(overrides))
  else result
  end
end