bricks 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -265,9 +265,17 @@ For an executable version of this documentation, please see spec/bricks_spec.rb.
265
265
  after :clone do
266
266
  send %w(on_the_bugle on_the_planet)[rand(2)]
267
267
  end
268
+
269
+ before :save do
270
+ active true
271
+ end
268
272
  end
269
273
 
270
- *Bricks* supports a single hook right now: after(:clone). It will be executed whenever you use any of #build, #build!, #create or #create!, right before you start customizing the resulting builder on your test.
274
+ *Bricks* supports only two hooks right now: after(:clone) and before(:save).
275
+
276
+ The after(:clone) hook will be executed whenever you use any of #build, #build!, #create or #create!, right before you start customizing the resulting builder on your test.
277
+
278
+ On the other hand, the before(:save) hook will be executed only for #create and #create!.
271
279
 
272
280
  Installation
273
281
  ------------
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.6.0
data/bricks.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "bricks"
8
- s.version = "0.5.1"
8
+ s.version = "0.6.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["David Leal"]
@@ -24,9 +24,11 @@ module Bricks
24
24
  end
25
25
 
26
26
  def after(hook, &block)
27
- @traits.class_eval do
28
- define_method "__after_#{hook}", &block
29
- end
27
+ define_hook :after, hook, &block
28
+ end
29
+
30
+ def before(hook, &block)
31
+ define_hook :before, hook, &block
30
32
  end
31
33
 
32
34
  def derive(args = {})
@@ -60,8 +62,10 @@ module Bricks
60
62
  def generate(opts = {})
61
63
  parent = opts[:parent]
62
64
  search = opts.has_key?(:search) ? opts[:search] : @search
63
- obj = initialize_object(parent)
64
65
 
66
+ run_hook :before, :save if @save
67
+
68
+ obj = initialize_object(parent)
65
69
  obj = adapter.find(@class, Hash[*@attrs.flatten]) || obj if search
66
70
  save_object(obj) if @save
67
71
 
@@ -103,6 +107,12 @@ module Bricks
103
107
 
104
108
  protected
105
109
 
110
+ def define_hook(position, name, &block)
111
+ @traits.class_eval do
112
+ define_method "__#{position}_#{name}", &block
113
+ end
114
+ end
115
+
106
116
  def run_hook(position, name)
107
117
  full_name = "__#{position}_#{name}"
108
118
 
data/spec/bricks_spec.rb CHANGED
@@ -48,6 +48,10 @@ describe Bricks do
48
48
  after :clone do
49
49
  popularity (1..100).to_a[rand(100)]
50
50
  end
51
+
52
+ before :save do
53
+ active true
54
+ end
51
55
  end
52
56
 
53
57
  builder Newspaper do
@@ -227,6 +231,10 @@ describe Bricks do
227
231
  build!(Article).popularity.should_not == build!(Article).popularity
228
232
  end
229
233
 
234
+ it "executes the `create' hook before an object is saved" do
235
+ create!(Article).active.should be_true
236
+ end
237
+
230
238
  it "it does not override values set after the builder is cloned" do
231
239
  build(Article).popularity!(50).popularity.should == 50
232
240
  end
@@ -8,6 +8,8 @@ ActiveRecord::Base.establish_connection(
8
8
  :database => ":memory:"
9
9
  )
10
10
 
11
+ ActiveRecord::Migration.verbose = false
12
+
11
13
  ActiveRecord::Schema.define(:version => 20110608204150) do
12
14
  create_table "articles", :force => true do |t|
13
15
  t.string "author"
@@ -18,6 +20,7 @@ ActiveRecord::Schema.define(:version => 20110608204150) do
18
20
  t.integer "newspaper_id"
19
21
  t.string "title"
20
22
  t.integer "popularity"
23
+ t.boolean "active"
21
24
  end
22
25
 
23
26
  create_table "newspapers", :force => true do |t|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bricks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-11-24 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &82543630 !ruby/object:Gem::Requirement
16
+ requirement: &78463240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '2.0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *82543630
24
+ version_requirements: *78463240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &82543330 !ruby/object:Gem::Requirement
27
+ requirement: &78462910 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.0.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *82543330
35
+ version_requirements: *78462910
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jeweler
38
- requirement: &82526310 !ruby/object:Gem::Requirement
38
+ requirement: &78462410 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.6.2
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *82526310
46
+ version_requirements: *78462410
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rcov
49
- requirement: &82525860 !ruby/object:Gem::Requirement
49
+ requirement: &78462060 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *82525860
57
+ version_requirements: *78462060
58
58
  description:
59
59
  email: david@mojotech.com
60
60
  executables: []
@@ -98,7 +98,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
98
98
  version: '0'
99
99
  segments:
100
100
  - 0
101
- hash: -651750993
101
+ hash: 791194095
102
102
  required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  none: false
104
104
  requirements: