nil_conditional 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 480a9c37b75244a79017defb5f87a09ec00bb4d2
4
- data.tar.gz: c8ade086f96dccf7122da47898c418e923851579
3
+ metadata.gz: 0fc18e077b2f985f98ac155b7aa1517b02d2b0ca
4
+ data.tar.gz: 807b525fb2bb0f95665213975d00b9937a4aa9e9
5
5
  SHA512:
6
- metadata.gz: 4cecbcded18c99be80b8a9ba7e69cc11bc6d5eeee237b67f5013f4f8e3c43027ca5d8e18220aad045d22decba661a971d027a8dd65fcbab69a2bbb48cd5709d6
7
- data.tar.gz: 40bb5fd2b473e6fdcd3635cdff15aba19d7c2007b373efbe64e770995276192a860329d8e7ebebc9de0f0bf283ee66cd7af12df2baaeddfecaa6718719ab894b
6
+ metadata.gz: b8ab231f5f82c32c5fd62b0b3f8078555049cd3d7dc5eb9a2a422d831f9ee48e32e9e819d12ae572a3ba7018af1843771265ae56a1949538f13c7d95dec35150
7
+ data.tar.gz: 4e1632f12d50a069259c1ea3165d382fa4c258b6fc48252bf1ac7dc19a20e2e59a85cfdf99629c48867b43d4c0b72a0c3d85722c541d8e6cb828126e59d52ff2
data/README.md CHANGED
@@ -16,8 +16,8 @@ Nil Conditional Operator is inspired by Null Conditional Operator introduced in
16
16
 
17
17
  ## Usage
18
18
 
19
- Use preceding double underscore (`__`) for methods (for ex. `__method`)
20
- Use preceding double underscore and trailing block for local variables (for ex. `__var{}`)
19
+ Use `?_` method as Nil Conditional Operator.
20
+ For example: `logger._?.log('some debug information')`
21
21
 
22
22
  ```ruby
23
23
 
@@ -25,53 +25,60 @@ Use preceding double underscore and trailing block for local variables (for ex.
25
25
  logger.foo.bar.car.cow
26
26
  NoMethodError: undefined method `foo` for #<NilClass>
27
27
 
28
- # logger is nil or doesn't exist
29
- __logger{}.foo.bar.car.cow
28
+ # logger is nil
29
+ logger._?.foo.bar.car.cow
30
30
  => #<NilConditional>
31
31
 
32
- # object exists and all methods are valid
33
- __object{}.foo.bar.car.cow
32
+ # logger exists and all methods are valid
33
+ logger._?.foo.bar.car.cow
34
+ => "moooo"
35
+
36
+ # logger exists and all methods are valid
37
+ logger._?.foo.bar.car.cow
34
38
  => "moooo"
35
39
 
36
40
  # logger exists but doesn't have warn method
37
- logger.__warn('some warning')
41
+ logger._?.warn('some warning')
38
42
  => #<NilConditional>
39
43
 
40
- __non_existent_local_variable{}
41
- => #<NilConditional>
44
+ # logger exists and have warn method, but foo is invalid
45
+ logger._?.warn('some warning').foo
46
+ => NoMethodError: undefined method `foo`
42
47
 
43
- __non_existent_method
48
+ logger._?.warn('some warning')._?.foo
44
49
  => #<NilConditional>
45
50
 
46
51
  Object.new.non_existent_method
47
52
  NoMethodError: undefined method `non_existent_method` for #<Object>
48
53
 
49
- Object.new.__non_existent_method
54
+ Object.new._?.non_existent_method
50
55
  => #<NilConditional>
51
56
 
52
- Object.new.__non_existent_method.nil?
57
+ Object.new._?.non_existent_method.nil?
53
58
  => true
54
-
55
- __new_object.foo.bar.car_?.cow_?
56
- => nil
57
59
  ```
58
60
 
59
- `NilConditional` instanced return always new NilConditional object if method is missing.
61
+ `NilConditional` instances always return new NilConditional object if method is missing.
60
62
  These objects are also `eql` to `nil`.
61
63
 
62
64
 
63
65
  ## Changelog
64
66
 
67
+ * Changes from version with major 1
68
+
69
+ Previous version used different syntax
70
+
65
71
  * Changes from version with major 0
66
72
 
67
- Previous version didn't support methods with special characters and there was no support for local variables
68
- Previous version used trailing `_?` as NilConditional operator. Current version uses preceding double underscore character `__`.
73
+ * Previous version didn't support methods with special characters and there was no support for local variables.
74
+ * Previous version used trailing `_?` as NilConditional operator. Current version uses preceding double underscore character `__`.
69
75
 
70
76
 
71
77
  ## Discussion
72
78
 
73
79
  Feel free to submit ideas via issues and discuss better solution to Nil Conditional Operator in Ruby
74
80
 
81
+
75
82
  ## License
76
83
 
77
84
  This is free software, licensed under MIT License. See LICENSE.txt file.
data/Rakefile CHANGED
@@ -1,5 +1,13 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
  require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
3
4
 
4
5
  RSpec::Core::RakeTask.new('spec')
5
- task :default => :spec
6
+ RuboCop::RakeTask.new
7
+
8
+ task :test do
9
+ Rake::Task['spec'].invoke
10
+ Rake::Task['rubocop'].invoke
11
+ end
12
+
13
+ task default: :test
@@ -1,34 +1,29 @@
1
1
  # NilConditional Operator in Ruby
2
2
 
3
+ # Patch Object class
3
4
  class Object
4
- def method_missing(name, *params)
5
- if name =~ /^__.+$/
6
- original = name.to_s.sub(/^__/, '').to_sym
7
-
8
- if block_given? && original =~ /^\w+$/
9
- binding = Proc.new.binding
10
- if binding.local_variable_defined?(original)
11
- var = binding.local_variable_get(original)
12
- return var unless var.nil?
13
- end
14
- end
15
-
16
- if respond_to?(original)
17
- return_value = block_given? ? send(original, *params, &Proc.new) :
18
- send(original, *params)
19
- return return_value unless return_value.nil?
20
- end
21
-
22
- NilConditional.new
23
- else
24
- super
25
- end
5
+ def _?
6
+ NilConditional.new(self)
26
7
  end
27
8
  end
28
9
 
10
+ # Nil Conditional class
29
11
  class NilConditional
12
+ def initialize(object)
13
+ @object = object
14
+ end
15
+
30
16
  def method_missing(name, *params)
31
- return self.class.new
17
+ if @object.respond_to?(name)
18
+ return_value = nil
19
+ if block_given?
20
+ return_value = @object.public_send(name, *params, &Proc.new)
21
+ else
22
+ return_value = @object.public_send(name, *params)
23
+ end
24
+ return return_value unless return_value.nil?
25
+ end
26
+ self.class.new(@object)
32
27
  end
33
28
 
34
29
  def nil?
@@ -36,7 +31,7 @@ class NilConditional
36
31
  end
37
32
 
38
33
  def ==(other)
39
- return true if other == nil
34
+ return true if other.nil?
40
35
  super
41
36
  end
42
37
 
@@ -3,20 +3,21 @@ lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
- spec.name = "nil_conditional"
7
- spec.version = '1.0.0'
8
- spec.authors = ["Grzegorz Bizon"]
9
- spec.email = ["grzegorz.bizon@ntsn.pl"]
10
- spec.summary = %q{Nil Conditional Operator in Ruby}
11
- spec.homepage = "http://github.com/grzesiek/nil-conditional"
12
- spec.license = "MIT"
6
+ spec.name = 'nil_conditional'
7
+ spec.version = '2.0.0'
8
+ spec.authors = ['Grzegorz Bizon']
9
+ spec.email = ['grzegorz.bizon@ntsn.pl']
10
+ spec.summary = 'Nil Conditional Operator in Ruby'
11
+ spec.homepage = 'http://github.com/grzesiek/nil-conditional'
12
+ spec.license = 'MIT'
13
13
 
14
14
  spec.files = `git ls-files -z`.split("\x0")
15
15
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
16
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
- spec.require_paths = ["lib"]
17
+ spec.require_paths = ['lib']
18
18
 
19
- spec.add_development_dependency "bundler", "~> 1.7"
20
- spec.add_development_dependency "rake", "~> 10.0"
21
- spec.add_development_dependency "rspec", "~> 3.2.0"
19
+ spec.add_development_dependency 'bundler', '~> 1.7'
20
+ spec.add_development_dependency 'rake', '~> 10.0'
21
+ spec.add_development_dependency 'rspec', '~> 3.2.0'
22
+ spec.add_development_dependency 'rubocop', '~> 0.30.0'
22
23
  end
@@ -1,15 +1,16 @@
1
1
  describe NilConditional do
2
2
  context 'nil value' do
3
3
  it 'should be considered as nil' do
4
- expect(NilConditional.new.nil?).to be(true)
5
- expect(NilConditional.new).to eq(nil)
6
- expect(NilConditional.new).to eql(nil)
4
+ expect(NilConditional.new(Object.new).nil?).to be(true)
5
+ expect(NilConditional.new(Object.new)).to eq(nil)
6
+ expect(NilConditional.new(Object.new)).to eql(nil)
7
7
  end
8
8
  end
9
9
 
10
10
  context 'missing method behavior' do
11
11
  it 'return new NilConditional object when method is missing' do
12
- expect(NilConditional.new.non_existent_method).to be_a(NilConditional)
12
+ conditional = NilConditional.new(Object.new)
13
+ expect(conditional.non_existent_method).to be_a(NilConditional)
13
14
  end
14
15
  end
15
16
  end
data/spec/nil_spec.rb CHANGED
@@ -1,8 +1,7 @@
1
1
  describe 'nil object' do
2
2
  context 'local variable' do
3
3
  it 'should return nil conditional if var is nil' do
4
- var = nil
5
- expect(__var{}).to be_a(NilConditional)
4
+ expect(nil._?).to be_a(NilConditional)
6
5
  end
7
6
  end
8
7
 
@@ -13,11 +12,11 @@ describe 'nil object' do
13
12
 
14
13
  it 'should return NilConditional instance when method return nil' do
15
14
  expect(Object.nil_test).to be nil
16
- expect(Object.new.__nil_test?).to be_a(NilConditional)
15
+ expect(Object.new._?.nil_test).to be_a(NilConditional)
17
16
  end
18
17
 
19
- it 'should return NilConditional instance when method return nil and uses block' do
20
- expect(Object.new.__nil_test?{}).to be_a(NilConditional)
18
+ it 'should return NilConditional when method returns nil using block' do
19
+ expect(Object.new._?.nil_test {}).to be_a(NilConditional)
21
20
  end
22
21
  end
23
22
  end
data/spec/object_spec.rb CHANGED
@@ -4,27 +4,27 @@ describe Object do
4
4
  expect { Object.new.test_method }.to raise_error(NoMethodError)
5
5
  end
6
6
 
7
- it 'should not raise error when receiving __test_method' do
8
- expect { Object.new.__test_method }.to_not raise_error
7
+ it 'should not raise error when receiving test_method' do
8
+ expect { Object.new._?.test_method }.to_not raise_error
9
9
  end
10
10
 
11
- it 'should return NilConditional instance when received non existent method with preceding __' do
12
- expect(Object.new.__test_method).to be_a(NilConditional)
11
+ it 'should return NilConditional instance when received unknown method' do
12
+ expect(Object.new._?.test_method).to be_a(NilConditional)
13
13
  end
14
14
 
15
15
  it 'should support chained methods with nil conditional' do
16
- expect { Object.new.__test_method.foo_?.bar_?.car_?.cow_? }.to_not raise_error
17
- expect(Object.new.__test_method.foo_?.bar_?.car_?.cow_?).to be_a(NilConditional)
16
+ expect { Object.new._?.test_method.foo.bar.car.cow }.to_not raise_error
17
+ expect(Object.new._?.test_method.foo.bar.car.cow).to be_a(NilConditional)
18
18
  end
19
19
 
20
20
  it 'should support methods with arguments' do
21
- expect { Object.__test_method_with_args(1, 2, 3) }.to_not raise_error
22
- expect(Object.__test_method_with_args(1, 2, 3)).to be_a(NilConditional)
21
+ expect { Object._?.test_method_with_args(1, 2, 3) }.to_not raise_error
22
+ expect(Object._?.test_method_with_args(1, 2, 3)).to be_a(NilConditional)
23
23
  end
24
24
 
25
25
  it 'should support methods with blocks' do
26
- expect(Object.__test_method_with_block{ a = 'b' }).to be_a(NilConditional)
27
- expect(['a', 'b'].__delete_if { |i| i == 'b' }).to eq([ 'a' ])
26
+ expect(Object._?.test_with_block { 'test' }).to be_a(NilConditional)
27
+ expect(%w(a b)._?.delete_if { |i| i == 'b' }).to eq(['a'])
28
28
  end
29
29
  end
30
30
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1 @@
1
1
  require 'nil_conditional'
2
-
3
- RSpec.configure do |config|
4
- end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nil_conditional
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grzegorz Bizon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-02 00:00:00.000000000 Z
11
+ date: 2015-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 3.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.30.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.30.0
55
69
  description:
56
70
  email:
57
71
  - grzegorz.bizon@ntsn.pl
@@ -68,7 +82,6 @@ files:
68
82
  - Rakefile
69
83
  - lib/nil_conditional.rb
70
84
  - nil_conditional.gemspec
71
- - spec/local_variables_spec.rb
72
85
  - spec/nil_conditional_spec.rb
73
86
  - spec/nil_spec.rb
74
87
  - spec/object_spec.rb
@@ -98,8 +111,8 @@ signing_key:
98
111
  specification_version: 4
99
112
  summary: Nil Conditional Operator in Ruby
100
113
  test_files:
101
- - spec/local_variables_spec.rb
102
114
  - spec/nil_conditional_spec.rb
103
115
  - spec/nil_spec.rb
104
116
  - spec/object_spec.rb
105
117
  - spec/spec_helper.rb
118
+ has_rdoc:
@@ -1,20 +0,0 @@
1
- describe 'local variables' do
2
- context 'test variable' do
3
- it 'should raise error when using non existent test variable' do
4
- expect { test_variable }.to raise_error(NameError)
5
- end
6
-
7
- it 'should not raise error when using __test_variable{}' do
8
- expect { __test_variable{} }.to_not raise_error
9
- end
10
-
11
- it 'should return NilConditional instance when using non existent var with preceding __ and trailing block' do
12
- expect(__test_variable{}).to be_a(NilConditional)
13
- end
14
-
15
- it 'should support chained methods with nil conditional' do
16
- expect { __test_variable{}.foo_?.bar_?.car_?.cow_? }.to_not raise_error
17
- expect(__test_variable{}.foo_?.bar_?.car_?.cow_?).to be_a(NilConditional)
18
- end
19
- end
20
- end