Skip to content

Commit

Permalink
Use Ruby 1.9's stabby lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Apr 20, 2012
1 parent dc81590 commit 5b9aa48
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion lib/factory_girl/attribute.rb
Expand Up @@ -15,7 +15,7 @@ def initialize(name, ignored)
end

def to_proc
lambda { }
-> { }
end

def association?
Expand Down
2 changes: 1 addition & 1 deletion lib/factory_girl/attribute/association.rb
Expand Up @@ -12,7 +12,7 @@ def initialize(name, factory, overrides)
def to_proc
factory = @factory
overrides = @overrides
lambda { association(factory, overrides) }
-> { association(factory, overrides) }
end

def association?
Expand Down
2 changes: 1 addition & 1 deletion lib/factory_girl/attribute/dynamic.rb
Expand Up @@ -9,7 +9,7 @@ def initialize(name, ignored, block)
def to_proc
block = @block

lambda {
-> {
value = block.arity == 1 ? block.call(self) : instance_exec(&block)
raise SequenceAbuseError if FactoryGirl::Sequence === value
value
Expand Down
2 changes: 1 addition & 1 deletion lib/factory_girl/attribute/sequence.rb
Expand Up @@ -9,7 +9,7 @@ def initialize(name, sequence, ignored)

def to_proc
sequence = @sequence
lambda { FactoryGirl.generate(sequence) }
-> { FactoryGirl.generate(sequence) }
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/factory_girl/attribute/static.rb
Expand Up @@ -8,7 +8,7 @@ def initialize(name, value, ignored)

def to_proc
value = @value
lambda { value }
-> { value }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/factory_girl/definition.rb
Expand Up @@ -6,7 +6,7 @@ def initialize(name = nil, base_traits = [])
@declarations = DeclarationList.new(name)
@callbacks = []
@defined_traits = []
@to_create = lambda {|instance| instance.save! }
@to_create = ->(instance) { instance.save! }
@base_traits = base_traits
@additional_traits = []
@constructor = nil
Expand Down
2 changes: 1 addition & 1 deletion lib/factory_girl/evaluator.rb
Expand Up @@ -23,7 +23,7 @@ def initialize(build_strategy, overrides = {})
@cached_attributes = overrides

@overrides.each do |name, value|
singleton_class.send :define_method, name, lambda { value }
singleton_class.send :define_method, name, -> { value }
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/factory_girl/factory.rb
Expand Up @@ -27,7 +27,7 @@ def build_class #:nodoc:
end

def run(strategy_class, overrides, &block) #:nodoc:
block ||= lambda {|result| result }
block ||= ->(result) { result }
compile

strategy = strategy_class.new
Expand Down Expand Up @@ -136,7 +136,7 @@ def parent

def instance_builder
build_class = self.build_class
constructor || lambda { build_class.new }
constructor || -> { build_class.new }
end

def initialize_copy(source)
Expand Down
12 changes: 6 additions & 6 deletions spec/factory_girl/attribute/dynamic_spec.rb
Expand Up @@ -2,30 +2,30 @@

describe FactoryGirl::Attribute::Dynamic do
let(:name) { :first_name }
let(:block) { lambda { } }
let(:block) { -> { } }

subject { FactoryGirl::Attribute::Dynamic.new(name, false, block) }

its(:name) { should == name }

context "with a block returning a static value" do
let(:block) { lambda { "value" } }
let(:block) { -> { "value" } }

it "returns the value when executing the proc" do
subject.to_proc.call.should == "value"
end
end

context "with a block returning its block-level variable" do
let(:block) { lambda {|thing| thing } }
let(:block) { ->(thing) { thing } }

it "returns self when executing the proc" do
subject.to_proc.call.should == subject
end
end

context "with a block referencing an attribute on the attribute" do
let(:block) { lambda { attribute_defined_on_attribute } }
let(:block) { -> { attribute_defined_on_attribute } }
let(:result) { "other attribute value" }

before do
Expand All @@ -38,7 +38,7 @@
end

context "with a block returning a sequence" do
let(:block) { lambda { FactoryGirl.register_sequence(FactoryGirl::Sequence.new(:email, 1) {|n| "foo#{n}" }) } }
let(:block) { -> { FactoryGirl.register_sequence(FactoryGirl::Sequence.new(:email, 1) {|n| "foo#{n}" }) } }

it "raises a sequence abuse error" do
expect { subject.to_proc.call }.to raise_error(FactoryGirl::SequenceAbuseError)
Expand All @@ -47,6 +47,6 @@
end

describe FactoryGirl::Attribute::Dynamic, "with a string name" do
subject { FactoryGirl::Attribute::Dynamic.new("name", false, lambda { } ) }
subject { FactoryGirl::Attribute::Dynamic.new("name", false, -> { } ) }
its(:name) { should == :name }
end
8 changes: 4 additions & 4 deletions spec/factory_girl/attribute_list_spec.rb
Expand Up @@ -2,7 +2,7 @@

describe FactoryGirl::AttributeList, "#define_attribute" do
let(:static_attribute) { FactoryGirl::Attribute::Static.new(:full_name, "value", false) }
let(:dynamic_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, lambda {|u| "#{u.full_name}@example.com" }) }
let(:dynamic_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, ->(u) { "#{u.full_name}@example.com" }) }

it "maintains a list of attributes" do
subject.define_attribute(static_attribute)
Expand Down Expand Up @@ -42,8 +42,8 @@
describe FactoryGirl::AttributeList, "#apply_attributes" do
let(:full_name_attribute) { FactoryGirl::Attribute::Static.new(:full_name, "John Adams", false) }
let(:city_attribute) { FactoryGirl::Attribute::Static.new(:city, "Boston", false) }
let(:email_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, lambda {|model| "#{model.full_name}@example.com" }) }
let(:login_attribute) { FactoryGirl::Attribute::Dynamic.new(:login, false, lambda {|model| "username-#{model.full_name}" }) }
let(:email_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, ->(model) { "#{model.full_name}@example.com" }) }
let(:login_attribute) { FactoryGirl::Attribute::Dynamic.new(:login, false, ->(model) { "username-#{model.full_name}" }) }

def list(*attributes)
FactoryGirl::AttributeList.new.tap do |list|
Expand All @@ -61,7 +61,7 @@ def list(*attributes)

describe FactoryGirl::AttributeList, "#associations" do
let(:full_name_attribute) { FactoryGirl::Attribute::Static.new(:full_name, "value", false) }
let(:email_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, lambda {|u| "#{u.full_name}@example.com" }) }
let(:email_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, ->(u) { "#{u.full_name}@example.com" }) }
let(:author_attribute) { FactoryGirl::Attribute::Association.new(:author, :user, {}) }
let(:profile_attribute) { FactoryGirl::Attribute::Association.new(:profile, :profile, {}) }

Expand Down
14 changes: 7 additions & 7 deletions spec/factory_girl/callback_spec.rb
Expand Up @@ -2,40 +2,40 @@

describe FactoryGirl::Callback do
it "has a name" do
FactoryGirl::Callback.new(:after_create, lambda {}).name.should == :after_create
FactoryGirl::Callback.new(:after_create, -> {}).name.should == :after_create
end

it "converts strings to symbols" do
FactoryGirl::Callback.new("after_create", lambda {}).name.should == :after_create
FactoryGirl::Callback.new("after_create", -> {}).name.should == :after_create
end

it "runs its block with no parameters" do
ran_with = nil
FactoryGirl::Callback.new(:after_create, lambda { ran_with = [] }).run(:one, :two)
FactoryGirl::Callback.new(:after_create, -> { ran_with = [] }).run(:one, :two)
ran_with.should == []
end

it "runs its block with one parameter" do
ran_with = nil
FactoryGirl::Callback.new(:after_create, lambda { |one| ran_with = [one] }).run(:one, :two)
FactoryGirl::Callback.new(:after_create, ->(one) { ran_with = [one] }).run(:one, :two)
ran_with.should == [:one]
end

it "runs its block with two parameters" do
ran_with = nil
FactoryGirl::Callback.new(:after_create, lambda { |one, two| ran_with = [one, two] }).run(:one, :two)
FactoryGirl::Callback.new(:after_create, ->(one, two) { ran_with = [one, two] }).run(:one, :two)
ran_with.should == [:one, :two]
end

it "allows valid callback names to be assigned" do
FactoryGirl.callback_names.each do |callback_name|
expect { FactoryGirl::Callback.new(callback_name, lambda {}) }.
expect { FactoryGirl::Callback.new(callback_name, -> {}) }.
to_not raise_error(FactoryGirl::InvalidCallbackNameError)
end
end

it "raises if an invalid callback name is assigned" do
expect { FactoryGirl::Callback.new(:magic_fairies, lambda {}) }.
expect { FactoryGirl::Callback.new(:magic_fairies, -> {}) }.
to raise_error(FactoryGirl::InvalidCallbackNameError, /magic_fairies is not a valid callback name/)
end
end
12 changes: 6 additions & 6 deletions spec/factory_girl/definition_proxy_spec.rb
Expand Up @@ -16,7 +16,7 @@
end

it "declares a dynamic attribute on the factory" do
attribute_value = lambda { "dynamic attribute" }
attribute_value = -> { "dynamic attribute" }
proxy.add_attribute(:attribute_name, &attribute_value)
subject.should have_dynamic_declaration(:attribute_name).with_value(attribute_value)
end
Expand All @@ -38,7 +38,7 @@
end

it "declares a dynamic attribute on the factory" do
attribute_value = lambda { "dynamic attribute" }
attribute_value = -> { "dynamic attribute" }
proxy.add_attribute(:attribute_name, &attribute_value)
subject.should have_dynamic_declaration(:attribute_name).ignored.with_value(attribute_value)
end
Expand Down Expand Up @@ -77,7 +77,7 @@
end

it "declares a dynamic attribute" do
attribute_value = lambda { "dynamic attribute" }
attribute_value = -> { "dynamic attribute" }
proxy.attribute_name &attribute_value
subject.should have_dynamic_declaration(:attribute_name).with_value(attribute_value)
end
Expand Down Expand Up @@ -124,7 +124,7 @@
describe FactoryGirl::DefinitionProxy, "adding callbacks" do
subject { FactoryGirl::Definition.new }
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
let(:callback) { lambda { "my awesome callback!" } }
let(:callback) { -> { "my awesome callback!" } }

context "#after_build" do
before { proxy.after_build(&callback) }
Expand All @@ -147,7 +147,7 @@
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }

it "accepts a block to run in place of #save!" do
to_create_block = lambda {|record| record.persist }
to_create_block = ->(record) { record.persist }
proxy.to_create(&to_create_block)
subject.to_create.should == to_create_block
end
Expand All @@ -168,7 +168,7 @@
end

it "with a block" do
child_block = lambda { }
child_block = -> { }
proxy.factory(:child, {}, &child_block)
proxy.child_factories.should include([:child, {}, child_block])
end
Expand Down
8 changes: 4 additions & 4 deletions spec/factory_girl/evaluator_class_definer_spec.rb
@@ -1,9 +1,9 @@
require "spec_helper"

describe FactoryGirl::EvaluatorClassDefiner do
let(:simple_attribute) { stub("simple attribute", name: :simple, to_proc: lambda { 1 }) }
let(:relative_attribute) { stub("relative attribute", name: :relative, to_proc: lambda { simple + 1 }) }
let(:attribute_that_raises_a_second_time) { stub("attribute that would raise without a cache", name: :raises_without_proper_cache, to_proc: lambda { raise "failed" if @run; @run = true; nil }) }
let(:simple_attribute) { stub("simple attribute", name: :simple, to_proc: -> { 1 }) }
let(:relative_attribute) { stub("relative attribute", name: :relative, to_proc: -> { simple + 1 }) }
let(:attribute_that_raises_a_second_time) { stub("attribute that would raise without a cache", name: :raises_without_proper_cache, to_proc: -> { raise "failed" if @run; @run = true; nil }) }

let(:attributes) { [simple_attribute, relative_attribute, attribute_that_raises_a_second_time] }
let(:class_definer) { FactoryGirl::EvaluatorClassDefiner.new(attributes, FactoryGirl::Evaluator) }
Expand Down Expand Up @@ -32,7 +32,7 @@
end

context "with a custom evaluator as a parent class" do
let(:child_attributes) { [stub("child attribute", name: :simple, to_proc: lambda { 1 })] }
let(:child_attributes) { [stub("child attribute", name: :simple, to_proc: -> { 1 })] }
let(:child_definer) { FactoryGirl::EvaluatorClassDefiner.new(child_attributes, class_definer.evaluator_class) }

subject { child_definer.evaluator_class }
Expand Down
6 changes: 3 additions & 3 deletions spec/factory_girl/factory_spec.rb
Expand Up @@ -19,7 +19,7 @@
it "passes a custom creation block" do
strategy = stub("strategy", result: nil, add_observer: true)
FactoryGirl::Strategy::Build.stubs(new: strategy)
block = lambda {}
block = -> {}
factory = FactoryGirl::Factory.new(:object)
factory.to_create(&block)

Expand Down Expand Up @@ -69,7 +69,7 @@
end

it "does not call a lazy attribute block for an overridden attribute" do
declaration = FactoryGirl::Declaration::Dynamic.new(@name, false, lambda { flunk })
declaration = FactoryGirl::Declaration::Dynamic.new(@name, false, -> { flunk })
@factory.declare_attribute(declaration)
@factory.run(FactoryGirl::Strategy::AttributesFor, @hash)
end
Expand Down Expand Up @@ -252,7 +252,7 @@ def self.to_s

it "calls the block and returns the result" do
block_run = nil
block = lambda {|result| block_run = "changed" }
block = ->(result) { block_run = "changed" }
subject.run(FactoryGirl::Strategy::Build, { }, &block)
block_run.should == "changed"
end
Expand Down

0 comments on commit 5b9aa48

Please sign in to comment.