ruby-try 1.0.0 → 1.0.1

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/Rakefile CHANGED
@@ -1,2 +1,21 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
2
3
 
4
+ RSpec::Core::RakeTask.new(:ensure_all_specs_pass)
5
+
6
+
7
+ task :ensure_all_commited do
8
+ untracked = `git ls-files --others --exclude-standard`
9
+ modified = `git ls-files -m`
10
+ uncommited_changed = `git diff --shortstat --staged`
11
+ if untracked.length > 0 || modified.length > 0 || uncommited_changed.length > 0
12
+ abort("Commit your changes first and leave directory clean.")
13
+ end
14
+ end
15
+
16
+ task :check
17
+
18
+ task :check => :ensure_all_commited
19
+ task :check => :ensure_all_specs_pass
20
+
21
+ task :build => :check
@@ -1,4 +1,43 @@
1
1
  class Object
2
+ # Invokes the public method whose name goes as first argument just like
3
+ # +public_send+ does, except that if the receiver does not respond to it the
4
+ # call returns +nil+ rather than raising an exception.
5
+ #
6
+ # This method is defined to be able to write
7
+ #
8
+ # @person.try(:name)
9
+ #
10
+ # instead of
11
+ #
12
+ # @person ? @person.name : nil
13
+ #
14
+ # +try+ returns +nil+ when called on +nil+ regardless of whether it responds
15
+ # to the method:
16
+ #
17
+ # nil.try(:to_i) # => nil, rather than 0
18
+ #
19
+ # Arguments and blocks are forwarded to the method if invoked:
20
+ #
21
+ # @posts.try(:each_slice, 2) do |a, b|
22
+ # ...
23
+ # end
24
+ #
25
+ # The number of arguments in the signature must match. If the object responds
26
+ # to the method the call is attempted and +ArgumentError+ is still raised
27
+ # otherwise.
28
+ #
29
+ # If +try+ is called without arguments it yields the receiver to a given
30
+ # block unless it is +nil+:
31
+ #
32
+ # @person.try do |p|
33
+ # ...
34
+ # end
35
+ #
36
+ # Please also note that +try+ is defined on +Object+, therefore it won't work
37
+ # with instances of classes that do not have +Object+ among their ancestors,
38
+ # like direct subclasses of +BasicObject+. For example, using +try+ with
39
+ # +SimpleDelegator+ will delegate +try+ to the target instead of calling it on
40
+ # delegator itself.
2
41
  def try(*a, &b)
3
42
  if a.empty? && block_given?
4
43
  yield self
@@ -32,7 +71,7 @@ class Object
32
71
  end
33
72
 
34
73
  class NilClass
35
- # Calling +try+ on +nil+ returns +nil+
74
+ # Calling +try+ on +nil+ always returns +nil+.
36
75
  # It becomes specially helpful when navigating through associations that may return +nil+.
37
76
  #
38
77
  # nil.try(:name) # => nil
@@ -42,11 +81,14 @@ class NilClass
42
81
  #
43
82
  # With +try+
44
83
  # @person.try(:children).try(:first).try(:name)
45
- #
46
84
  def try(*args)
47
85
  nil
48
86
  end
49
87
 
88
+ def try!(*args)
89
+ nil
90
+ end
91
+
50
92
  # Calling +try?+ on +nil+ returns +false+
51
93
  # With +try?+
52
94
  # @person.try(:children).try(:first).try?(:has_dark_hairs?)
@@ -57,8 +99,4 @@ class NilClass
57
99
  raise ArgumentError, "For non-boolean methods use only try()"
58
100
  end
59
101
  end
60
-
61
- def try!(*args)
62
- nil
63
- end
64
102
  end
@@ -4,18 +4,10 @@ $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.0'
8
- spec.date = '2014-08-01'
7
+ spec.version = '1.0.1'
8
+ spec.date = '2014-08-02'
9
9
  spec.summary = "Provides RoR try() and extends it by new try?() method."
10
- spec.description = <<-DESC
11
- This gem has two versions of `try()`.
12
-
13
- Vanilla `try()`
14
- This is RoR vanilla `try()` and `try!()` methods. Nothing new.
15
- Boolean `try?()`
16
- It is returns `false` instead of `nil`, when trying to call something like
17
- `nil.try?(:some_boolean_method?)`
18
- DESC
10
+ spec.description = "Provides RoR try() and extends it by new try?() method."
19
11
  spec.authors = ["Oleg Orlov"]
20
12
  spec.email = 'orelcokolov@gmail.com'
21
13
  spec.homepage = 'http://rubygems.org/gems/ruby-try'
@@ -59,8 +59,6 @@ describe Object do
59
59
 
60
60
  describe "#try" do
61
61
  context "for existing method" do
62
- include_context "#try, method exists"
63
-
64
62
  it "should return value for general method" do
65
63
  subject.try(:get_zero).should eq(0)
66
64
  end
@@ -72,7 +70,6 @@ describe Object do
72
70
 
73
71
  context "for non-existing method" do
74
72
  include_context "#try, method not exists"
75
-
76
73
  end
77
74
 
78
75
  end
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.0
4
+ version: 1.0.1
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: 2014-08-01 00:00:00.000000000 Z
12
+ date: 2014-08-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -43,10 +43,7 @@ dependencies:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
45
  version: '10.0'
46
- description: ! " This gem has two versions of `try()`. \n\n Vanilla `try()`\n
47
- \ This is RoR vanilla `try()` and `try!()` methods. Nothing new.\n Boolean
48
- `try?()`\n It is returns `false` instead of `nil`, when trying to call something
49
- like \n `nil.try?(:some_boolean_method?)`\n"
46
+ description: Provides RoR try() and extends it by new try?() method.
50
47
  email: orelcokolov@gmail.com
51
48
  executables: []
52
49
  extensions: []