Permalink
Browse files

Execute blocks in the binding of an Object

  • Loading branch information...
1 parent 455cf8a commit 11d86c630bc8a1cc98c02a2d1ad029f507990cd5 Colin Bartlett and Paul Elliott committed with Hashrocket Workstation Oct 19, 2012
View
@@ -9,6 +9,7 @@ module Schematic
autoload :Definition, 'fabrication/schematic/definition'
autoload :Manager, 'fabrication/schematic/manager'
autoload :Evaluator, 'fabrication/schematic/evaluator'
+ autoload :Runner, 'fabrication/schematic/runner'
end
autoload :Config, 'fabrication/config'
@@ -1,8 +1,9 @@
class Fabrication::Schematic::Attribute
- attr_accessor :name, :params, :value
+ attr_accessor :klass, :name, :params, :value
- def initialize(name, value, params={}, &block)
+ def initialize(klass, name, value, params={}, &block)
+ self.klass = klass
self.name = name
self.params = params
self.value = value.nil? ? block : value
@@ -22,16 +23,20 @@ def transient?
def processed_value(processed_attributes)
if count
- (1..count).map { |i| value.call(processed_attributes, i) }
+ (1..count).map { |i| execute(processed_attributes, i, &value) }
elsif value_proc?
- value.call(processed_attributes)
+ execute(processed_attributes, &value)
else
value
end
end
private
+ def execute(*args, &block)
+ Fabrication::Schematic::Runner.new(klass).instance_exec(*args, &block)
+ end
+
def count; params[:count] end
def value_proc?; Proc === value end
@@ -24,7 +24,7 @@ def attribute(name)
end
def append_or_update_attribute(attribute_name, value, params={}, &block)
- attribute = Fabrication::Schematic::Attribute.new(attribute_name, value, params, &block)
+ attribute = Fabrication::Schematic::Attribute.new(klass, attribute_name, value, params, &block)
if index = attributes.index { |a| a.name == attribute.name }
attribute.transient! if attributes[index].transient?
attributes[index] = attribute
@@ -39,13 +39,4 @@ def transient(*field_names)
@_definition.append_or_update_attribute(field_name, nil, transient: true)
end
end
-
- def sequence(name=::Fabrication::Sequencer::DEFAULT, start=nil, &block)
- name = "#{@_definition.klass.to_s.downcase.gsub(/::/, '_')}_#{name}"
- ::Fabrication::Sequencer.sequence(name, start, &block)
- end
-
- def p(*args); ::Kernel.p(*args) end
- def puts(*args); ::Kernel.puts(*args) end
- def rand(*args); ::Kernel.rand(*args) end
end
@@ -0,0 +1,13 @@
+class Fabrication::Schematic::Runner
+
+ attr_accessor :klass
+
+ def initialize(klass)
+ self.klass = klass
+ end
+
+ def sequence(name=Fabrication::Sequencer::DEFAULT, start=nil, &block)
+ name = "#{klass.to_s.downcase.gsub(/::/, '_')}_#{name}"
+ Fabrication::Sequencer.sequence(name, start, &block)
+ end
+end
@@ -5,9 +5,10 @@
describe ".new" do
context "with name, params, and a static value" do
subject do
- Fabrication::Schematic::Attribute.new("a", "c", {:b => 1})
+ Fabrication::Schematic::Attribute.new(Object, "a", "c", {:b => 1})
end
+ its(:klass) { should == Object }
its(:name) { should == "a" }
its(:params) { should == {:b => 1} }
its(:value) { should == "c" }
@@ -16,7 +17,7 @@
context "with a block value" do
subject do
- Fabrication::Schematic::Attribute.new("a", Proc.new { "c" })
+ Fabrication::Schematic::Attribute.new(Object, "a", Proc.new { "c" })
end
it "has a proc for a value" do
@@ -25,35 +26,35 @@
end
context "with nils" do
- subject { Fabrication::Schematic::Attribute.new("a", nil) }
+ subject { Fabrication::Schematic::Attribute.new(Object, "a", nil) }
its(:params) { should == {} }
its(:value) { should be_nil }
end
end
describe '#transient?' do
- subject { Fabrication::Schematic::Attribute.new("a", nil, transient: true) }
+ subject { Fabrication::Schematic::Attribute.new(Object, "a", nil, transient: true) }
it { should be_transient }
end
describe '#processed_value' do
subject { attribute.processed_value({}) }
context 'singular value' do
- let(:attribute) { Fabrication::Schematic::Attribute.new("a", "something") }
+ let(:attribute) { Fabrication::Schematic::Attribute.new(Object, "a", "something") }
it { should == 'something' }
end
context 'singular block' do
let(:attribute) do
- Fabrication::Schematic::Attribute.new("a", nil, {}) { 'something' }
+ Fabrication::Schematic::Attribute.new(Object, "a", nil, {}) { 'something' }
end
it { should == 'something' }
end
context 'collection block' do
let(:attribute) do
- Fabrication::Schematic::Attribute.new("a", nil, {count: 2}) { 'something' }
+ Fabrication::Schematic::Attribute.new(Object, "a", nil, {count: 2}) { 'something' }
end
it { should == %w(something something) }
end
@@ -0,0 +1,35 @@
+require 'spec_helper'
+
+describe Fabrication::Schematic::Evaluator do
+ let(:definition) { Fabrication::Schematic::Definition.new(ParentRubyObject) }
+ let(:evaluator) { Fabrication::Schematic::Evaluator.new }
+
+ describe 'attribute handling' do
+ subject { definition.attributes.first }
+ before { evaluator.process(definition, &block) }
+
+ context 'without a count' do
+ let(:block) do
+ proc do
+ dynamic_field { Fabricate(:child_ruby_object) }
+ end
+ end
+ its(:name) { should == :dynamic_field }
+ it 'the attribute produces the correct value' do
+ subject.processed_value({}).should be_kind_of(ChildRubyObject)
+ end
+ end
+
+ context 'with a count' do
+ let(:block) do
+ proc do
+ dynamic_field(count: 2) { Fabricate(:child_ruby_object) }
+ end
+ end
+ its(:name) { should == :dynamic_field }
+ it 'the attribute produces the correct value' do
+ subject.processed_value({}).first.should be_kind_of(ChildRubyObject)
+ end
+ end
+ end
+end

0 comments on commit 11d86c6

Please sign in to comment.