null_and_void 1.2 → 1.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.
- checksums.yaml +7 -0
- data/README.md +8 -1
- data/lib/null_and_void/convertible.rb +4 -4
- data/lib/null_and_void/inflections.rb +47 -0
- data/lib/null_and_void/model_support.rb +4 -4
- data/lib/null_and_void/version.rb +1 -1
- metadata +20 -41
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 58b39812153fc58e40a5fc785eeecbb11f5b6505
|
4
|
+
data.tar.gz: 392b568d86fe7b7a3271c7c440346147dccf52b0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aacd733afcbf39f5f7dcec15ca0b10d2731633a7e580135f5bde4e0dceea42f7018bc2092ee4d61eaa2f1db0081d280c2df7132c10acc2a5db53583570b52e32
|
7
|
+
data.tar.gz: 88be210ed359bdfbde81eea48ff5e70be1d077ac0859194b440860c0ad7ed533f9449d107dea7c284f95b2505255d306810ccac66d3ab3bf34d4634d8df7d1e4
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@ Null And Void
|
|
3
3
|
|
4
4
|
**Create Null Objects with Ease**
|
5
5
|
|
6
|
-
<img src="
|
6
|
+
<img src="https://dl.dropbox.com/s/3vro4yd2vsa5xga/bad-luck-brian-null-object.jpg" align="right" />
|
7
7
|
|
8
8
|
Using the Null Object pattern in Ruby is rather easy so why create a gem?
|
9
9
|
|
@@ -220,6 +220,13 @@ Will return:
|
|
220
220
|
NullUser.instance
|
221
221
|
```
|
222
222
|
|
223
|
+
Other Approaches
|
224
|
+
------
|
225
|
+
|
226
|
+
If you'd like to try a more builder-centric approach to Null Objects, check out:
|
227
|
+
|
228
|
+
[Naught](http://github.com/avdi/naught)
|
229
|
+
|
223
230
|
Issues
|
224
231
|
------
|
225
232
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'null_and_void/inflections'
|
2
2
|
|
3
3
|
module NullAndVoid
|
4
4
|
module Convertible
|
@@ -51,12 +51,12 @@ module NullAndVoid
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def to_model
|
54
|
-
base_path =
|
55
|
-
module_path =
|
54
|
+
base_path = NullAndVoid::Inflections.demodulize(self.class.name)
|
55
|
+
module_path = NullAndVoid::Inflections.deconstantize(self.class.name)
|
56
56
|
source_model = base_path.gsub(/^Null/, '')
|
57
57
|
source_model_path = "#{module_path}::#{source_model}"
|
58
58
|
|
59
|
-
|
59
|
+
NullAndVoid::Inflections.constantize(source_model_path).new
|
60
60
|
end
|
61
61
|
end
|
62
62
|
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module NullAndVoid
|
2
|
+
module Inflections
|
3
|
+
def self.demodulize(path)
|
4
|
+
path = path.to_s
|
5
|
+
if i = path.rindex('::')
|
6
|
+
path[(i+2)..-1]
|
7
|
+
else
|
8
|
+
path
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.deconstantize(path)
|
13
|
+
path.to_s[0, path.rindex('::') || 0] # implementation based on the one in facets' Module#spacename
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.constantize(camel_cased_word)
|
17
|
+
names = camel_cased_word.split('::')
|
18
|
+
|
19
|
+
# Trigger a builtin NameError exception including the ill-formed constant in the message.
|
20
|
+
Object.const_get(camel_cased_word) if names.empty?
|
21
|
+
|
22
|
+
# Remove the first blank element in case of '::ClassName' notation.
|
23
|
+
names.shift if names.size > 1 && names.first.empty?
|
24
|
+
|
25
|
+
names.inject(Object) do |constant, name|
|
26
|
+
if constant == Object
|
27
|
+
constant.const_get(name)
|
28
|
+
else
|
29
|
+
candidate = constant.const_get(name)
|
30
|
+
next candidate if constant.const_defined?(name, false)
|
31
|
+
next candidate unless Object.const_defined?(name)
|
32
|
+
|
33
|
+
# Go down the ancestors to check it it's owned
|
34
|
+
# directly before we reach Object or the end of ancestors.
|
35
|
+
constant = constant.ancestors.inject do |const, ancestor|
|
36
|
+
break const if ancestor == Object
|
37
|
+
break ancestor if ancestor.const_defined?(name, false)
|
38
|
+
const
|
39
|
+
end
|
40
|
+
|
41
|
+
# owner is in Object, so raise
|
42
|
+
constant.const_get(name, false)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -1,16 +1,16 @@
|
|
1
|
-
require '
|
1
|
+
require 'null_and_void/inflections'
|
2
2
|
require 'null_and_void/null_object'
|
3
3
|
|
4
4
|
module NullAndVoid
|
5
5
|
module ModelSupport
|
6
6
|
module ClassMethods
|
7
7
|
def as_null_object
|
8
|
-
base_path =
|
9
|
-
module_path =
|
8
|
+
base_path = NullAndVoid::Inflections.demodulize(self.name)
|
9
|
+
module_path = NullAndVoid::Inflections.deconstantize(self.name)
|
10
10
|
null_object_base = "Null#{base_path}"
|
11
11
|
source_model_path = "#{module_path}::#{null_object_base}"
|
12
12
|
|
13
|
-
|
13
|
+
NullAndVoid::Inflections.constantize(source_model_path).instance
|
14
14
|
rescue NameError
|
15
15
|
NullAndVoid::NullObject.instance
|
16
16
|
end
|
metadata
CHANGED
@@ -1,62 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: null_and_void
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
version: '1.2'
|
4
|
+
version: 1.3.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- jfelchner
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-03-01 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
|
-
prerelease: false
|
16
|
-
type: :runtime
|
17
|
-
name: activesupport
|
18
|
-
requirement: !ruby/object:Gem::Requirement
|
19
|
-
none: false
|
20
|
-
requirements:
|
21
|
-
- - ~>
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: '3.1'
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ~>
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '3.1'
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
prerelease: false
|
32
|
-
type: :development
|
33
14
|
name: rspec
|
34
15
|
requirement: !ruby/object:Gem::Requirement
|
35
|
-
none: false
|
36
16
|
requirements:
|
37
|
-
- - ~>
|
17
|
+
- - "~>"
|
38
18
|
- !ruby/object:Gem::Version
|
39
19
|
version: '2.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
40
22
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
23
|
requirements:
|
43
|
-
- - ~>
|
24
|
+
- - "~>"
|
44
25
|
- !ruby/object:Gem::Version
|
45
26
|
version: '2.12'
|
46
27
|
- !ruby/object:Gem::Dependency
|
47
|
-
prerelease: false
|
48
|
-
type: :development
|
49
28
|
name: rspectacular
|
50
29
|
requirement: !ruby/object:Gem::Requirement
|
51
|
-
none: false
|
52
30
|
requirements:
|
53
|
-
- - ~>
|
31
|
+
- - "~>"
|
54
32
|
- !ruby/object:Gem::Version
|
55
33
|
version: '0.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
56
36
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
37
|
requirements:
|
59
|
-
- - ~>
|
38
|
+
- - "~>"
|
60
39
|
- !ruby/object:Gem::Version
|
61
40
|
version: '0.11'
|
62
41
|
description: Makes generating Null Objects easy.
|
@@ -66,8 +45,12 @@ extensions: []
|
|
66
45
|
extra_rdoc_files:
|
67
46
|
- README.md
|
68
47
|
files:
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- lib/null_and_void.rb
|
69
51
|
- lib/null_and_void/convertible.rb
|
70
52
|
- lib/null_and_void/falsifiable.rb
|
53
|
+
- lib/null_and_void/inflections.rb
|
71
54
|
- lib/null_and_void/introspectable.rb
|
72
55
|
- lib/null_and_void/maybe.rb
|
73
56
|
- lib/null_and_void/model_support.rb
|
@@ -75,9 +58,6 @@ files:
|
|
75
58
|
- lib/null_and_void/nullified.rb
|
76
59
|
- lib/null_and_void/stubbable.rb
|
77
60
|
- lib/null_and_void/version.rb
|
78
|
-
- lib/null_and_void.rb
|
79
|
-
- Rakefile
|
80
|
-
- README.md
|
81
61
|
- spec/convertible_spec.rb
|
82
62
|
- spec/falsifiable_spec.rb
|
83
63
|
- spec/introspectable_spec.rb
|
@@ -89,28 +69,27 @@ files:
|
|
89
69
|
- spec/stubbable_spec.rb
|
90
70
|
homepage: https://github.com/jfelchner/null_and_void
|
91
71
|
licenses: []
|
72
|
+
metadata: {}
|
92
73
|
post_install_message:
|
93
74
|
rdoc_options:
|
94
|
-
- --charset = UTF-8
|
75
|
+
- "--charset = UTF-8"
|
95
76
|
require_paths:
|
96
77
|
- lib
|
97
78
|
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
-
none: false
|
99
79
|
requirements:
|
100
|
-
- -
|
80
|
+
- - ">="
|
101
81
|
- !ruby/object:Gem::Version
|
102
82
|
version: '0'
|
103
83
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
-
none: false
|
105
84
|
requirements:
|
106
|
-
- -
|
85
|
+
- - ">="
|
107
86
|
- !ruby/object:Gem::Version
|
108
87
|
version: '0'
|
109
88
|
requirements: []
|
110
89
|
rubyforge_project: null_and_void
|
111
|
-
rubygems_version:
|
90
|
+
rubygems_version: 2.2.0
|
112
91
|
signing_key:
|
113
|
-
specification_version:
|
92
|
+
specification_version: 4
|
114
93
|
summary: Easy Null Objects
|
115
94
|
test_files:
|
116
95
|
- spec/convertible_spec.rb
|