buff-extensions 0.4.0 → 0.5.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.
@@ -1 +1,2 @@
1
1
  require_relative 'object/blank'
2
+ require_relative 'object/try'
@@ -0,0 +1,81 @@
1
+ # Borrowed and modified from
2
+ # {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/try.rb}
3
+
4
+ class Object
5
+ # Invokes the public method whose name goes as first argument just like
6
+ # +public_send+ does, except that if the receiver does not respond to it the
7
+ # call returns +nil+ rather than raising an exception.
8
+ #
9
+ # This method is defined to be able to write
10
+ #
11
+ # @person.try(:name)
12
+ #
13
+ # instead of
14
+ #
15
+ # @person ? @person.name : nil
16
+ #
17
+ # +try+ returns +nil+ when called on +nil+ regardless of whether it responds
18
+ # to the method:
19
+ #
20
+ # nil.try(:to_i) # => nil, rather than 0
21
+ #
22
+ # Arguments and blocks are forwarded to the method if invoked:
23
+ #
24
+ # @posts.try(:each_slice, 2) do |a, b|
25
+ # ...
26
+ # end
27
+ #
28
+ # The number of arguments in the signature must match. If the object responds
29
+ # to the method the call is attempted and +ArgumentError+ is still raised
30
+ # otherwise.
31
+ #
32
+ # If +try+ is called without arguments it yields the receiver to a given
33
+ # block unless it is +nil+:
34
+ #
35
+ # @person.try do |p|
36
+ # ...
37
+ # end
38
+ #
39
+ # Please also note that +try+ is defined on +Object+, therefore it won't work
40
+ # with instances of classes that do not have +Object+ among their ancestors,
41
+ # like direct subclasses of +BasicObject+. For example, using +try+ with
42
+ # +SimpleDelegator+ will delegate +try+ to the target instead of calling it on
43
+ # delegator itself.
44
+ def try(*a, &b)
45
+ if a.empty? && block_given?
46
+ yield self
47
+ else
48
+ public_send(*a, &b) if respond_to?(a.first)
49
+ end
50
+ end
51
+
52
+ # Same as #try, but will raise a NoMethodError exception if the receiving is not nil and
53
+ # does not implemented the tried method.
54
+ def try!(*a, &b)
55
+ if a.empty? && block_given?
56
+ yield self
57
+ else
58
+ public_send(*a, &b)
59
+ end
60
+ end
61
+ end
62
+
63
+ class NilClass
64
+ # Calling +try+ on +nil+ always returns +nil+.
65
+ # It becomes specially helpful when navigating through associations that may return +nil+.
66
+ #
67
+ # nil.try(:name) # => nil
68
+ #
69
+ # Without +try+
70
+ # @person && !@person.children.blank? && @person.children.first.name
71
+ #
72
+ # With +try+
73
+ # @person.try(:children).try(:first).try(:name)
74
+ def try(*args)
75
+ nil
76
+ end
77
+
78
+ def try!(*args)
79
+ nil
80
+ end
81
+ end
@@ -1,5 +1,5 @@
1
1
  module Buff
2
2
  module Extensions
3
- VERSION = "0.4.0"
3
+ VERSION = "0.5.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buff-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-18 00:00:00.000000000 Z
12
+ date: 2013-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: buff-ruby_engine
@@ -201,6 +201,7 @@ files:
201
201
  - lib/buff/extensions/kernel/reporting.rb
202
202
  - lib/buff/extensions/object.rb
203
203
  - lib/buff/extensions/object/blank.rb
204
+ - lib/buff/extensions/object/try.rb
204
205
  - lib/buff/extensions/string.rb
205
206
  - lib/buff/extensions/string/inflections.rb
206
207
  - lib/buff/extensions/version.rb
@@ -227,7 +228,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
227
228
  version: '0'
228
229
  segments:
229
230
  - 0
230
- hash: -1420043020667632926
231
+ hash: -1972530205168263660
231
232
  requirements: []
232
233
  rubyforge_project:
233
234
  rubygems_version: 1.8.23