ruby-try 1.0.2 → 1.1.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.
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  Ruby Try
2
2
  ========
3
3
  [![Gem Version](https://badge.fury.io/rb/ruby-try.svg)](http://badge.fury.io/rb/ruby-try)
4
+ [![Dependency Status](https://gemnasium.com/OrelSokolov/ruby-try.svg)](https://gemnasium.com/OrelSokolov/ruby-try)
4
5
  [![Code Climate](https://codeclimate.com/github/OrelSokolov/ruby-try/badges/gpa.svg)](https://codeclimate.com/github/OrelSokolov/ruby-try)
5
6
 
6
7
  This gem has two versions of `try()`.
@@ -1,5 +1,20 @@
1
+ module RubyTry
2
+ def try?(*args)
3
+ if args.first =~ /[?]$/
4
+ if respond_to?(args.first)
5
+ public_send(*args)
6
+ else
7
+ false
8
+ end
9
+ else
10
+ raise ArgumentError, "For non-boolean methods use only try()"
11
+ end
12
+ end
13
+ end
14
+
1
15
  class Object
2
- # Invokes the public method whose name goes as first argument just like
16
+ include RubyTry
17
+ # Invokes the public method whose name goes as first argument just like
3
18
  # +public_send+ does, except that if the receiver does not respond to it the
4
19
  # call returns +nil+ rather than raising an exception.
5
20
  #
@@ -56,22 +71,11 @@ class Object
56
71
  end
57
72
  end
58
73
 
59
- def try?(*a, &b)
60
- if a.empty? && block_given?
61
- yield self
62
- else
63
- if respond_to?(a.first)
64
- public_send(*a, &b)
65
- else
66
- nil.try?(*a, &b)
67
- end
68
- end
69
- end
70
-
71
74
  end
72
75
 
73
76
  class NilClass
74
- # Calling +try+ on +nil+ always returns +nil+.
77
+ include RubyTry
78
+ # Calling +try+ on +nil+ always returns +nil+.
75
79
  # It becomes specially helpful when navigating through associations that may return +nil+.
76
80
  #
77
81
  # nil.try(:name) # => nil
@@ -88,15 +92,4 @@ class NilClass
88
92
  def try!(*args)
89
93
  nil
90
94
  end
91
-
92
- # Calling +try?+ on +nil+ returns +false+
93
- # With +try?+
94
- # @person.try(:children).try(:first).try?(:has_dark_hairs?)
95
- def try?(*args)
96
- if args.first =~ /[?]$/
97
- false
98
- else
99
- raise ArgumentError, "For non-boolean methods use only try()"
100
- end
101
- end
102
95
  end
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'ruby-try'
7
- spec.version = '1.0.2'
7
+ spec.version = '1.1.0'
8
8
  spec.date = '2014-08-02'
9
9
  spec.summary = "Provides RoR try() and extends it by new try?() method."
10
10
  spec.description = "Provides RoR try() and extends it by new try?() method."
@@ -153,8 +153,8 @@ describe NilClass do
153
153
  context "for existing method" do
154
154
  include_context "#try?, method exists"
155
155
 
156
- it "should return false for boolean method" do
157
- subject.try?(:zero?).should eq(false)
156
+ it "should return value for boolean method" do
157
+ subject.try?(:zero?).should eq(true)
158
158
  end
159
159
  end
160
160
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-try
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: