active_null_object 0.0.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -0
- data/VERSION +1 -1
- data/lib/active_null_object.rb +2 -1
- data/lib/active_null_object/association.rb +9 -11
- data/lib/active_null_object/create_null_object.rb +17 -0
- data/lib/active_null_object/finder.rb +3 -2
- metadata +39 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e8fbf97673b27b4967b211923df0901636be09f
|
4
|
+
data.tar.gz: 432a2be04b42248319a0b4d0c471dbcc6c7aaefa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83ee41e7d52451c3f2614ef77ee1c315afd2edcfd0caff730b74163c2d75cfbdf9c49fc3c2d5387577f5f925325c0fed1a02912947b4239771f177be868d6926
|
7
|
+
data.tar.gz: fd224e50c9d079b5d82a125cecb28c864fabbb1d36d20d0d0b40f283e291c92c43dc50fdfb9ff4784d65b1ccd0785c2957244f00cdb5766e739147723d91bd84
|
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
Active Null Object
|
2
2
|
==================
|
3
3
|
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/active_null_object.svg)](http://badge.fury.io/rb/active_null_object)
|
5
|
+
[![Build Status](https://travis-ci.org/stefanroex/active_null_object.svg)](https://travis-ci.org/stefanroex/active_null_object)
|
6
|
+
|
4
7
|
Instead of returning nil when an association is not found, return a [Null Object](http://martinfowler.com/eaaCatalog/specialCase.html).
|
5
8
|
|
6
9
|
Associations
|
@@ -18,4 +21,7 @@ Finder
|
|
18
21
|
```ruby
|
19
22
|
# Use a specialized finder if you want a Null Object if the record is not found.
|
20
23
|
User.find_or_null_object(1)
|
24
|
+
|
25
|
+
# ...or pass explicit class name.
|
26
|
+
User.find_or_null_object(1, null_object: 'NullAdmin')
|
21
27
|
```
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
1.0.0
|
data/lib/active_null_object.rb
CHANGED
@@ -2,11 +2,12 @@ require 'active_record'
|
|
2
2
|
require 'active_support'
|
3
3
|
require 'active_support/core_ext/module/attribute_accessors'
|
4
4
|
require 'active_null_object/association'
|
5
|
+
require 'active_null_object/create_null_object'
|
5
6
|
require 'active_null_object/finder'
|
6
7
|
|
7
8
|
module ActiveNullObject
|
8
9
|
mattr_accessor :initialize_method
|
9
|
-
self.initialize_method =
|
10
|
+
self.initialize_method = 'new'
|
10
11
|
end
|
11
12
|
|
12
13
|
ActiveRecord::Base.send(:include, ActiveNullObject::Association)
|
@@ -9,21 +9,19 @@ module ActiveNullObject
|
|
9
9
|
end
|
10
10
|
|
11
11
|
module ClassMethods
|
12
|
-
def belongs_to_with_null_object(
|
12
|
+
def belongs_to_with_null_object(relation_name, scope = nil, options = {})
|
13
13
|
options = scope if scope.is_a?(Hash)
|
14
14
|
null_object_name = options.delete(:null_object)
|
15
|
-
belongs_to_without_null_object(
|
16
|
-
|
15
|
+
belongs_to_without_null_object(relation_name, scope, options)
|
16
|
+
if null_object_name
|
17
|
+
define_null_object_getter(relation_name, null_object_name)
|
18
|
+
end
|
17
19
|
end
|
18
20
|
|
19
|
-
def define_null_object_getter(
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
"Null#{name.to_s.classify}"
|
24
|
-
end
|
25
|
-
define_method(name) do
|
26
|
-
super() || null_object.constantize.public_send(ActiveNullObject.initialize_method)
|
21
|
+
def define_null_object_getter(relation_name, custom_name)
|
22
|
+
create_null_object = CreateNullObject.new(relation_name, custom_name)
|
23
|
+
define_method(relation_name) do
|
24
|
+
super() || create_null_object.call
|
27
25
|
end
|
28
26
|
end
|
29
27
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ActiveNullObject
|
2
|
+
CreateNullObject = Struct.new(:class_name, :custom_name) do
|
3
|
+
def call
|
4
|
+
object_name.constantize.public_send(ActiveNullObject.initialize_method)
|
5
|
+
end
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def object_name
|
10
|
+
if custom_name.is_a?(String)
|
11
|
+
custom_name
|
12
|
+
else
|
13
|
+
"Null#{class_name.to_s.classify}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -3,8 +3,9 @@ module ActiveNullObject
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
module ClassMethods
|
6
|
-
def find_or_null_object(id)
|
7
|
-
|
6
|
+
def find_or_null_object(id, options = {})
|
7
|
+
custom_name = options.delete(:null_object)
|
8
|
+
find_by(id: id) || CreateNullObject.new(name, custom_name).call
|
8
9
|
end
|
9
10
|
end
|
10
11
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_null_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Roex
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-09-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,42 +16,70 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 4.0.
|
19
|
+
version: 4.2.0.rc3
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 4.0.
|
26
|
+
version: 4.2.0.rc3
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 4.0.
|
33
|
+
version: 4.2.0.rc3
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 4.0.
|
40
|
+
version: 4.2.0.rc3
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 3.0.0
|
47
|
+
version: 3.0.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 3.0.0
|
54
|
+
version: 3.0.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
55
83
|
- !ruby/object:Gem::Dependency
|
56
84
|
name: sqlite3
|
57
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -76,6 +104,7 @@ files:
|
|
76
104
|
- VERSION
|
77
105
|
- lib/active_null_object.rb
|
78
106
|
- lib/active_null_object/association.rb
|
107
|
+
- lib/active_null_object/create_null_object.rb
|
79
108
|
- lib/active_null_object/finder.rb
|
80
109
|
homepage: http://stefanroex.nl
|
81
110
|
licenses: []
|
@@ -96,8 +125,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
125
|
version: '0'
|
97
126
|
requirements: []
|
98
127
|
rubyforge_project:
|
99
|
-
rubygems_version: 2.
|
128
|
+
rubygems_version: 2.4.5
|
100
129
|
signing_key:
|
101
130
|
specification_version: 4
|
102
131
|
summary: Simple null objects in Active Record
|
103
132
|
test_files: []
|
133
|
+
has_rdoc:
|