sigterm_extensions 0.0.4
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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +17 -0
- data/Gemfile +6 -0
- data/LICENSE.md +0 -0
- data/README.md +0 -0
- data/bin/ctxirb +156 -0
- data/lib/git.rb +166 -0
- data/lib/git/LICENSE +21 -0
- data/lib/git/author.rb +14 -0
- data/lib/git/base.rb +551 -0
- data/lib/git/base/factory.rb +75 -0
- data/lib/git/branch.rb +126 -0
- data/lib/git/branches.rb +71 -0
- data/lib/git/config.rb +22 -0
- data/lib/git/diff.rb +159 -0
- data/lib/git/index.rb +5 -0
- data/lib/git/lib.rb +1041 -0
- data/lib/git/log.rb +128 -0
- data/lib/git/object.rb +312 -0
- data/lib/git/path.rb +31 -0
- data/lib/git/remote.rb +36 -0
- data/lib/git/repository.rb +6 -0
- data/lib/git/stash.rb +27 -0
- data/lib/git/stashes.rb +55 -0
- data/lib/git/status.rb +199 -0
- data/lib/git/version.rb +5 -0
- data/lib/git/working_directory.rb +4 -0
- data/lib/sigterm_extensions.rb +75 -0
- data/lib/sigterm_extensions/all.rb +12 -0
- data/lib/sigterm_extensions/backtrace_cleaner.rb +129 -0
- data/lib/sigterm_extensions/callbacks.rb +847 -0
- data/lib/sigterm_extensions/concern.rb +169 -0
- data/lib/sigterm_extensions/configurable.rb +38 -0
- data/lib/sigterm_extensions/core_ext.rb +4 -0
- data/lib/sigterm_extensions/core_ext/array.rb +3 -0
- data/lib/sigterm_extensions/core_ext/array/extract.rb +19 -0
- data/lib/sigterm_extensions/core_ext/array/extract_options.rb +29 -0
- data/lib/sigterm_extensions/core_ext/class.rb +3 -0
- data/lib/sigterm_extensions/core_ext/class/attribute.rb +139 -0
- data/lib/sigterm_extensions/core_ext/class/attribute_accessors.rb +4 -0
- data/lib/sigterm_extensions/core_ext/class/subclasses.rb +52 -0
- data/lib/sigterm_extensions/core_ext/custom.rb +12 -0
- data/lib/sigterm_extensions/core_ext/digest.rb +3 -0
- data/lib/sigterm_extensions/core_ext/digest/uuid.rb +51 -0
- data/lib/sigterm_extensions/core_ext/enumerable.rb +232 -0
- data/lib/sigterm_extensions/core_ext/file.rb +3 -0
- data/lib/sigterm_extensions/core_ext/file/atomic.rb +68 -0
- data/lib/sigterm_extensions/core_ext/hash.rb +3 -0
- data/lib/sigterm_extensions/core_ext/hash/deep_merge.rb +41 -0
- data/lib/sigterm_extensions/core_ext/hash/deep_transform_values.rb +44 -0
- data/lib/sigterm_extensions/core_ext/hash/except.rb +22 -0
- data/lib/sigterm_extensions/core_ext/hash/keys.rb +141 -0
- data/lib/sigterm_extensions/core_ext/hash/reverse_merge.rb +23 -0
- data/lib/sigterm_extensions/core_ext/hash/slice.rb +24 -0
- data/lib/sigterm_extensions/core_ext/kernel.rb +3 -0
- data/lib/sigterm_extensions/core_ext/kernel/concern.rb +12 -0
- data/lib/sigterm_extensions/core_ext/kernel/reporting.rb +43 -0
- data/lib/sigterm_extensions/core_ext/kernel/singleton_class.rb +6 -0
- data/lib/sigterm_extensions/core_ext/load_error.rb +7 -0
- data/lib/sigterm_extensions/core_ext/module.rb +3 -0
- data/lib/sigterm_extensions/core_ext/module/aliasing.rb +29 -0
- data/lib/sigterm_extensions/core_ext/module/anonymous.rb +28 -0
- data/lib/sigterm_extensions/core_ext/module/attr_internal.rb +36 -0
- data/lib/sigterm_extensions/core_ext/module/attribute_accessors.rb +208 -0
- data/lib/sigterm_extensions/core_ext/module/attribute_accessors_per_thread.rb +146 -0
- data/lib/sigterm_extensions/core_ext/module/concerning.rb +132 -0
- data/lib/sigterm_extensions/core_ext/module/delegation.rb +319 -0
- data/lib/sigterm_extensions/core_ext/module/redefine_method.rb +38 -0
- data/lib/sigterm_extensions/core_ext/module/remove_method.rb +15 -0
- data/lib/sigterm_extensions/core_ext/name_error.rb +36 -0
- data/lib/sigterm_extensions/core_ext/object.rb +3 -0
- data/lib/sigterm_extensions/core_ext/object/blank.rb +153 -0
- data/lib/sigterm_extensions/core_ext/object/colors.rb +39 -0
- data/lib/sigterm_extensions/core_ext/object/duplicable.rb +47 -0
- data/lib/sigterm_extensions/core_ext/object/inclusion.rb +27 -0
- data/lib/sigterm_extensions/core_ext/object/instance_variables.rb +28 -0
- data/lib/sigterm_extensions/core_ext/object/methods.rb +61 -0
- data/lib/sigterm_extensions/core_ext/object/with_options.rb +80 -0
- data/lib/sigterm_extensions/core_ext/range.rb +3 -0
- data/lib/sigterm_extensions/core_ext/range/compare_range.rb +74 -0
- data/lib/sigterm_extensions/core_ext/range/conversions.rb +39 -0
- data/lib/sigterm_extensions/core_ext/range/overlaps.rb +8 -0
- data/lib/sigterm_extensions/core_ext/securerandom.rb +43 -0
- data/lib/sigterm_extensions/core_ext/string.rb +3 -0
- data/lib/sigterm_extensions/core_ext/string/access.rb +93 -0
- data/lib/sigterm_extensions/core_ext/string/filters.rb +143 -0
- data/lib/sigterm_extensions/core_ext/string/starts_ends_with.rb +4 -0
- data/lib/sigterm_extensions/core_ext/string/strip.rb +25 -0
- data/lib/sigterm_extensions/core_ext/tryable.rb +132 -0
- data/lib/sigterm_extensions/descendants_tracker.rb +108 -0
- data/lib/sigterm_extensions/gem_methods.rb +47 -0
- data/lib/sigterm_extensions/hash_binding.rb +16 -0
- data/lib/sigterm_extensions/inflector.rb +339 -0
- data/lib/sigterm_extensions/inflector/acronyms.rb +42 -0
- data/lib/sigterm_extensions/inflector/inflections.rb +249 -0
- data/lib/sigterm_extensions/inflector/inflections/defaults.rb +117 -0
- data/lib/sigterm_extensions/inflector/rules.rb +37 -0
- data/lib/sigterm_extensions/inflector/version.rb +8 -0
- data/lib/sigterm_extensions/interactive_editor.rb +120 -0
- data/lib/sigterm_extensions/lazy.rb +34 -0
- data/lib/sigterm_extensions/lazy_load_hooks.rb +79 -0
- data/lib/sigterm_extensions/option_merger.rb +32 -0
- data/lib/sigterm_extensions/ordered_hash.rb +48 -0
- data/lib/sigterm_extensions/ordered_options.rb +83 -0
- data/lib/sigterm_extensions/paths.rb +235 -0
- data/lib/sigterm_extensions/per_thread_registry.rb +58 -0
- data/lib/sigterm_extensions/proxy_object.rb +14 -0
- data/lib/sigterm_extensions/staging/boot.rb +31 -0
- data/lib/sigterm_extensions/staging/boot/bundler_patch.rb +24 -0
- data/lib/sigterm_extensions/staging/boot/command.rb +26 -0
- data/lib/sigterm_extensions/staging/boot/gemfile_next_auto_sync.rb +79 -0
- data/lib/sigterm_extensions/version.rb +4 -0
- data/lib/sigterm_extensions/wrappable.rb +16 -0
- data/sigterm_extensions.gemspec +42 -0
- data/templates/dotpryrc.rb.erb +124 -0
- metadata +315 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
class Module
|
|
2
|
+
# Marks the named method as intended to be redefined, if it exists.
|
|
3
|
+
# Suppresses the Ruby method redefinition warning. Prefer
|
|
4
|
+
# #redefine_method where possible.
|
|
5
|
+
def silence_redefinition_of_method(method)
|
|
6
|
+
if method_defined?(method) || private_method_defined?(method)
|
|
7
|
+
# This suppresses the "method redefined" warning; the self-alias
|
|
8
|
+
# looks odd, but means we don't need to generate a unique name
|
|
9
|
+
alias_method method, method
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Replaces the existing method definition, if there is one, with the passed
|
|
14
|
+
# block as its body.
|
|
15
|
+
def redefine_method(method, &block)
|
|
16
|
+
visibility = method_visibility(method)
|
|
17
|
+
silence_redefinition_of_method(method)
|
|
18
|
+
define_method(method, &block)
|
|
19
|
+
send(visibility, method)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Replaces the existing singleton method definition, if there is one, with
|
|
23
|
+
# the passed block as its body.
|
|
24
|
+
def redefine_singleton_method(method, &block)
|
|
25
|
+
singleton_class.redefine_method(method, &block)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def method_visibility(method) # :nodoc:
|
|
29
|
+
case
|
|
30
|
+
when private_method_defined?(method)
|
|
31
|
+
:private
|
|
32
|
+
when protected_method_defined?(method)
|
|
33
|
+
:protected
|
|
34
|
+
else
|
|
35
|
+
:public
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require "sigterm_extensions/core_ext/module/redefine_method"
|
|
2
|
+
|
|
3
|
+
class Module
|
|
4
|
+
# Removes the named method, if it exists.
|
|
5
|
+
def remove_possible_method(method)
|
|
6
|
+
if method_defined?(method) || private_method_defined?(method)
|
|
7
|
+
undef_method(method)
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Removes the named singleton method, if it exists.
|
|
12
|
+
def remove_possible_singleton_method(method)
|
|
13
|
+
singleton_class.remove_possible_method(method)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
class NameError
|
|
2
|
+
# Extract the name of the missing constant from the exception message.
|
|
3
|
+
#
|
|
4
|
+
# begin
|
|
5
|
+
# HelloWorld
|
|
6
|
+
# rescue NameError => e
|
|
7
|
+
# e.missing_name
|
|
8
|
+
# end
|
|
9
|
+
# # => "HelloWorld"
|
|
10
|
+
def missing_name
|
|
11
|
+
# Since ruby v2.3.0 `did_you_mean` gem is loaded by default.
|
|
12
|
+
# It extends NameError#message with spell corrections which are SLOW.
|
|
13
|
+
# We should use original_message message instead.
|
|
14
|
+
message = respond_to?(:original_message) ? original_message : self.message
|
|
15
|
+
|
|
16
|
+
unless /undefined local variable or method/.match?(message)
|
|
17
|
+
$1 if /((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/ =~ message
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Was this exception raised because the given name was missing?
|
|
22
|
+
#
|
|
23
|
+
# begin
|
|
24
|
+
# HelloWorld
|
|
25
|
+
# rescue NameError => e
|
|
26
|
+
# e.missing_name?("HelloWorld")
|
|
27
|
+
# end
|
|
28
|
+
# # => true
|
|
29
|
+
def missing_name?(name)
|
|
30
|
+
if name.is_a? Symbol
|
|
31
|
+
self.name == name
|
|
32
|
+
else
|
|
33
|
+
missing_name == name.to_s
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
require "concurrent/map"
|
|
2
|
+
|
|
3
|
+
class Object
|
|
4
|
+
# An object is blank if it's false, empty, or a whitespace string.
|
|
5
|
+
# For example, +nil+, '', ' ', [], {}, and +false+ are all blank.
|
|
6
|
+
#
|
|
7
|
+
# This simplifies
|
|
8
|
+
#
|
|
9
|
+
# !address || address.empty?
|
|
10
|
+
#
|
|
11
|
+
# to
|
|
12
|
+
#
|
|
13
|
+
# address.blank?
|
|
14
|
+
#
|
|
15
|
+
# @return [true, false]
|
|
16
|
+
def blank?
|
|
17
|
+
respond_to?(:empty?) ? !!empty? : !self
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# An object is present if it's not blank.
|
|
21
|
+
#
|
|
22
|
+
# @return [true, false]
|
|
23
|
+
def present?
|
|
24
|
+
!blank?
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Returns the receiver if it's present otherwise returns +nil+.
|
|
28
|
+
# <tt>object.presence</tt> is equivalent to
|
|
29
|
+
#
|
|
30
|
+
# object.present? ? object : nil
|
|
31
|
+
#
|
|
32
|
+
# For example, something like
|
|
33
|
+
#
|
|
34
|
+
# state = params[:state] if params[:state].present?
|
|
35
|
+
# country = params[:country] if params[:country].present?
|
|
36
|
+
# region = state || country || 'US'
|
|
37
|
+
#
|
|
38
|
+
# becomes
|
|
39
|
+
#
|
|
40
|
+
# region = params[:state].presence || params[:country].presence || 'US'
|
|
41
|
+
#
|
|
42
|
+
# @return [Object]
|
|
43
|
+
def presence
|
|
44
|
+
self if present?
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
class NilClass
|
|
49
|
+
# +nil+ is blank:
|
|
50
|
+
#
|
|
51
|
+
# nil.blank? # => true
|
|
52
|
+
#
|
|
53
|
+
# @return [true]
|
|
54
|
+
def blank?
|
|
55
|
+
true
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
class FalseClass
|
|
60
|
+
# +false+ is blank:
|
|
61
|
+
#
|
|
62
|
+
# false.blank? # => true
|
|
63
|
+
#
|
|
64
|
+
# @return [true]
|
|
65
|
+
def blank?
|
|
66
|
+
true
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
class TrueClass
|
|
71
|
+
# +true+ is not blank:
|
|
72
|
+
#
|
|
73
|
+
# true.blank? # => false
|
|
74
|
+
#
|
|
75
|
+
# @return [false]
|
|
76
|
+
def blank?
|
|
77
|
+
false
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
class Array
|
|
82
|
+
# An array is blank if it's empty:
|
|
83
|
+
#
|
|
84
|
+
# [].blank? # => true
|
|
85
|
+
# [1,2,3].blank? # => false
|
|
86
|
+
#
|
|
87
|
+
# @return [true, false]
|
|
88
|
+
alias_method :blank?, :empty?
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
class Hash
|
|
92
|
+
# A hash is blank if it's empty:
|
|
93
|
+
#
|
|
94
|
+
# {}.blank? # => true
|
|
95
|
+
# { key: 'value' }.blank? # => false
|
|
96
|
+
#
|
|
97
|
+
# @return [true, false]
|
|
98
|
+
alias_method :blank?, :empty?
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
class String
|
|
102
|
+
BLANK_RE = /\A[[:space:]]*\z/
|
|
103
|
+
ENCODED_BLANKS = Concurrent::Map.new do |h, enc|
|
|
104
|
+
h[enc] = Regexp.new(BLANK_RE.source.encode(enc), BLANK_RE.options | Regexp::FIXEDENCODING)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# A string is blank if it's empty or contains whitespaces only:
|
|
108
|
+
#
|
|
109
|
+
# ''.blank? # => true
|
|
110
|
+
# ' '.blank? # => true
|
|
111
|
+
# "\t\n\r".blank? # => true
|
|
112
|
+
# ' blah '.blank? # => false
|
|
113
|
+
#
|
|
114
|
+
# Unicode whitespace is supported:
|
|
115
|
+
#
|
|
116
|
+
# "\u00a0".blank? # => true
|
|
117
|
+
#
|
|
118
|
+
# @return [true, false]
|
|
119
|
+
def blank?
|
|
120
|
+
# The regexp that matches blank strings is expensive. For the case of empty
|
|
121
|
+
# strings we can speed up this method (~3.5x) with an empty? call. The
|
|
122
|
+
# penalty for the rest of strings is marginal.
|
|
123
|
+
empty? ||
|
|
124
|
+
begin
|
|
125
|
+
BLANK_RE.match?(self)
|
|
126
|
+
rescue Encoding::CompatibilityError
|
|
127
|
+
ENCODED_BLANKS[self.encoding].match?(self)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
class Numeric #:nodoc:
|
|
133
|
+
# No number is blank:
|
|
134
|
+
#
|
|
135
|
+
# 1.blank? # => false
|
|
136
|
+
# 0.blank? # => false
|
|
137
|
+
#
|
|
138
|
+
# @return [false]
|
|
139
|
+
def blank?
|
|
140
|
+
false
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
class Time #:nodoc:
|
|
145
|
+
# No Time is blank:
|
|
146
|
+
#
|
|
147
|
+
# Time.now.blank? # => false
|
|
148
|
+
#
|
|
149
|
+
# @return [false]
|
|
150
|
+
def blank?
|
|
151
|
+
false
|
|
152
|
+
end
|
|
153
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Method to pretty-print object methods
|
|
2
|
+
# Coded by sebastian delmont
|
|
3
|
+
# http://snippets.dzone.com/posts/show/2916
|
|
4
|
+
#class Object
|
|
5
|
+
# ANSI_BOLD = "\033[1m"
|
|
6
|
+
# ANSI_RESET = "\033[0m"
|
|
7
|
+
# ANSI_LGRAY = "\033[0;37m"
|
|
8
|
+
# ANSI_GRAY = "\033[1;30m"
|
|
9
|
+
# # Print object's methods
|
|
10
|
+
# def pm(*options)
|
|
11
|
+
# methods = self.methods
|
|
12
|
+
# methods -= Object.methods unless options.include? :more
|
|
13
|
+
# filter = options.select {|opt| opt.kind_of? Regexp}.first
|
|
14
|
+
# methods = methods.select {|name| name =~ filter} if filter
|
|
15
|
+
#
|
|
16
|
+
# data = methods.sort.collect do |name|
|
|
17
|
+
# method = self.method(name)
|
|
18
|
+
# if method.arity == 0
|
|
19
|
+
# args = "()"
|
|
20
|
+
# elsif method.arity > 0
|
|
21
|
+
# n = method.arity
|
|
22
|
+
# args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")})"
|
|
23
|
+
# elsif method.arity < 0
|
|
24
|
+
# n = -method.arity
|
|
25
|
+
# args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")}, ...)"
|
|
26
|
+
# end
|
|
27
|
+
# klass = $1 if method.inspect =~ /Method: (.*?)#/
|
|
28
|
+
# [name, args, klass]
|
|
29
|
+
# end
|
|
30
|
+
# max_name = data.collect {|item| item[0].size}.max
|
|
31
|
+
# max_args = data.collect {|item| item[1].size}.max
|
|
32
|
+
# data.each do |item|
|
|
33
|
+
# print " #{ANSI_BOLD}#{item[0].to_s.rjust(max_name)}#{ANSI_RESET}"
|
|
34
|
+
# print "#{ANSI_GRAY}#{item[1].ljust(max_args)}#{ANSI_RESET}"
|
|
35
|
+
# print " #{ANSI_LGRAY}#{item[2]}#{ANSI_RESET}\n"
|
|
36
|
+
# end
|
|
37
|
+
# data.size
|
|
38
|
+
# end
|
|
39
|
+
#end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#--
|
|
2
|
+
# Most objects are cloneable, but not all. For example you can't dup methods:
|
|
3
|
+
#
|
|
4
|
+
# method(:puts).dup # => TypeError: allocator undefined for Method
|
|
5
|
+
#
|
|
6
|
+
# Classes may signal their instances are not duplicable removing +dup+/+clone+
|
|
7
|
+
# or raising exceptions from them. So, to dup an arbitrary object you normally
|
|
8
|
+
# use an optimistic approach and are ready to catch an exception, say:
|
|
9
|
+
#
|
|
10
|
+
# arbitrary_object.dup rescue object
|
|
11
|
+
#
|
|
12
|
+
# Rails dups objects in a few critical spots where they are not that arbitrary.
|
|
13
|
+
# That rescue is very expensive (like 40 times slower than a predicate), and it
|
|
14
|
+
# is often triggered.
|
|
15
|
+
#
|
|
16
|
+
# That's why we hardcode the following cases and check duplicable? instead of
|
|
17
|
+
# using that rescue idiom.
|
|
18
|
+
#++
|
|
19
|
+
class Object
|
|
20
|
+
# Can you safely dup this object?
|
|
21
|
+
#
|
|
22
|
+
# False for method objects;
|
|
23
|
+
# true otherwise.
|
|
24
|
+
def duplicable?
|
|
25
|
+
true
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
class Method
|
|
30
|
+
# Methods are not duplicable:
|
|
31
|
+
#
|
|
32
|
+
# method(:puts).duplicable? # => false
|
|
33
|
+
# method(:puts).dup # => TypeError: allocator undefined for Method
|
|
34
|
+
def duplicable?
|
|
35
|
+
false
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
class UnboundMethod
|
|
40
|
+
# Unbound methods are not duplicable:
|
|
41
|
+
#
|
|
42
|
+
# method(:puts).unbind.duplicable? # => false
|
|
43
|
+
# method(:puts).unbind.dup # => TypeError: allocator undefined for UnboundMethod
|
|
44
|
+
def duplicable?
|
|
45
|
+
false
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
class Object
|
|
2
|
+
# Returns true if this object is included in the argument. Argument must be
|
|
3
|
+
# any object which responds to +#include?+. Usage:
|
|
4
|
+
#
|
|
5
|
+
# characters = ["Konata", "Kagami", "Tsukasa"]
|
|
6
|
+
# "Konata".in?(characters) # => true
|
|
7
|
+
#
|
|
8
|
+
# This will throw an +ArgumentError+ if the argument doesn't respond
|
|
9
|
+
# to +#include?+.
|
|
10
|
+
def in?(another_object)
|
|
11
|
+
another_object.include?(self)
|
|
12
|
+
rescue NoMethodError
|
|
13
|
+
raise ArgumentError.new("The parameter passed to #in? must respond to #include?")
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Returns the receiver if it's included in the argument otherwise returns +nil+.
|
|
17
|
+
# Argument must be any object which responds to +#include?+. Usage:
|
|
18
|
+
#
|
|
19
|
+
# params[:bucket_type].presence_in %w( project calendar )
|
|
20
|
+
#
|
|
21
|
+
# This will throw an +ArgumentError+ if the argument doesn't respond to +#include?+.
|
|
22
|
+
#
|
|
23
|
+
# @return [Object]
|
|
24
|
+
def presence_in(another_object)
|
|
25
|
+
in?(another_object) ? self : nil
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
class Object
|
|
2
|
+
# Returns a hash with string keys that maps instance variable names without "@" to their
|
|
3
|
+
# corresponding values.
|
|
4
|
+
#
|
|
5
|
+
# class C
|
|
6
|
+
# def initialize(x, y)
|
|
7
|
+
# @x, @y = x, y
|
|
8
|
+
# end
|
|
9
|
+
# end
|
|
10
|
+
#
|
|
11
|
+
# C.new(0, 1).instance_values # => {"x" => 0, "y" => 1}
|
|
12
|
+
def instance_values
|
|
13
|
+
Hash[instance_variables.map { |name| [name[1..-1], instance_variable_get(name)] }]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Returns an array of instance variable names as strings including "@".
|
|
17
|
+
#
|
|
18
|
+
# class C
|
|
19
|
+
# def initialize(x, y)
|
|
20
|
+
# @x, @y = x, y
|
|
21
|
+
# end
|
|
22
|
+
# end
|
|
23
|
+
#
|
|
24
|
+
# C.new(0, 1).instance_variable_names # => ["@y", "@x"]
|
|
25
|
+
def instance_variable_names
|
|
26
|
+
instance_variables.map(&:to_s)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
class Object
|
|
2
|
+
# Returns true if this object is included in the argument. Argument must be
|
|
3
|
+
# any object which responds to +#include?+. Usage:
|
|
4
|
+
#
|
|
5
|
+
# characters = ["Konata", "Kagami", "Tsukasa"]
|
|
6
|
+
# "Konata".in?(characters) # => true
|
|
7
|
+
#
|
|
8
|
+
# This will throw an +ArgumentError+ if the argument doesn't respond
|
|
9
|
+
# to +#include?+.
|
|
10
|
+
def in?(another_object)
|
|
11
|
+
another_object.include?(self)
|
|
12
|
+
rescue NoMethodError
|
|
13
|
+
raise ArgumentError.new("The parameter passed to #in? must respond to #include?")
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Returns the receiver if it's included in the argument otherwise returns +nil+.
|
|
17
|
+
# Argument must be any object which responds to +#include?+. Usage:
|
|
18
|
+
#
|
|
19
|
+
# params[:bucket_type].presence_in %w( project calendar )
|
|
20
|
+
#
|
|
21
|
+
# This will throw an +ArgumentError+ if the argument doesn't respond to +#include?+.
|
|
22
|
+
#
|
|
23
|
+
# @return [Object]
|
|
24
|
+
def presence_in(another_object)
|
|
25
|
+
in?(another_object) ? self : nil
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# list methods which aren't in superclass
|
|
29
|
+
def local_methods(obj = self)
|
|
30
|
+
(obj.methods - obj.class.superclass.instance_methods).sort
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def my_methods
|
|
34
|
+
base_object = case self
|
|
35
|
+
when Class then Class.new
|
|
36
|
+
when Module then Module.new
|
|
37
|
+
else Object.new
|
|
38
|
+
end
|
|
39
|
+
(methods - base_object.methods).sort
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# print documentation
|
|
43
|
+
#
|
|
44
|
+
# ri 'Array#pop'
|
|
45
|
+
# Array.ri
|
|
46
|
+
# Array.ri :pop
|
|
47
|
+
# arr.ri :pop
|
|
48
|
+
def ri(method = nil)
|
|
49
|
+
unless method && method =~ /^[A-Z]/ # if class isn't specified
|
|
50
|
+
klass = self.kind_of?(Class) ? name : self.class.name
|
|
51
|
+
method = [klass, method].compact.join('#')
|
|
52
|
+
end
|
|
53
|
+
puts `ri '#{method}'`
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Return only the methods not present on basic objects
|
|
57
|
+
def interesting_methods
|
|
58
|
+
(self.methods - Object.new.methods).sort
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
end
|