blueprints 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.2
1
+ 0.5.0
data/blueprints.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{blueprints}
8
- s.version = "0.4.2"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andrius Chamentauskas"]
12
- s.date = %q{2010-01-02}
12
+ s.date = %q{2010-02-17}
13
13
  s.description = %q{Another replacement for factories and fixtures. The library that lazy typists will love}
14
14
  s.email = %q{sinsiliux@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -63,15 +63,15 @@ Gem::Specification.new do |s|
63
63
  s.summary = %q{Another replacement for factories and fixtures}
64
64
  s.test_files = [
65
65
  "spec/no_db/fixtures/fruit.rb",
66
- "spec/no_db/blueprint.rb",
67
- "spec/no_db/blueprints_spec.rb",
68
66
  "spec/no_db/spec_helper.rb",
69
- "spec/active_record/fixtures/tree.rb",
67
+ "spec/no_db/blueprints_spec.rb",
68
+ "spec/no_db/blueprint.rb",
70
69
  "spec/active_record/fixtures/fruit.rb",
70
+ "spec/active_record/fixtures/tree.rb",
71
71
  "spec/active_record/fixtures/schema.rb",
72
- "spec/active_record/blueprint.rb",
73
- "spec/active_record/blueprints_spec.rb",
74
72
  "spec/active_record/spec_helper.rb",
73
+ "spec/active_record/blueprints_spec.rb",
74
+ "spec/active_record/blueprint.rb",
75
75
  "test/test_helper.rb",
76
76
  "test/blueprints_test.rb"
77
77
  ]
@@ -2,5 +2,9 @@ module Blueprints
2
2
  # Class that blueprint blocks are evaluated against. Allows you to access options that were passed to build method.
3
3
  class Context
4
4
  attr_accessor :options
5
+
6
+ def build(*names)
7
+ Namespace.root.build(*names)
8
+ end
5
9
  end
6
10
  end
@@ -69,12 +69,10 @@ module Blueprints
69
69
  end
70
70
 
71
71
  module Instance
72
- # Updates attributes of object and calls save!. Bypasses attr_protected ant attr_accessible.
72
+ # Updates attributes of object and calls save!. Bypasses attr_protected anduep attr_accessible.
73
73
  def blueprint(attributes)
74
- attributes.each do |attr, value|
75
- value = Blueprints::Namespace.root.context.instance_variable_get(value) if value.is_a? Symbol and value.to_s =~ /^@.+$/
76
- send("#{attr}=", value)
77
- end
74
+ attributes.each {|attr, value| attributes[attr] = Blueprints::Namespace.root.context.instance_variable_get(value) if value.is_a? Symbol and value.to_s =~ /^@.+$/ }
75
+ send(:attributes=, attributes, false)
78
76
  save!
79
77
  end
80
78
  end
@@ -7,11 +7,12 @@ module Blueprints
7
7
  # build :apple, :orange
8
8
  #
9
9
  # # build :apple scenario with additional options
10
- # build :apple, :color => 'red'
10
+ # build :apple => {:color => 'red'}
11
+ #
12
+ # # options can also be passed for several blueprints
13
+ # build :pear, :apple => {:color => 'red'}, :orange => {:color => 'orange'}
11
14
  def build_plan(*names)
12
- result = Namespace.root.build(*names).last
13
- Namespace.root.copy_ivars(self)
14
- result
15
+ returning(Namespace.root.build(*names)) { Namespace.root.copy_ivars(self) }
15
16
  end
16
17
 
17
18
  alias :build :build_plan
@@ -19,6 +19,12 @@ module Blueprints
19
19
  @result
20
20
  end
21
21
 
22
+ # Changes blueprint block to build another blueprint by passing additional options to it. Usually used to dry up
23
+ # blueprints that are often built with some options.
24
+ def extends(parent, options)
25
+ @block = Proc.new { build parent => options }
26
+ end
27
+
22
28
  private
23
29
 
24
30
  def surface_errors
@@ -9,6 +9,7 @@ module Blueprints
9
9
  def initialize
10
10
  @executed_plans = Set.new
11
11
  @global_executed_plans = Set.new
12
+ @auto_iv_list = Set.new
12
13
 
13
14
  super ''
14
15
  end
@@ -38,14 +39,23 @@ module Blueprints
38
39
 
39
40
  # Builds blueprints that are passed against current context.
40
41
  def build(*names)
41
- options = names.extract_options!
42
- names.map {|name| self[name].build(options) }
42
+ names.inject(nil) do |result, member|
43
+ if member.is_a?(Hash)
44
+ member.map {|name, options| self[name].build(options) }.last
45
+ else
46
+ self[member].build
47
+ end
48
+ end
43
49
  end
44
50
 
45
- # Sets instance variable in current context to passed value.
51
+ # Sets instance variable in current context to passed value. If instance variable with same name already exists, it
52
+ # is set only if it was set using this same method
46
53
  def add_variable(name, value)
47
54
  name = "@#{name}" unless name.to_s[0, 1] == "@"
48
- @context.instance_variable_set(name, value) unless @context.instance_variable_get(name)
55
+ if !@context.instance_variable_get(name) or @auto_iv_list.include?(name)
56
+ @auto_iv_list << name
57
+ @context.instance_variable_set(name, value)
58
+ end
49
59
  end
50
60
 
51
61
  @@root = RootNamespace.new
@@ -35,12 +35,17 @@ end
35
35
  blueprint :parent_not_existing => :not_existing
36
36
 
37
37
  Tree.blueprint :oak, :name => 'Oak', :size => 'large'
38
+ blueprint(:huge_oak).extends(:oak, :size => 'huge')
38
39
 
39
40
  blueprint :pine do
40
41
  @the_pine = Tree.blueprint :name => 'Pine', :size => 'medium'
41
42
  end
42
43
 
43
44
  Fruit.blueprint(:acorn, :species => 'Acorn', :tree => :@oak).depends_on(:oak)
45
+ blueprint :small_acorn do
46
+ build :acorn => {:average_diameter => 1}
47
+ end
48
+ blueprint(:huge_acorn => :huge_oak).extends(:acorn, :average_diameter => 100)
44
49
 
45
50
  namespace :pitted => :pine do
46
51
  Tree.blueprint :peach_tree, :name => 'pitted peach tree'
@@ -53,5 +58,5 @@ namespace :pitted => :pine do
53
58
  end
54
59
 
55
60
  blueprint :apple_with_params do
56
- Fruit.create! options.merge(:species => 'apple')
61
+ Fruit.create! options.reverse_merge(:species => 'apple')
57
62
  end
@@ -254,7 +254,7 @@ describe Blueprints do
254
254
  end
255
255
 
256
256
  it "should automatically merge passed options" do
257
- build :oak, :size => 'optional'
257
+ build :oak => {:size => 'optional'}
258
258
  @oak.name.should == 'Oak'
259
259
  @oak.size.should == 'optional'
260
260
  end
@@ -309,7 +309,7 @@ describe Blueprints do
309
309
 
310
310
  describe 'extra parameters' do
311
311
  it "should allow passing extra parameters when building" do
312
- build :apple_with_params, :average_diameter => 14
312
+ build :apple_with_params => {:average_diameter => 14}
313
313
  @apple_with_params.average_diameter.should == 14
314
314
  @apple_with_params.species.should == 'apple'
315
315
  end
@@ -321,9 +321,48 @@ describe Blueprints do
321
321
  end
322
322
 
323
323
  it "should use extra params only on blueprints specified" do
324
- build :acorn, :average_diameter => 5
324
+ build :acorn => {:average_diameter => 5}
325
325
  @acorn.average_diameter.should == 5
326
326
  end
327
+
328
+ it "should allow passing extra params for each blueprint individually" do
329
+ build :acorn => {:average_diameter => 3}, :apple_with_params => {:average_diameter => 2}
330
+ @acorn.average_diameter.should == 3
331
+ @apple_with_params.average_diameter.should == 2
332
+ end
333
+
334
+ it "should allow passing options for some blueprints only" do
335
+ build(:acorn, :apple_with_params => {:average_diameter => 2}).should == @apple_with_params
336
+ @acorn.average_diameter.should == nil
337
+ @apple_with_params.average_diameter.should == 2
338
+ end
339
+ end
340
+
341
+ it "should overwrite auto created instance variable with another auto created one" do
342
+ build :acorn => {:average_diameter => 3}
343
+ demolish :fruits, :undo => :acorn
344
+ @acorn.average_diameter.should == 3
345
+
346
+ build :acorn => {:average_diameter => 5}
347
+ @acorn.average_diameter.should == 5
348
+ end
349
+
350
+ describe "extending blueprints" do
351
+ it "should allow to call build method inside blueprint body" do
352
+ build :small_acorn
353
+ @small_acorn.average_diameter.should == 1
354
+ @small_acorn.should == @acorn
355
+ end
356
+
357
+ it "should allow to use shortcut to extend blueprint" do
358
+ build :huge_acorn
359
+ @huge_acorn.average_diameter.should == 100
360
+ end
361
+
362
+ it "should allow extended blueprint be dependency and associated object" do
363
+ build :huge_acorn
364
+ @huge_acorn.tree.size.should == 'huge'
365
+ end
327
366
  end
328
367
  end
329
368
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blueprints
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrius Chamentauskas
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-02 00:00:00 +02:00
12
+ date: 2010-02-17 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -91,14 +91,14 @@ specification_version: 3
91
91
  summary: Another replacement for factories and fixtures
92
92
  test_files:
93
93
  - spec/no_db/fixtures/fruit.rb
94
- - spec/no_db/blueprint.rb
95
- - spec/no_db/blueprints_spec.rb
96
94
  - spec/no_db/spec_helper.rb
97
- - spec/active_record/fixtures/tree.rb
95
+ - spec/no_db/blueprints_spec.rb
96
+ - spec/no_db/blueprint.rb
98
97
  - spec/active_record/fixtures/fruit.rb
98
+ - spec/active_record/fixtures/tree.rb
99
99
  - spec/active_record/fixtures/schema.rb
100
- - spec/active_record/blueprint.rb
101
- - spec/active_record/blueprints_spec.rb
102
100
  - spec/active_record/spec_helper.rb
101
+ - spec/active_record/blueprints_spec.rb
102
+ - spec/active_record/blueprint.rb
103
103
  - test/test_helper.rb
104
104
  - test/blueprints_test.rb