-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ActiveAttr::BlockInitializations, closes #33
- Loading branch information
Showing
4 changed files
with
107 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require "active_attr/chainable_initialization" | ||
require "active_support/concern" | ||
|
||
module ActiveAttr | ||
# BlockInitialization allows you to build an instance in a block | ||
# | ||
# Including the BlockInitialization module into your class will yield the | ||
# model instance to a block passed to when creating a new instance. | ||
# | ||
# @example Usage | ||
# class Person | ||
# include ActiveAttr::BlockInitialization | ||
# end | ||
# | ||
# @since 0.3.0 | ||
module BlockInitialization | ||
extend ActiveSupport::Concern | ||
include ChainableInitialization | ||
|
||
# Initialize a model and build via a block | ||
# | ||
# @example | ||
# person = Person.new do |p| | ||
# p.first_name = "Chris" | ||
# p.last_name = "Griego" | ||
# end | ||
# | ||
# person.first_name #=> "Chris" | ||
# person.last_name #=> "Griego" | ||
# | ||
# @since 0.3.0 | ||
def initialize(*) | ||
super | ||
yield self if block_given? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require "spec_helper" | ||
require "active_attr/block_initialization" | ||
|
||
module ActiveAttr | ||
describe BlockInitialization do | ||
subject { model_class.new("arg") {} } | ||
|
||
let :model_class do | ||
Class.new do | ||
include InitializationVerifier | ||
include BlockInitialization | ||
|
||
def initialize(arg) | ||
super | ||
end | ||
end | ||
end | ||
|
||
describe "#initialize" do | ||
it "invokes the superclass initializer" do | ||
should be_initialized | ||
end | ||
|
||
it "doesn't raise when not passed a block" do | ||
expect { model_class.new("arg") }.not_to raise_error | ||
end | ||
|
||
it "yields the new instance" do | ||
yielded_instance = nil | ||
|
||
returned_instance = model_class.new("arg") do |yielded| | ||
yielded_instance = yielded | ||
end | ||
|
||
returned_instance.should equal yielded_instance | ||
end | ||
end | ||
end | ||
end |