Class: Forge::Definition
- Inherits:
-
Object
- Object
- Forge::Definition
- Defined in:
- lib/forge.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
-
#klass ⇒ Object
Returns the value of attribute klass.
Instance Method Summary collapse
- #build(attrs = {}) ⇒ Object
- #create(attrs = {}) ⇒ Object
-
#initialize(klass, &block) ⇒ Definition
constructor
A new instance of Definition.
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
#block ⇒ Object
Returns the value of attribute block.
57 58 59 |
# File 'lib/forge.rb', line 57 def block @block end |
#klass ⇒ Object
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 |