bricks 0.0.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +36 -11
- data/VERSION +1 -1
- data/bricks.gemspec +3 -4
- data/lib/bricks/builder.rb +19 -10
- data/lib/bricks/dsl.rb +4 -4
- metadata +21 -46
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
Bricks
|
2
2
|
======
|
3
3
|
|
4
|
-
Bricks is a hybrid Object Builder/Factory implementation. It aims to be a more flexible alternative to the existing Object Factory solutions while
|
4
|
+
*Bricks* is a hybrid Object Builder/Factory implementation. It aims to be a more flexible alternative to the existing Object Factory solutions while remaining as simple as possible.
|
5
5
|
|
6
6
|
Usage
|
7
7
|
-----
|
8
8
|
|
9
|
-
|
9
|
+
We'll use the following domain to describe *Brick's* features:
|
10
10
|
|
11
11
|
# Only ActiveRecord objects are supported right now.
|
12
12
|
|
@@ -41,7 +41,7 @@ Let's assume you have the following class:
|
|
41
41
|
|
42
42
|
# == Schema Information
|
43
43
|
#
|
44
|
-
# Table name:
|
44
|
+
# Table name: readers
|
45
45
|
#
|
46
46
|
# id :integer(4) not null, primary key
|
47
47
|
# name :string(255)
|
@@ -50,7 +50,7 @@ Let's assume you have the following class:
|
|
50
50
|
class Reader < ActiveRecord::Base
|
51
51
|
end
|
52
52
|
|
53
|
-
At its simplest, you can start using Bricks without declaring any builder (*note:* it gets less verbose).
|
53
|
+
At its simplest, you can start using *Bricks* without declaring any builder (*note:* it gets less verbose).
|
54
54
|
|
55
55
|
article_builder = build(Article)
|
56
56
|
|
@@ -62,11 +62,11 @@ This will give you a builder for the Article class, which you can then use to bu
|
|
62
62
|
|
63
63
|
Contrary to the original pattern, builders are stateful (i.e., you don't get a new builder every time you call a method on the current builder).
|
64
64
|
|
65
|
-
You can get the underlying instance by calling
|
65
|
+
You can get the underlying instance by calling `#generate`.
|
66
66
|
|
67
67
|
article = article_builder.generate
|
68
68
|
|
69
|
-
This will initialize an Article with the attributes you passed the builder. If, instead of initializing, you'd prefer the record to be created right away, use
|
69
|
+
This will initialize an Article with the attributes you passed the builder. If, instead of initializing, you'd prefer the record to be created right away, use `#create` instead.
|
70
70
|
|
71
71
|
If you don't really care about the builder and just want the underlying instance you can instead use.
|
72
72
|
|
@@ -81,7 +81,7 @@ When you want to use the default builder, without customizing it any further, yo
|
|
81
81
|
|
82
82
|
### Building builders
|
83
83
|
|
84
|
-
Of course, using builders like described above isn't of much use. Let's create a builder for
|
84
|
+
Of course, using builders like described above isn't of much use. Let's create a builder for `Article`:
|
85
85
|
|
86
86
|
Bricks do
|
87
87
|
builder Article do
|
@@ -113,7 +113,7 @@ You can get at the underlying instance from deferred blocks:
|
|
113
113
|
|
114
114
|
### Associations
|
115
115
|
|
116
|
-
Bricks supports setting association records.
|
116
|
+
*Bricks* supports setting association records.
|
117
117
|
|
118
118
|
#### Many-to-one (belongs to)
|
119
119
|
|
@@ -133,7 +133,7 @@ You can also customize the association builder instance:
|
|
133
133
|
|
134
134
|
builder Article do
|
135
135
|
# ...
|
136
|
-
publication.name("The
|
136
|
+
publication.name("The Caribeaneer")
|
137
137
|
end
|
138
138
|
|
139
139
|
If you prepend a "~" to the association declaration, the record will be initialized/created *only* if a record with the given attributes doesn't exist yet:
|
@@ -171,7 +171,8 @@ Given the builder:
|
|
171
171
|
you can do something like:
|
172
172
|
|
173
173
|
np = build!(Newspaper)
|
174
|
-
np.
|
174
|
+
np.class # => Newspaper
|
175
|
+
np.name # => "The Caribbean Times"
|
175
176
|
|
176
177
|
### Traits
|
177
178
|
|
@@ -192,7 +193,7 @@ The real power of the Builder pattern comes from the use of traits. Instead of d
|
|
192
193
|
|
193
194
|
Use it like this:
|
194
195
|
|
195
|
-
build(Article).alternative_publication("The Caribeaneer").by_elaine
|
196
|
+
article = build(Article).alternative_publication("The Caribeaneer").by_elaine!
|
196
197
|
|
197
198
|
Note that if you want to override a *-to-many association inside a trait, you need to clear it first:
|
198
199
|
|
@@ -214,6 +215,30 @@ Note that if you want to override a *-to-many association inside a trait, you ne
|
|
214
215
|
|
215
216
|
For an executable version of this documentation, please see spec/bricks_spec.rb.
|
216
217
|
|
218
|
+
Installation
|
219
|
+
------------
|
220
|
+
|
221
|
+
### Rails 2
|
222
|
+
|
223
|
+
Add `config.gem "bricks"` to `environments/test.rb` or, as a rails plugin:
|
224
|
+
|
225
|
+
$ script/plugin install git://github.com/mojotech/bricks.git # Rails 2
|
226
|
+
|
227
|
+
### Rails 3
|
228
|
+
|
229
|
+
Add `gem "bricks"` to your `Gemfile`, or, as a rails plugin:
|
230
|
+
|
231
|
+
$ rails plugin install git://github.com/mojotech/bricks.git # Rails 3
|
232
|
+
|
233
|
+
### RSpec
|
234
|
+
|
235
|
+
# you only need to add the following line if you're using the gem
|
236
|
+
require 'bricks/adapters/active_record'
|
237
|
+
|
238
|
+
# put this inside RSpec's configure block to get access to
|
239
|
+
# #build, #build!, #create and #create! in your specs
|
240
|
+
config.include Bricks::DSL
|
241
|
+
|
217
242
|
Copyright
|
218
243
|
---------
|
219
244
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/bricks.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{bricks}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.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"]
|
12
|
-
s.date = %q{2011-06-
|
12
|
+
s.date = %q{2011-06-17}
|
13
13
|
s.email = %q{david@mojotech.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE.txt",
|
@@ -39,11 +39,10 @@ Gem::Specification.new do |s|
|
|
39
39
|
s.homepage = %q{http://github.com/mojotech/bricks}
|
40
40
|
s.licenses = ["MIT"]
|
41
41
|
s.require_paths = ["lib"]
|
42
|
-
s.rubygems_version = %q{1.
|
42
|
+
s.rubygems_version = %q{1.6.2}
|
43
43
|
s.summary = %q{Hybrid object builder/factory.}
|
44
44
|
|
45
45
|
if s.respond_to? :specification_version then
|
46
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
46
|
s.specification_version = 3
|
48
47
|
|
49
48
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
data/lib/bricks/builder.rb
CHANGED
@@ -23,16 +23,25 @@ module Bricks
|
|
23
23
|
self
|
24
24
|
end
|
25
25
|
|
26
|
-
def derive(
|
27
|
-
|
28
|
-
|
26
|
+
def derive(*args)
|
27
|
+
klass, save = case args.size
|
28
|
+
when 2
|
29
|
+
args
|
30
|
+
when 1
|
31
|
+
case args.first
|
32
|
+
when Class
|
33
|
+
[args.first, @save]
|
34
|
+
else
|
35
|
+
[@class, args.first]
|
36
|
+
end
|
37
|
+
when 0
|
38
|
+
[@class, @save]
|
39
|
+
else
|
40
|
+
raise ArgumentError, "wrong number of arguments " +
|
41
|
+
"(#{args.size} for 0, 1 or 2)"
|
42
|
+
end
|
29
43
|
|
30
|
-
|
31
|
-
derive(@class, false)
|
32
|
-
end
|
33
|
-
|
34
|
-
def dup_as_creator
|
35
|
-
derive(@class, true)
|
44
|
+
Builder.new(klass, @attrs, @traits, save)
|
36
45
|
end
|
37
46
|
|
38
47
|
def initialize(klass, attrs = nil, traits = nil, save = false, &block)
|
@@ -138,7 +147,7 @@ module Bricks
|
|
138
147
|
elsif val
|
139
148
|
pair[-1] = val
|
140
149
|
elsif adapter.association?(@class, name, :one)
|
141
|
-
pair[-1] =
|
150
|
+
pair[-1] = builder(adapter.association(@class, name).klass, @save)
|
142
151
|
elsif adapter.association?(@class, name, :many)
|
143
152
|
pair[-1] ||= BuilderSet.new(adapter.association(@class, name).klass)
|
144
153
|
else
|
data/lib/bricks/dsl.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Bricks
|
2
2
|
module DSL
|
3
3
|
def build(klass)
|
4
|
-
builder(klass)
|
4
|
+
builder(klass, false)
|
5
5
|
end
|
6
6
|
|
7
7
|
def build!(klass)
|
@@ -9,15 +9,15 @@ module Bricks
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def create(klass)
|
12
|
-
builder(klass)
|
12
|
+
builder(klass, true)
|
13
13
|
end
|
14
14
|
|
15
15
|
def create!(klass)
|
16
16
|
create(klass).generate
|
17
17
|
end
|
18
18
|
|
19
|
-
def builder(klass)
|
20
|
-
Bricks.builders[klass]
|
19
|
+
def builder(klass, save)
|
20
|
+
Bricks.builders[klass].derive(save)
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
metadata
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bricks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 0
|
10
|
-
version: 0.0.0
|
4
|
+
prerelease:
|
5
|
+
version: 0.3.0
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- David Leal
|
@@ -15,70 +10,53 @@ autorequire:
|
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
12
|
|
18
|
-
date: 2011-06-
|
13
|
+
date: 2011-06-17 00:00:00 +01:00
|
19
14
|
default_executable:
|
20
15
|
dependencies:
|
21
16
|
- !ruby/object:Gem::Dependency
|
22
|
-
|
23
|
-
|
17
|
+
name: rspec
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
19
|
none: false
|
25
20
|
requirements:
|
26
21
|
- - ~>
|
27
22
|
- !ruby/object:Gem::Version
|
28
|
-
hash: 3
|
29
|
-
segments:
|
30
|
-
- 2
|
31
|
-
- 0
|
32
23
|
version: "2.0"
|
33
|
-
name: rspec
|
34
|
-
requirement: *id001
|
35
24
|
type: :development
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
25
|
prerelease: false
|
38
|
-
version_requirements:
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
30
|
none: false
|
40
31
|
requirements:
|
41
32
|
- - ~>
|
42
33
|
- !ruby/object:Gem::Version
|
43
|
-
hash: 23
|
44
|
-
segments:
|
45
|
-
- 1
|
46
|
-
- 0
|
47
|
-
- 0
|
48
34
|
version: 1.0.0
|
49
|
-
name: bundler
|
50
|
-
requirement: *id002
|
51
35
|
type: :development
|
52
|
-
- !ruby/object:Gem::Dependency
|
53
36
|
prerelease: false
|
54
|
-
version_requirements:
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: jeweler
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
41
|
none: false
|
56
42
|
requirements:
|
57
43
|
- - ~>
|
58
44
|
- !ruby/object:Gem::Version
|
59
|
-
hash: 11
|
60
|
-
segments:
|
61
|
-
- 1
|
62
|
-
- 6
|
63
|
-
- 2
|
64
45
|
version: 1.6.2
|
65
|
-
name: jeweler
|
66
|
-
requirement: *id003
|
67
46
|
type: :development
|
68
|
-
- !ruby/object:Gem::Dependency
|
69
47
|
prerelease: false
|
70
|
-
version_requirements:
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rcov
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
71
52
|
none: false
|
72
53
|
requirements:
|
73
54
|
- - ">="
|
74
55
|
- !ruby/object:Gem::Version
|
75
|
-
hash: 3
|
76
|
-
segments:
|
77
|
-
- 0
|
78
56
|
version: "0"
|
79
|
-
name: rcov
|
80
|
-
requirement: *id004
|
81
57
|
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *id004
|
82
60
|
description:
|
83
61
|
email: david@mojotech.com
|
84
62
|
executables: []
|
@@ -122,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
122
100
|
requirements:
|
123
101
|
- - ">="
|
124
102
|
- !ruby/object:Gem::Version
|
125
|
-
hash:
|
103
|
+
hash: -732403827
|
126
104
|
segments:
|
127
105
|
- 0
|
128
106
|
version: "0"
|
@@ -131,14 +109,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
109
|
requirements:
|
132
110
|
- - ">="
|
133
111
|
- !ruby/object:Gem::Version
|
134
|
-
hash: 3
|
135
|
-
segments:
|
136
|
-
- 0
|
137
112
|
version: "0"
|
138
113
|
requirements: []
|
139
114
|
|
140
115
|
rubyforge_project:
|
141
|
-
rubygems_version: 1.
|
116
|
+
rubygems_version: 1.6.2
|
142
117
|
signing_key:
|
143
118
|
specification_version: 3
|
144
119
|
summary: Hybrid object builder/factory.
|