dissociated_introspection 0.1.5 → 0.1.6
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7052a41ca167f0a3c71da1b189c65b2f1bb4b4de
|
4
|
+
data.tar.gz: 64f62516603fcb83ac64e1c8c1030909d42e4bed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e1fc88468c673b89e02bf72fd254631afdce81eb6e3369113fb03e1b91576ee213d72ca81e9d6d483546470cf31ee82f112c5a730765e04f80ef51773680353
|
7
|
+
data.tar.gz: 09f111b9848cdbe7101c17e6e79a0a88129db5ad8d285f100349947b9b8347106cf843778e7edfd9d934d86b77638bf6642c0578e665d33d93ad88821e86c687
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
module DissociatedIntrospection
|
4
|
+
module Try
|
5
|
+
module Core
|
6
|
+
def try(*a, &b)
|
7
|
+
if a.empty? || respond_to?(a.first)
|
8
|
+
if a.empty? && block_given?
|
9
|
+
if b.arity.zero?
|
10
|
+
instance_eval(&b)
|
11
|
+
else
|
12
|
+
yield self
|
13
|
+
end
|
14
|
+
else
|
15
|
+
public_send(*a, &b)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
refine Object do
|
22
|
+
include Core
|
23
|
+
end
|
24
|
+
|
25
|
+
refine Delegator do
|
26
|
+
include Core
|
27
|
+
end
|
28
|
+
|
29
|
+
refine NilClass do
|
30
|
+
def try(*args)
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# class Object
|
37
|
+
##
|
38
|
+
# :method: try
|
39
|
+
#
|
40
|
+
# :call-seq:
|
41
|
+
# try(*a, &b)
|
42
|
+
#
|
43
|
+
# Invokes the public method whose name goes as first argument just like
|
44
|
+
# +public_send+ does, except that if the receiver does not respond to it the
|
45
|
+
# call returns +nil+ rather than raising an exception.
|
46
|
+
#
|
47
|
+
# This method is defined to be able to write
|
48
|
+
#
|
49
|
+
# @person.try(:name)
|
50
|
+
#
|
51
|
+
# instead of
|
52
|
+
#
|
53
|
+
# @person.name if @person
|
54
|
+
#
|
55
|
+
# +try+ calls can be chained:
|
56
|
+
#
|
57
|
+
# @person.try(:spouse).try(:name)
|
58
|
+
#
|
59
|
+
# instead of
|
60
|
+
#
|
61
|
+
# @person.spouse.name if @person && @person.spouse
|
62
|
+
#
|
63
|
+
# +try+ will also return +nil+ if the receiver does not respond to the method:
|
64
|
+
#
|
65
|
+
# @person.try(:non_existing_method) # => nil
|
66
|
+
#
|
67
|
+
# instead of
|
68
|
+
#
|
69
|
+
# @person.non_existing_method if @person.respond_to?(:non_existing_method) # => nil
|
70
|
+
#
|
71
|
+
# +try+ returns +nil+ when called on +nil+ regardless of whether it responds
|
72
|
+
# to the method:
|
73
|
+
#
|
74
|
+
# nil.try(:to_i) # => nil, rather than 0
|
75
|
+
#
|
76
|
+
# Arguments and blocks are forwarded to the method if invoked:
|
77
|
+
#
|
78
|
+
# @posts.try(:each_slice, 2) do |a, b|
|
79
|
+
# ...
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# The number of arguments in the signature must match. If the object responds
|
83
|
+
# to the method the call is attempted and +ArgumentError+ is still raised
|
84
|
+
# in case of argument mismatch.
|
85
|
+
#
|
86
|
+
# If +try+ is called without arguments it yields the receiver to a given
|
87
|
+
# block unless it is +nil+:
|
88
|
+
#
|
89
|
+
# @person.try do |p|
|
90
|
+
# ...
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# You can also call try with a block without accepting an argument, and the block
|
94
|
+
# will be instance_eval'ed instead:
|
95
|
+
#
|
96
|
+
# @person.try { upcase.truncate(50) }
|
97
|
+
#
|
98
|
+
# Please also note that +try+ is defined on +Object+. Therefore, it won't work
|
99
|
+
# with instances of classes that do not have +Object+ among their ancestors,
|
100
|
+
# like direct subclasses of +BasicObject+.
|
101
|
+
|
102
|
+
##
|
103
|
+
# :method: try!
|
104
|
+
#
|
105
|
+
# :call-seq:
|
106
|
+
# try!(*a, &b)
|
107
|
+
#
|
108
|
+
# Same as #try, but raises a NoMethodError exception if the receiver is
|
109
|
+
# not +nil+ and does not implement the tried method.
|
110
|
+
#
|
111
|
+
# "a".try!(:upcase) # => "A"
|
112
|
+
# nil.try!(:upcase) # => nil
|
113
|
+
# 123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Fixnum
|
114
|
+
# end
|
115
|
+
|
116
|
+
# class Delegator
|
117
|
+
##
|
118
|
+
# :method: try
|
119
|
+
#
|
120
|
+
# :call-seq:
|
121
|
+
# try(a*, &b)
|
122
|
+
#
|
123
|
+
# See Object#try
|
124
|
+
|
125
|
+
##
|
126
|
+
# :method: try!
|
127
|
+
#
|
128
|
+
# :call-seq:
|
129
|
+
# try!(a*, &b)
|
130
|
+
#
|
131
|
+
# See Object#try!
|
132
|
+
# end
|
133
|
+
|
134
|
+
# class NilClass
|
135
|
+
# Calling +try+ on +nil+ always returns +nil+.
|
136
|
+
# It becomes especially helpful when navigating through associations that may return +nil+.
|
137
|
+
#
|
138
|
+
# nil.try(:name) # => nil
|
139
|
+
#
|
140
|
+
# Without +try+
|
141
|
+
# @person && @person.children.any? && @person.children.first.name
|
142
|
+
#
|
143
|
+
# With +try+
|
144
|
+
# @person.try(:children).try(:first).try(:name)
|
145
|
+
# end
|
146
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'parser/current'
|
2
2
|
require 'unparser'
|
3
3
|
require 'dissociated_introspection/version'
|
4
|
-
require 'dissociated_introspection/
|
4
|
+
require 'dissociated_introspection/try'
|
5
5
|
require 'dissociated_introspection/eval_sandbox'
|
6
6
|
require 'dissociated_introspection/ruby_class'
|
7
7
|
require 'dissociated_introspection/inspection'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dissociated_introspection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dustin Zeisler
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parser
|
@@ -100,11 +100,11 @@ files:
|
|
100
100
|
- bin/setup
|
101
101
|
- dissociated_introspection.gemspec
|
102
102
|
- lib/dissociated_introspection.rb
|
103
|
-
- lib/dissociated_introspection/active_support.rb
|
104
103
|
- lib/dissociated_introspection/eval_sandbox.rb
|
105
104
|
- lib/dissociated_introspection/inspection.rb
|
106
105
|
- lib/dissociated_introspection/recording_parent.rb
|
107
106
|
- lib/dissociated_introspection/ruby_class.rb
|
107
|
+
- lib/dissociated_introspection/try.rb
|
108
108
|
- lib/dissociated_introspection/version.rb
|
109
109
|
homepage: https://github.com/zeisler/dissociated_introspection
|
110
110
|
licenses:
|
@@ -1,145 +0,0 @@
|
|
1
|
-
require 'delegate'
|
2
|
-
module ActiveSupport
|
3
|
-
module Try
|
4
|
-
module Core
|
5
|
-
def try(*a, &b)
|
6
|
-
if a.empty? || respond_to?(a.first)
|
7
|
-
if a.empty? && block_given?
|
8
|
-
if b.arity.zero?
|
9
|
-
instance_eval(&b)
|
10
|
-
else
|
11
|
-
yield self
|
12
|
-
end
|
13
|
-
else
|
14
|
-
public_send(*a, &b)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
refine Object do
|
21
|
-
include Core
|
22
|
-
end
|
23
|
-
|
24
|
-
refine Delegator do
|
25
|
-
include Core
|
26
|
-
end
|
27
|
-
|
28
|
-
refine NilClass do
|
29
|
-
def try(*args)
|
30
|
-
nil
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
# class Object
|
36
|
-
##
|
37
|
-
# :method: try
|
38
|
-
#
|
39
|
-
# :call-seq:
|
40
|
-
# try(*a, &b)
|
41
|
-
#
|
42
|
-
# Invokes the public method whose name goes as first argument just like
|
43
|
-
# +public_send+ does, except that if the receiver does not respond to it the
|
44
|
-
# call returns +nil+ rather than raising an exception.
|
45
|
-
#
|
46
|
-
# This method is defined to be able to write
|
47
|
-
#
|
48
|
-
# @person.try(:name)
|
49
|
-
#
|
50
|
-
# instead of
|
51
|
-
#
|
52
|
-
# @person.name if @person
|
53
|
-
#
|
54
|
-
# +try+ calls can be chained:
|
55
|
-
#
|
56
|
-
# @person.try(:spouse).try(:name)
|
57
|
-
#
|
58
|
-
# instead of
|
59
|
-
#
|
60
|
-
# @person.spouse.name if @person && @person.spouse
|
61
|
-
#
|
62
|
-
# +try+ will also return +nil+ if the receiver does not respond to the method:
|
63
|
-
#
|
64
|
-
# @person.try(:non_existing_method) # => nil
|
65
|
-
#
|
66
|
-
# instead of
|
67
|
-
#
|
68
|
-
# @person.non_existing_method if @person.respond_to?(:non_existing_method) # => nil
|
69
|
-
#
|
70
|
-
# +try+ returns +nil+ when called on +nil+ regardless of whether it responds
|
71
|
-
# to the method:
|
72
|
-
#
|
73
|
-
# nil.try(:to_i) # => nil, rather than 0
|
74
|
-
#
|
75
|
-
# Arguments and blocks are forwarded to the method if invoked:
|
76
|
-
#
|
77
|
-
# @posts.try(:each_slice, 2) do |a, b|
|
78
|
-
# ...
|
79
|
-
# end
|
80
|
-
#
|
81
|
-
# The number of arguments in the signature must match. If the object responds
|
82
|
-
# to the method the call is attempted and +ArgumentError+ is still raised
|
83
|
-
# in case of argument mismatch.
|
84
|
-
#
|
85
|
-
# If +try+ is called without arguments it yields the receiver to a given
|
86
|
-
# block unless it is +nil+:
|
87
|
-
#
|
88
|
-
# @person.try do |p|
|
89
|
-
# ...
|
90
|
-
# end
|
91
|
-
#
|
92
|
-
# You can also call try with a block without accepting an argument, and the block
|
93
|
-
# will be instance_eval'ed instead:
|
94
|
-
#
|
95
|
-
# @person.try { upcase.truncate(50) }
|
96
|
-
#
|
97
|
-
# Please also note that +try+ is defined on +Object+. Therefore, it won't work
|
98
|
-
# with instances of classes that do not have +Object+ among their ancestors,
|
99
|
-
# like direct subclasses of +BasicObject+.
|
100
|
-
|
101
|
-
##
|
102
|
-
# :method: try!
|
103
|
-
#
|
104
|
-
# :call-seq:
|
105
|
-
# try!(*a, &b)
|
106
|
-
#
|
107
|
-
# Same as #try, but raises a NoMethodError exception if the receiver is
|
108
|
-
# not +nil+ and does not implement the tried method.
|
109
|
-
#
|
110
|
-
# "a".try!(:upcase) # => "A"
|
111
|
-
# nil.try!(:upcase) # => nil
|
112
|
-
# 123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Fixnum
|
113
|
-
# end
|
114
|
-
|
115
|
-
class Delegator
|
116
|
-
##
|
117
|
-
# :method: try
|
118
|
-
#
|
119
|
-
# :call-seq:
|
120
|
-
# try(a*, &b)
|
121
|
-
#
|
122
|
-
# See Object#try
|
123
|
-
|
124
|
-
##
|
125
|
-
# :method: try!
|
126
|
-
#
|
127
|
-
# :call-seq:
|
128
|
-
# try!(a*, &b)
|
129
|
-
#
|
130
|
-
# See Object#try!
|
131
|
-
end
|
132
|
-
|
133
|
-
class NilClass
|
134
|
-
# Calling +try+ on +nil+ always returns +nil+.
|
135
|
-
# It becomes especially helpful when navigating through associations that may return +nil+.
|
136
|
-
#
|
137
|
-
# nil.try(:name) # => nil
|
138
|
-
#
|
139
|
-
# Without +try+
|
140
|
-
# @person && @person.children.any? && @person.children.first.name
|
141
|
-
#
|
142
|
-
# With +try+
|
143
|
-
# @person.try(:children).try(:first).try(:name)
|
144
|
-
end
|
145
|
-
end
|