Class: Forge::Definition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, &block) ⇒ Definition



59
60
61
62
# File 'lib/forge.rb', line 59

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

Instance Attribute Details

#blockObject

Returns the value of attribute block.



57
58
59
# File 'lib/forge.rb', line 57

def block
  @block
end

#klassObject

Returns the value of attribute klass.



57
58
59
# File 'lib/forge.rb', line 57

def klass
  @klass
end

Instance Method Details

#build(attrs = {}) ⇒ Object



64
65
66
67
68
# File 'lib/forge.rb', line 64

def build(attrs={})
  instance = @klass.new.tap { |o| @block.call(o) }
  attrs.each { |k,v| instance.public_send(:"#{k}=", v) }
  instance
end

#create(attrs = {}) ⇒ Object



70
71
72
# File 'lib/forge.rb', line 70

def create(attrs={})
  build(attrs).tap { |o| o.save }
end