embedded_record 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - ree
6
+ - jruby
7
+ - rbx-2.0
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # EmbeddedRecord
1
+ # EmbeddedRecord [![Build Status](https://secure.travis-ci.org/wojtekmach/embedded_record.png)](http://travis-ci.org/wojtekmach/embedded_record)
2
2
 
3
- * http://github.com/wojtekmach/embedded\_record
3
+ * http://github.com/wojtekmach/embedded_record
4
4
 
5
5
  ## DESCRIPTION:
6
6
 
@@ -73,9 +73,24 @@ p car.color_ids # => [:red, :green]
73
73
  p car.colors_mask # => 3 (2**0 + 2**1)
74
74
  ```
75
75
 
76
+ NullObject support:
77
+
78
+ ```
79
+ class Color
80
+ record nil, :name => "Blank"
81
+ end
82
+
83
+ Color.ids # => [:red, :green, :blue] # still the same
84
+
85
+ Car.new.color.name # => "Blank" # No whiny nils!
86
+ ```
87
+
76
88
  ## REQUIREMENTS:
77
89
 
78
- None
90
+ Development:
91
+
92
+ * rake
93
+ * minitest >= 2.6
79
94
 
80
95
  ## INSTALL:
81
96
 
@@ -17,7 +17,6 @@ Gem::Specification.new do |s|
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
18
  s.require_paths = ["lib"]
19
19
 
20
- # specify any dependencies here; for example:
21
- # s.add_development_dependency "rspec"
22
- # s.add_runtime_dependency "rest-client"
20
+ s.add_development_dependency "rake"
21
+ s.add_development_dependency "minitest", ">= 2.6"
23
22
  end
@@ -1,5 +1,5 @@
1
1
  module EmbeddedRecord
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
 
4
4
  def self.included(klass)
5
5
  klass.extend self
@@ -17,6 +17,8 @@ module EmbeddedRecord
17
17
  define_method name do
18
18
  if val = send(attr)
19
19
  klass.all[val]
20
+ elsif klass.null_record
21
+ klass.null_record
20
22
  end
21
23
  end
22
24
 
@@ -198,7 +200,26 @@ module EmbeddedRecord::Record
198
200
 
199
201
  record.send "#{k}=", v
200
202
  end
201
- records[id] = record
203
+
204
+ if id == nil
205
+ @null_record = record
206
+ else
207
+ records[id] = record
208
+ end
209
+ end
210
+
211
+ ##
212
+ # Returns null record, a record which id is nil
213
+ #
214
+ # Example:
215
+ #
216
+ # class Foo
217
+ # record nil, :name => "Empty"
218
+ # end
219
+ #
220
+ # Foo.null_record.name # => "Empty"
221
+ def null_record
222
+ @null_record
202
223
  end
203
224
 
204
225
  ##
@@ -206,7 +227,7 @@ module EmbeddedRecord::Record
206
227
  def attribute(name)
207
228
  attributes << name
208
229
 
209
- unless method_defined?(name)
230
+ if !method_defined?(name) || name == :id
210
231
  attr_accessor name
211
232
  end
212
233
  end
@@ -137,3 +137,47 @@ describe "Object embedding records" do
137
137
  @shirt.colors.must_equal [Color.find(:green), Color.find(:blue)]
138
138
  end
139
139
  end
140
+
141
+ describe EmbeddedRecord do
142
+ before do
143
+ @cls = Class.new do
144
+ include EmbeddedRecord
145
+ end
146
+ end
147
+
148
+ describe "with embed_record and null record" do
149
+ before do
150
+ rec = Class.new do
151
+ include EmbeddedRecord::Record
152
+ attribute :name
153
+ record nil, :name => "Empty"
154
+ end
155
+
156
+ @cls.class_eval do
157
+ attr_accessor :foo_mask
158
+ embed_record :foo, :class => rec
159
+ end
160
+ end
161
+
162
+ it "#<name> when <name>_mask is nil returns null record" do
163
+ @cls.new.foo.name.must_equal "Empty"
164
+ end
165
+ end
166
+ end
167
+
168
+ describe EmbeddedRecord::Record do
169
+ before do
170
+ @cls = Class.new do
171
+ include EmbeddedRecord::Record
172
+ attribute :name
173
+ end
174
+ end
175
+
176
+ it "#record with nil acts as a null object" do
177
+ @cls.class_eval do
178
+ record nil, :name => "Empty"
179
+ end
180
+ @cls.null_record.name.must_equal "Empty"
181
+ @cls.all.must_equal []
182
+ end
183
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embedded_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,30 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-06 00:00:00.000000000Z
13
- dependencies: []
12
+ date: 2011-11-07 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70207362929780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70207362929780
25
+ - !ruby/object:Gem::Dependency
26
+ name: minitest
27
+ requirement: &70207362928920 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '2.6'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70207362928920
14
36
  description:
15
37
  email:
16
38
  - wojtek@wojtekmach.pl
@@ -19,6 +41,7 @@ extensions: []
19
41
  extra_rdoc_files: []
20
42
  files:
21
43
  - .gitignore
44
+ - .travis.yml
22
45
  - Gemfile
23
46
  - README.md
24
47
  - Rakefile