nigilist 0.1.0 → 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/.travis.yml +1 -0
- data/CHANGELOG.md +10 -0
- data/README.md +14 -10
- data/VERSION +1 -1
- data/lib/nigilist.rb +25 -8
- data/nigilist.gemspec +6 -4
- data/test/nihilist_on_rails_test.rb +56 -0
- data/test/nihilist_test.rb +37 -9
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a586c6911dc9db595a024023cc0ce60a71fb4fc9dbc550206d13249bc6108d37
|
4
|
+
data.tar.gz: cea1e62f69d40782d265d169db670526083d92d98fc7287e9a271994268cc7e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf85e5252552d1e1efdb39dfa22e37d0809c53253c60d63bc7cafb7335e0835eb318902f3b2b835d891830a87934befec057ad4fef995b6e206b7d704ce20aaa
|
7
|
+
data.tar.gz: cc5a1445e53c58daa7ec9ec98463f0bf8f7d31d0aa3e539e22033f29ed6e2730064584a4cac11aac945ed84ca987282449230a1685b7b871456c22ad27f887cb
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
## 1.0.0
|
2
|
+
|
3
|
+
### Features
|
4
|
+
|
5
|
+
- [**BREAKING**] For Ruby on Rails plural method call now return ActiveRecord's model null relation if model with same singular name exists, otherwise return `[]` ([#6](https://github.com/aishek/nihilist/pull/6))
|
6
|
+
- `respond_to?` now return `true` if method call handled by nigilist ([#6](https://github.com/aishek/nihilist/pull/6))
|
7
|
+
|
8
|
+
### Fixes
|
9
|
+
|
10
|
+
- [**BREAKING**] Methods with plural name and underscore at the end (for example `monkeys_`) now return `nil` instead of `[]` ([#6](https://github.com/aishek/nihilist/pull/6))
|
data/README.md
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
[![Code Climate](https://codeclimate.com/github/aishek/nihilist.svg)](https://codeclimate.com/github/aishek/nihilist)
|
6
6
|
|
7
7
|
|
8
|
-
Nigilist is extremely useful for implement Null Object pattern. It allows null object to return `false` for all question-methods, `nil` for all non-bang-methods, `[]` for all methods in plural form without defining them.
|
8
|
+
Nigilist is extremely useful for implement Null Object pattern. It allows null object to return `false` for all question-methods, `nil` for all non-bang-methods, `[]` for all methods in plural form (and in Ruby on Rails null-relation for same name ActiveRecord model) without defining them.
|
9
9
|
|
10
10
|
## Installation
|
11
11
|
|
@@ -16,6 +16,9 @@ gem install nigilist
|
|
16
16
|
## Usage
|
17
17
|
|
18
18
|
```ruby
|
19
|
+
class Order < ActiveRecord::Base
|
20
|
+
end
|
21
|
+
|
19
22
|
class Guest
|
20
23
|
include Nigilist
|
21
24
|
|
@@ -24,16 +27,17 @@ class Guest
|
|
24
27
|
end
|
25
28
|
|
26
29
|
def locale
|
27
|
-
'ru'
|
30
|
+
'ru'
|
28
31
|
end
|
29
32
|
end
|
30
33
|
|
31
34
|
current_user = Guest.new
|
32
|
-
current_user.admin?
|
33
|
-
current_user.polite? # true
|
34
|
-
current_user.
|
35
|
-
current_user.
|
36
|
-
current_user.
|
35
|
+
current_user.admin? # false — for all question methods
|
36
|
+
current_user.polite? # true — becase it is explicit defined
|
37
|
+
current_user.orders # Order.none — for all plural methods because same singular name ActiveRecord model exists
|
38
|
+
current_user.planets # [] — for all plural methods without because singular name ActiveRecord model doesn't exists
|
39
|
+
current_user.address # nil — for all non question, non plural and non bang methods
|
40
|
+
current_user.locale # 'ru' — becase it is explicit defined
|
37
41
|
```
|
38
42
|
|
39
43
|
### Explanation of Null Object pattern
|
@@ -51,15 +55,15 @@ current_user = current_session.user || Guest.new
|
|
51
55
|
current_user.admin?
|
52
56
|
```
|
53
57
|
|
54
|
-
So when your project
|
58
|
+
So when your project grows and other methods got it place inside `User` model you have to define same methods in `Guest` model which contains no logic but only return `false` or `nil`. In that case Nigilist could be helpful.
|
55
59
|
|
56
60
|
## License
|
57
61
|
|
58
|
-
Nigilist is free software, and may be redistributed under the
|
62
|
+
Nigilist is free software, and may be redistributed under the MIT License.
|
59
63
|
|
60
64
|
## Credits
|
61
65
|
|
62
|
-
Gem nigilist is maintained by [Cifronomika](
|
66
|
+
Gem nigilist is maintained by [Cifronomika](https://cifronomika.com/).
|
63
67
|
|
64
68
|
Contributors:
|
65
69
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
1.0.0
|
data/lib/nigilist.rb
CHANGED
@@ -1,14 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Nigilist
|
2
4
|
def method_missing(message, *args, &block)
|
3
5
|
method_name = message.to_s
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
6
|
+
|
7
|
+
return false if method_name.end_with?('?')
|
8
|
+
|
9
|
+
if method_name.end_with?('s') && method_name[-2..-1] != 'ss'
|
10
|
+
if defined?(Rails)
|
11
|
+
ar_relation_klass = method_name.singularize.camelize.safe_constantize
|
12
|
+
return ar_relation_klass.none if ar_relation_klass && ar_relation_klass.respond_to?(:none)
|
13
|
+
end
|
14
|
+
|
15
|
+
return []
|
12
16
|
end
|
17
|
+
|
18
|
+
return nil unless method_name.end_with?('!')
|
19
|
+
|
20
|
+
super
|
21
|
+
end
|
22
|
+
|
23
|
+
def respond_to_missing?(message, include_private = false)
|
24
|
+
method_name = message.to_s
|
25
|
+
|
26
|
+
method_name.end_with?('?') ||
|
27
|
+
(method_name.end_with?('s') && method_name[-2..-1] != 'ss') ||
|
28
|
+
!method_name.end_with?('!') ||
|
29
|
+
super
|
13
30
|
end
|
14
31
|
end
|
data/nigilist.gemspec
CHANGED
@@ -2,17 +2,17 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: nigilist
|
5
|
+
# stub: nigilist 1.0.0 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "nigilist".freeze
|
9
|
-
s.version = "
|
9
|
+
s.version = "1.0.0"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
12
|
s.metadata = { "allowed_push_host" => "https://rubygems.org" } if s.respond_to? :metadata=
|
13
13
|
s.require_paths = ["lib".freeze]
|
14
14
|
s.authors = ["Alexandr Borisov".freeze]
|
15
|
-
s.date = "
|
15
|
+
s.date = "2019-07-10"
|
16
16
|
s.description = "After include Nigilist module all ?-ends methods will return false, all non-!-ends methods will return nil.".freeze
|
17
17
|
s.email = "ab@cifronomika.ru".freeze
|
18
18
|
s.extra_rdoc_files = [
|
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
|
|
20
20
|
]
|
21
21
|
s.files = [
|
22
22
|
".travis.yml",
|
23
|
+
"CHANGELOG.md",
|
23
24
|
"Gemfile",
|
24
25
|
"Gemfile.lock",
|
25
26
|
"LISENCE.txt",
|
@@ -28,11 +29,12 @@ Gem::Specification.new do |s|
|
|
28
29
|
"VERSION",
|
29
30
|
"lib/nigilist.rb",
|
30
31
|
"nigilist.gemspec",
|
32
|
+
"test/nihilist_on_rails_test.rb",
|
31
33
|
"test/nihilist_test.rb"
|
32
34
|
]
|
33
35
|
s.homepage = "https://github.com/aishek/nihilist".freeze
|
34
36
|
s.licenses = ["MIT".freeze]
|
35
|
-
s.rubygems_version = "
|
37
|
+
s.rubygems_version = "3.0.3".freeze
|
36
38
|
s.summary = "Nigilist simplifies NullObject pattern classes".freeze
|
37
39
|
|
38
40
|
if s.respond_to? :specification_version then
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'minitest/pride'
|
5
|
+
require 'minitest/power_assert'
|
6
|
+
|
7
|
+
require 'nigilist'
|
8
|
+
|
9
|
+
class Guest
|
10
|
+
include Nigilist
|
11
|
+
|
12
|
+
def costs
|
13
|
+
{}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class NigilistRailsTest < Minitest::Test
|
18
|
+
def setup
|
19
|
+
Object.const_set('Rails', Module.new)
|
20
|
+
|
21
|
+
String.send(:define_method, :singularize) do self[0..-2] end
|
22
|
+
String.send(:define_method, :camelize) do capitalize end
|
23
|
+
String.send(:define_method, :safe_constantize) do
|
24
|
+
if self == 'Post'
|
25
|
+
return Class.new do
|
26
|
+
def self.none
|
27
|
+
:fake_active_record_null_relation
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
|
34
|
+
@guest = Guest.new
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_explicit_plural_method_without_active_record_model_return_its_value
|
38
|
+
assert { @guest.costs == {} }
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_plural_method_without_active_record_model_return_empty_array
|
42
|
+
assert { @guest.planets == [] }
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_plural_method_with_active_record_model_return_null_relation
|
46
|
+
assert { @guest.posts == :fake_active_record_null_relation }
|
47
|
+
end
|
48
|
+
|
49
|
+
def teardown
|
50
|
+
Object.send(:remove_const, 'Rails')
|
51
|
+
|
52
|
+
String.send(:remove_method, :singularize)
|
53
|
+
String.send(:remove_method, :camelize)
|
54
|
+
String.send(:remove_method, :safe_constantize)
|
55
|
+
end
|
56
|
+
end
|
data/test/nihilist_test.rb
CHANGED
@@ -7,12 +7,16 @@ require 'nigilist'
|
|
7
7
|
class Guest
|
8
8
|
include Nigilist
|
9
9
|
|
10
|
+
def hosts
|
11
|
+
:all
|
12
|
+
end
|
13
|
+
|
10
14
|
def polite?
|
11
15
|
true
|
12
16
|
end
|
13
17
|
|
14
18
|
def planet
|
15
|
-
'Earth'
|
19
|
+
'Earth'
|
16
20
|
end
|
17
21
|
end
|
18
22
|
|
@@ -21,31 +25,55 @@ class NigilistTest < Minitest::Test
|
|
21
25
|
@guest = Guest.new
|
22
26
|
end
|
23
27
|
|
24
|
-
def
|
28
|
+
def test_question_end_method_return_false
|
25
29
|
assert { @guest.admin? == false }
|
26
30
|
end
|
27
31
|
|
28
|
-
def
|
32
|
+
def test_explicit_defined_question_end_method_return_its_result
|
29
33
|
assert { @guest.polite? == true }
|
30
34
|
end
|
31
35
|
|
32
|
-
def
|
36
|
+
def test_non_bang_end_method_return_nil
|
33
37
|
assert { @guest.name.nil? }
|
34
38
|
end
|
35
39
|
|
36
|
-
def
|
40
|
+
def test_explicit_non_bang_end_method_return_its_result
|
37
41
|
assert { @guest.planet == 'Earth' }
|
38
42
|
end
|
39
43
|
|
40
|
-
def
|
44
|
+
def test_plural_method_return_empty_array
|
41
45
|
assert { @guest.planets == [] }
|
42
46
|
end
|
43
47
|
|
44
|
-
def
|
48
|
+
def test_plural_method_with_undersores_return_empty_array
|
45
49
|
assert { @guest.planets_and_monkeys == [] }
|
46
50
|
end
|
47
51
|
|
48
|
-
def
|
49
|
-
assert { @guest.
|
52
|
+
def test_explicit_plural_method_return_its_result
|
53
|
+
assert { @guest.hosts == :all }
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_plural_method_with_undersore_at_the_end_return_nil
|
57
|
+
assert { @guest.monkeys_ == nil }
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_method_with_doudle_s_ending_return_nil
|
61
|
+
assert { @guest.stress == nil }
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_respond_to_plural_method
|
65
|
+
assert { @guest.respond_to?(:socks) }
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_respond_to_question_method
|
69
|
+
assert { @guest.respond_to?(:intresting?) }
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_respond_to_regular_method
|
73
|
+
assert { @guest.respond_to?(:mess) }
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_does_not_respond_to_bang_method
|
77
|
+
assert { !@guest.respond_to?(:catch!) }
|
50
78
|
end
|
51
79
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nigilist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexandr Borisov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -89,6 +89,7 @@ extra_rdoc_files:
|
|
89
89
|
- README.md
|
90
90
|
files:
|
91
91
|
- ".travis.yml"
|
92
|
+
- CHANGELOG.md
|
92
93
|
- Gemfile
|
93
94
|
- Gemfile.lock
|
94
95
|
- LISENCE.txt
|
@@ -97,6 +98,7 @@ files:
|
|
97
98
|
- VERSION
|
98
99
|
- lib/nigilist.rb
|
99
100
|
- nigilist.gemspec
|
101
|
+
- test/nihilist_on_rails_test.rb
|
100
102
|
- test/nihilist_test.rb
|
101
103
|
homepage: https://github.com/aishek/nihilist
|
102
104
|
licenses:
|
@@ -118,8 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
120
|
- !ruby/object:Gem::Version
|
119
121
|
version: '0'
|
120
122
|
requirements: []
|
121
|
-
|
122
|
-
rubygems_version: 2.7.3
|
123
|
+
rubygems_version: 3.0.3
|
123
124
|
signing_key:
|
124
125
|
specification_version: 4
|
125
126
|
summary: Nigilist simplifies NullObject pattern classes
|