nrser 0.2.0.pre.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nrser/ext/enumerable.rb +12 -3
- data/lib/nrser/ext/module.rb +62 -0
- data/lib/nrser/ext.rb +1 -0
- data/lib/nrser/functions/binding.rb +33 -0
- data/lib/nrser/functions/enumerable/associate.rb +103 -0
- data/lib/nrser/functions/enumerable/map_keys.rb +0 -0
- data/lib/nrser/functions/enumerable/map_values.rb +94 -0
- data/lib/nrser/functions/enumerable.rb +2 -87
- data/lib/nrser/functions/module/methods.rb +206 -0
- data/lib/nrser/functions/module/source_locations.rb +213 -0
- data/lib/nrser/functions/module.rb +2 -0
- data/lib/nrser/functions.rb +1 -0
- data/lib/nrser/logging/appender/sync.rb +148 -0
- data/lib/nrser/logging/appender.rb +3 -0
- data/lib/nrser/logging/formatters/color.rb +165 -0
- data/lib/nrser/logging/formatters.rb +1 -0
- data/lib/nrser/logging.rb +353 -0
- data/lib/nrser/refinements/module.rb +5 -0
- data/lib/nrser/refinements.rb +1 -0
- data/lib/nrser/rspex/described.rb +99 -0
- data/lib/nrser/rspex/example_group/describe_called_with.rb +2 -2
- data/lib/nrser/rspex/example_group/describe_class.rb +31 -0
- data/lib/nrser/rspex/example_group/describe_instance.rb +1 -1
- data/lib/nrser/rspex/example_group/describe_method.rb +40 -0
- data/lib/nrser/rspex/example_group.rb +2 -34
- data/lib/nrser/rspex/format.rb +19 -6
- data/lib/nrser/rspex.rb +1 -1
- data/lib/nrser/types/numbers.rb +16 -16
- data/lib/nrser/version.rb +1 -1
- data/lib/nrser.rb +2 -5
- data/spec/design/mapping_spec.rb +42 -0
- data/spec/lib/nrser/mean_streak/identity_instance_spec.rb +7 -5
- data/spec/spec_helper.rb +23 -105
- data/spec/support/shared/types.rb +92 -0
- data/spec/support/shared.rb +1 -0
- metadata +27 -24
- data/lib/nrser/labs/unicode_math.rb +0 -48
- data/lib/nrser/labs/where.rb +0 -50
- data/lib/nrser/logger.rb +0 -457
- data/spec/lib/nrser/logger/dest_spec.rb +0 -15
- data/spec/lib/nrser/logger/die_spec.rb +0 -41
- data/spec/lib/nrser/logger/install_spec.rb +0 -98
- data/spec/lib/nrser/logger/level_int_spec.rb +0 -22
- data/spec/lib/nrser/logger/level_name_spec.rb +0 -23
- data/spec/lib/nrser/logger/level_sym_spec.rb +0 -22
- data/spec/lib/nrser/logger/send_log_spec.rb +0 -63
- data/spec/lib/nrser/logger/use_spec.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5311c0d4f62623e09b45060dfdfb067649b17335
|
4
|
+
data.tar.gz: e0081ada45abd36fab607fbf9971700a94456df0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bde6122d23a49148589971a45ffbe9a55d0e5f703d72032eea32195d8d4da40f0f188b1e901532ec8c15b457fadacfee011f83eaa9a4b5bbd3198977ceee891b
|
7
|
+
data.tar.gz: ca3036819080f5fd335dbcb93dbaae77be968873f44945e9138a92f9779b88fdbe510073b664130c2a274937a26a8f361347919081cd4478f5cafa3c99f40f24
|
data/lib/nrser/ext/enumerable.rb
CHANGED
@@ -23,9 +23,18 @@ module NRSER::Ext::Enumerable
|
|
23
23
|
end
|
24
24
|
|
25
25
|
|
26
|
-
# See {NRSER.
|
27
|
-
def
|
28
|
-
NRSER.
|
26
|
+
# See {NRSER.assoc_by}
|
27
|
+
def assoc_by &block
|
28
|
+
NRSER.assoc_by self, &block
|
29
|
+
end
|
30
|
+
|
31
|
+
# Old name
|
32
|
+
alias_method :to_h_by, :assoc_by
|
33
|
+
|
34
|
+
|
35
|
+
# See {NRSER.assoc_to}
|
36
|
+
def assoc_to &block
|
37
|
+
NRSER.assoc_to self, &block
|
29
38
|
end
|
30
39
|
|
31
40
|
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# Extension methods for {Module}
|
2
|
+
#
|
3
|
+
# @note
|
4
|
+
# These **must not** conflict with function names, which are of course
|
5
|
+
# also module methods on the {NRSER} module, because they will override
|
6
|
+
# them and prevent access to the functions.
|
7
|
+
#
|
8
|
+
# Hence the target functions have been prefixed with `f_`.
|
9
|
+
#
|
10
|
+
module NRSER::Ext::Module
|
11
|
+
|
12
|
+
# @!group Module Methods
|
13
|
+
# ==========================================================================
|
14
|
+
|
15
|
+
# Calls {NRSER.class_method_objects_for} with `self` as first arg.
|
16
|
+
def class_method_objects *args
|
17
|
+
NRSER.class_method_objects_for self, *args
|
18
|
+
end
|
19
|
+
|
20
|
+
alias_method :class_Methods, :class_method_objects
|
21
|
+
|
22
|
+
|
23
|
+
# Calls {NRSER.own_class_method_objects_for} with `self` as first arg.
|
24
|
+
def own_class_method_objects *args
|
25
|
+
NRSER.own_class_method_objects_for self, *args
|
26
|
+
end
|
27
|
+
|
28
|
+
alias_method :own_class_Methods, :own_class_method_objects
|
29
|
+
|
30
|
+
|
31
|
+
# Calls {NRSER.instance_method_objects_for} with `self` as first arg.
|
32
|
+
def instance_method_objects *args
|
33
|
+
NRSER.instance_method_objects_for self, *args
|
34
|
+
end
|
35
|
+
|
36
|
+
alias_method :instance_Methods, :instance_method_objects
|
37
|
+
|
38
|
+
|
39
|
+
# Calls {NRSER.own_instance_method_objects_for} with `self` as first arg.
|
40
|
+
def own_instance_method_objects *args
|
41
|
+
NRSER.own_instance_method_objects_for self, *args
|
42
|
+
end
|
43
|
+
|
44
|
+
alias_method :own_instance_Methods, :own_instance_method_objects
|
45
|
+
|
46
|
+
|
47
|
+
# Calls {NRSER.class_method_source_locations_for} with `self` as the
|
48
|
+
# first arg.
|
49
|
+
def class_method_source_locations *args
|
50
|
+
NRSER.class_method_source_locations_for self, *args
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
# Calls {NRSER.class_method_source_locations_for} with `self` as the
|
55
|
+
# first arg.
|
56
|
+
def own_class_method_source_locations *args
|
57
|
+
NRSER.own_class_method_source_locations_for self, *args
|
58
|
+
end
|
59
|
+
|
60
|
+
# @!endgroup Module Methods
|
61
|
+
|
62
|
+
end # module NRSER::Ext::String
|
data/lib/nrser/ext.rb
CHANGED
@@ -40,4 +40,37 @@ module NRSER
|
|
40
40
|
bnd.local_variables.map { |symbol| bnd.local_variable_get symbol }
|
41
41
|
end # .locals
|
42
42
|
|
43
|
+
|
44
|
+
def self.call_block bnd, *args, &block
|
45
|
+
arg_count = 0
|
46
|
+
arg_rest = false
|
47
|
+
call_kwds = {}
|
48
|
+
block.parameters.each do |type, name|
|
49
|
+
logger.debug "Processing", type: type, name: name
|
50
|
+
|
51
|
+
case type
|
52
|
+
when :req, :opt
|
53
|
+
arg_count += 1
|
54
|
+
when :keyreq, :key
|
55
|
+
if bnd.local_variable_defined? name
|
56
|
+
call_kwds[name] = bnd.local_variable_get name
|
57
|
+
end
|
58
|
+
when :rest
|
59
|
+
arg_rest = true
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
call_args = if arg_rest
|
64
|
+
args
|
65
|
+
else
|
66
|
+
args[0...arg_count]
|
67
|
+
end
|
68
|
+
|
69
|
+
logger.debug "CALLING WITH",
|
70
|
+
args: call_args,
|
71
|
+
kwds: call_kwds
|
72
|
+
|
73
|
+
block.call *call_args, **call_kwds
|
74
|
+
end
|
75
|
+
|
43
76
|
end # module NRSER
|
@@ -0,0 +1,103 @@
|
|
1
|
+
##
|
2
|
+
# Functions for associating entries in an {Enumerable} as key or values in
|
3
|
+
# a {Hash}.
|
4
|
+
##
|
5
|
+
|
6
|
+
module NRSER
|
7
|
+
|
8
|
+
# @!group Enumerable Functions
|
9
|
+
|
10
|
+
# Convert an enumerable to a hash by passing each entry through `&block` to
|
11
|
+
# get it's key, raising an error if multiple entries map to the same key.
|
12
|
+
#
|
13
|
+
# @example Basic usage
|
14
|
+
# ['a', :b].to_h_by &:class
|
15
|
+
# # => {String=>"a", Symbol=>:b}
|
16
|
+
#
|
17
|
+
# @example Conflict error
|
18
|
+
# [:a, :b].to_h_by &:class
|
19
|
+
# # NRSER::ConflictError: Key Symbol is already in results with value:
|
20
|
+
# #
|
21
|
+
# # :a
|
22
|
+
# #
|
23
|
+
#
|
24
|
+
# @param [Enumerable<V>] enum
|
25
|
+
# Enumerable containing the values for the hash.
|
26
|
+
#
|
27
|
+
# @param [Proc<(V)=>K>] &block
|
28
|
+
# Block that maps `enum` values to their hash keys.
|
29
|
+
#
|
30
|
+
# @return [Hash<K, V>]
|
31
|
+
#
|
32
|
+
# @raise [NRSER::ConflictError]
|
33
|
+
# If two values map to the same key.
|
34
|
+
#
|
35
|
+
def self.assoc_by enum, &block
|
36
|
+
enum.each_with_object( {} ) { |element, result|
|
37
|
+
key = block.call element
|
38
|
+
|
39
|
+
if result.key? key
|
40
|
+
raise NRSER::ConflictError.new erb binding, <<-END
|
41
|
+
Key <%= key.inspect %> is already in results with value:
|
42
|
+
|
43
|
+
<%= result[key].pretty_inspect %>
|
44
|
+
|
45
|
+
END
|
46
|
+
end
|
47
|
+
|
48
|
+
result[key] = element
|
49
|
+
}
|
50
|
+
end # .to_h_by
|
51
|
+
|
52
|
+
singleton_class.send :alias_method, :to_h_by, :assoc_by
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
# Create a {Hash} mapping the entries in `enum` to the value returned by
|
57
|
+
# passing them through `&block`, raising on conflicts.
|
58
|
+
#
|
59
|
+
#
|
60
|
+
# @param [type] arg_name
|
61
|
+
# @todo Add name param description.
|
62
|
+
#
|
63
|
+
# @return [return_type]
|
64
|
+
# @todo Document return value.
|
65
|
+
#
|
66
|
+
def self.assoc_to enum, on_conflict: :raise, &block
|
67
|
+
enum.each_with_object( {} ) { |entry, hash|
|
68
|
+
value = if hash.key? entry
|
69
|
+
case on_conflict
|
70
|
+
when :raise
|
71
|
+
raise NRSER::ConflictError.new erb binding, <<-END
|
72
|
+
Entry <%= entry %> appears more than once in `enum`
|
73
|
+
|
74
|
+
This would cause conflict in the resulting {Hash}.
|
75
|
+
|
76
|
+
Entry:
|
77
|
+
|
78
|
+
<%= entry.pretty_inspect %>
|
79
|
+
|
80
|
+
END
|
81
|
+
when :first_wins
|
82
|
+
# do nothing
|
83
|
+
when :last_wins
|
84
|
+
hash[entry] = block.call entry
|
85
|
+
when Proc
|
86
|
+
hash[entry] = on_conflict.call \
|
87
|
+
entry: entry,
|
88
|
+
current_value: hash[entry],
|
89
|
+
block: block
|
90
|
+
else
|
91
|
+
raise ArgumentError,
|
92
|
+
"Bad `on_conflict`: #{ on_conflict.inspect }"
|
93
|
+
end
|
94
|
+
else
|
95
|
+
block.call entry
|
96
|
+
end
|
97
|
+
|
98
|
+
hash[entry] = value
|
99
|
+
}
|
100
|
+
end # .map_to
|
101
|
+
|
102
|
+
|
103
|
+
end # module NRSER
|
File without changes
|
@@ -0,0 +1,94 @@
|
|
1
|
+
module NRSER
|
2
|
+
|
3
|
+
# Maps an enumerable object to a *new* hash with the same keys and values
|
4
|
+
# obtained by calling `block` with the current key and value.
|
5
|
+
#
|
6
|
+
# If `enumerable` *does not* respond to `#to_pairs` then it's
|
7
|
+
# treated as a hash where the elements iterated by `#each` are it's keys
|
8
|
+
# and all it's values are `nil`.
|
9
|
+
#
|
10
|
+
# In this way, {NRSER.map_values} handles Hash, Array, Set, OpenStruct,
|
11
|
+
# and probably pretty much anything else reasonable you may throw at it.
|
12
|
+
#
|
13
|
+
# @param [#each_pair, #each] enum
|
14
|
+
#
|
15
|
+
# @yieldparam [Object] key
|
16
|
+
# The key that will be used for whatever value the block returns in the
|
17
|
+
# new hash.
|
18
|
+
#
|
19
|
+
# @yieldparam [nil, Object] value
|
20
|
+
# If `enumerable` responds to `#each_pair`, the second parameter it yielded
|
21
|
+
# along with `key`. Otherwise `nil`.
|
22
|
+
#
|
23
|
+
# @yieldreturn [Object]
|
24
|
+
# Value for the new hash.
|
25
|
+
#
|
26
|
+
# @return [Hash]
|
27
|
+
#
|
28
|
+
# @raise [ArgumentError]
|
29
|
+
# If `enumerable` does not respond to `#each_pair` or `#each`.
|
30
|
+
#
|
31
|
+
|
32
|
+
# Kind-of a Swiss Army knife for "I have the keys right, but I need to do
|
33
|
+
# some work to get the values I want".
|
34
|
+
#
|
35
|
+
# @overload map_values hash, &block
|
36
|
+
#
|
37
|
+
# Maps `hash` (which doesn't need to be a {Hash}, just support
|
38
|
+
# `#each_pair` in the same way) to a new {Hash} with the same keys and
|
39
|
+
# values created by calling `&block`.
|
40
|
+
#
|
41
|
+
# The arguments yielded to `&block` depend it's `#arity`:
|
42
|
+
#
|
43
|
+
# 1. If `&block` has more than one required argument
|
44
|
+
# (`block.arity > 1 || block.arity < -2`) then `key` and `value`
|
45
|
+
# will be yielded, so this works:
|
46
|
+
#
|
47
|
+
# NRSER.map_values( hash ) { |key, value| ... }
|
48
|
+
#
|
49
|
+
# 2. If `&block` has one required argument or less
|
50
|
+
# (`-2 <= block.arity <= 1`) then just `value` is yielded, so this
|
51
|
+
# also works:
|
52
|
+
#
|
53
|
+
# NRSER.map_values( hash ) { |value| ... }
|
54
|
+
#
|
55
|
+
# @overload map_values enumerable, &block
|
56
|
+
#
|
57
|
+
def self.map_values enum, &block
|
58
|
+
result = {}
|
59
|
+
|
60
|
+
arity = block.arity
|
61
|
+
|
62
|
+
if enum.respond_to? :each_pair
|
63
|
+
enum.each_pair { |key, value|
|
64
|
+
result[key] = if arity > 1 || arity < -2
|
65
|
+
block.call key, value
|
66
|
+
else
|
67
|
+
block.call value
|
68
|
+
end
|
69
|
+
}
|
70
|
+
elsif enum.respond_to? :each
|
71
|
+
value = nil
|
72
|
+
enum.each_with_index { |key, index|
|
73
|
+
result[key] = if arity > 1 || arity < -2
|
74
|
+
block.call key, nil
|
75
|
+
else
|
76
|
+
block.call key
|
77
|
+
end
|
78
|
+
}
|
79
|
+
else
|
80
|
+
raise ArgumentError.new erb binding, <<-END
|
81
|
+
First argument to {NRSER.map_values} must respond to #each_pair or #each
|
82
|
+
|
83
|
+
Received
|
84
|
+
|
85
|
+
<%= enum.pretty_inspect %>
|
86
|
+
|
87
|
+
of class <%= enum.class %>
|
88
|
+
END
|
89
|
+
end
|
90
|
+
|
91
|
+
result
|
92
|
+
end # .map_values
|
93
|
+
|
94
|
+
end # module NRSER
|
@@ -1,6 +1,8 @@
|
|
1
1
|
require_relative './enumerable/find_map'
|
2
2
|
require_relative './enumerable/find_all_map'
|
3
3
|
require_relative './enumerable/include_slice'
|
4
|
+
require_relative './enumerable/associate'
|
5
|
+
require_relative './enumerable/map_values'
|
4
6
|
|
5
7
|
module NRSER
|
6
8
|
|
@@ -35,61 +37,6 @@ module NRSER
|
|
35
37
|
object.respond_to?( :each_pair )
|
36
38
|
end # .hash_like?
|
37
39
|
|
38
|
-
|
39
|
-
# Maps an enumerable object to a *new* hash with the same keys and values
|
40
|
-
# obtained by calling `block` with the current key and value.
|
41
|
-
#
|
42
|
-
# If `enumerable` *does not* respond to `#to_pairs` then it's
|
43
|
-
# treated as a hash where the elements iterated by `#each` are it's keys
|
44
|
-
# and all it's values are `nil`.
|
45
|
-
#
|
46
|
-
# In this way, {NRSER.map_values} handles Hash, Array, Set, OpenStruct,
|
47
|
-
# and probably pretty much anything else reasonable you may throw at it.
|
48
|
-
#
|
49
|
-
# @param [#each_pair, #each] enum
|
50
|
-
#
|
51
|
-
# @yieldparam [Object] key
|
52
|
-
# The key that will be used for whatever value the block returns in the
|
53
|
-
# new hash.
|
54
|
-
#
|
55
|
-
# @yieldparam [nil, Object] value
|
56
|
-
# If `enumerable` responds to `#each_pair`, the second parameter it yielded
|
57
|
-
# along with `key`. Otherwise `nil`.
|
58
|
-
#
|
59
|
-
# @yieldreturn [Object]
|
60
|
-
# Value for the new hash.
|
61
|
-
#
|
62
|
-
# @return [Hash]
|
63
|
-
#
|
64
|
-
# @raise [TypeError]
|
65
|
-
# If `enumerable` does not respond to `#each_pair` or `#each`.
|
66
|
-
#
|
67
|
-
def self.map_values enum, &block
|
68
|
-
result = {}
|
69
|
-
|
70
|
-
if enum.respond_to? :each_pair
|
71
|
-
enum.each_pair { |key, value|
|
72
|
-
result[key] = block.call key, value
|
73
|
-
}
|
74
|
-
elsif enum.respond_to? :each
|
75
|
-
enum.each { |key|
|
76
|
-
result[key] = block.call key, nil
|
77
|
-
}
|
78
|
-
else
|
79
|
-
raise ArgumentError.new erb binding, <<-END
|
80
|
-
First argument to {NRSER.map_values} must respond to #each_pair or #each
|
81
|
-
|
82
|
-
Received
|
83
|
-
|
84
|
-
<%= enum.pretty_inspect %>
|
85
|
-
|
86
|
-
of class <%= enum.class %>
|
87
|
-
END
|
88
|
-
end
|
89
|
-
|
90
|
-
result
|
91
|
-
end # .map_values
|
92
|
-
|
93
40
|
|
94
41
|
# Find all entries in an {Enumerable} for which `&block` returns a truthy
|
95
42
|
# value, then check the amount of results found against the
|
@@ -218,38 +165,6 @@ module NRSER
|
|
218
165
|
end # .only!
|
219
166
|
|
220
167
|
|
221
|
-
# Convert an enumerable to a hash by passing each entry through `&block` to
|
222
|
-
# get it's key, raising an error if multiple entries map to the same key.
|
223
|
-
#
|
224
|
-
# @param [Enumerable<V>] enum
|
225
|
-
# Enumerable containing the values for the hash.
|
226
|
-
#
|
227
|
-
# @param [Proc<(V)=>K>] &block
|
228
|
-
# Block that maps `enum` values to their hash keys.
|
229
|
-
#
|
230
|
-
# @return [Hash<K, V>]
|
231
|
-
#
|
232
|
-
# @raise [NRSER::ConflictError]
|
233
|
-
# If two values map to the same key.
|
234
|
-
#
|
235
|
-
def self.to_h_by enum, &block
|
236
|
-
enum.each_with_object( {} ) { |element, result|
|
237
|
-
key = block.call element
|
238
|
-
|
239
|
-
if result.key? key
|
240
|
-
raise NRSER::ConflictError.new erb binding, <<-END
|
241
|
-
Key <%= key.inspect %> is already in results with value:
|
242
|
-
|
243
|
-
<%= result[key].pretty_inspect %>
|
244
|
-
|
245
|
-
END
|
246
|
-
end
|
247
|
-
|
248
|
-
result[key] = element
|
249
|
-
}
|
250
|
-
end # .to_h_by
|
251
|
-
|
252
|
-
|
253
168
|
# Create an {Enumerator} that iterates over the "values" of an
|
254
169
|
# {Enumerable} `enum`. If `enum` responds to `#each_value` than we return
|
255
170
|
# that. Otherwise, we return `#each_entry`.
|
@@ -0,0 +1,206 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
##
|
4
|
+
# Functions to get {Method} and {UnboundMethod} instances for class and
|
5
|
+
# instance methods (respectively) of a {Module}.
|
6
|
+
#
|
7
|
+
# @note
|
8
|
+
# Methods added to {NRSER::Ext::Module} can't be named the same as they will
|
9
|
+
# be in the extension due to shadowing.
|
10
|
+
#
|
11
|
+
##
|
12
|
+
|
13
|
+
|
14
|
+
# Definitions
|
15
|
+
# =======================================================================
|
16
|
+
|
17
|
+
module NRSER
|
18
|
+
|
19
|
+
# @!group Module Functions
|
20
|
+
# ==========================================================================
|
21
|
+
|
22
|
+
# Core private method that supports all the other "method getters".
|
23
|
+
#
|
24
|
+
# @private
|
25
|
+
#
|
26
|
+
# @param [Module] mod
|
27
|
+
# Module in question.
|
28
|
+
#
|
29
|
+
# @param [Boolean] include_super
|
30
|
+
# When `true`, includes inherited class methods.
|
31
|
+
#
|
32
|
+
# @param [:class | :instance] type:
|
33
|
+
# Get class or instance methods.
|
34
|
+
#
|
35
|
+
# @param [Boolean] sort:
|
36
|
+
# If `true`, will sort the methods by name, which is usually
|
37
|
+
# the useful way to look at and use them.
|
38
|
+
#
|
39
|
+
# @return [Array<(Method | UnboundMethod)>]
|
40
|
+
# List of method objects (all bound to `mod`).
|
41
|
+
#
|
42
|
+
def self.method_objects_for mod,
|
43
|
+
include_super,
|
44
|
+
type:,
|
45
|
+
sort:,
|
46
|
+
include_initialize: false
|
47
|
+
initialize_method = nil
|
48
|
+
|
49
|
+
get_names, get_method = case type
|
50
|
+
when :class
|
51
|
+
[:methods, :method]
|
52
|
+
|
53
|
+
when :instance
|
54
|
+
if include_initialize
|
55
|
+
# Only way I can figure out to find out if it is defined it to try
|
56
|
+
# to get the object and handle the error
|
57
|
+
begin
|
58
|
+
initialize_method = mod.instance_method :initialize
|
59
|
+
rescue NameError => error
|
60
|
+
else
|
61
|
+
# Don't want to include it if we're not `include_super` and it's
|
62
|
+
# inherited from a different module
|
63
|
+
unless include_super || initialize_method.owner == mod
|
64
|
+
initialize_method = nil
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
[:instance_methods, :instance_method]
|
70
|
+
|
71
|
+
else
|
72
|
+
raise ArgumentError,
|
73
|
+
"`type:` must be `:class` or `:instance`, found #{ type.inspect }"
|
74
|
+
|
75
|
+
end # case type
|
76
|
+
|
77
|
+
methods = mod.send( get_names, include_super ).map { |name|
|
78
|
+
mod.send get_method, name
|
79
|
+
}
|
80
|
+
|
81
|
+
methods << initialize_method unless initialize_method.nil?
|
82
|
+
|
83
|
+
methods.sort! { |a, b| a.name <=> b.name } if sort
|
84
|
+
|
85
|
+
methods
|
86
|
+
end # .method_objects_for
|
87
|
+
|
88
|
+
private_class_method :method_objects_for
|
89
|
+
|
90
|
+
|
91
|
+
# Get class methods for a {Module} ({Class} are also {Module}, so works
|
92
|
+
# same for those).
|
93
|
+
#
|
94
|
+
# @param mod (see .method_objects_for)
|
95
|
+
# @param include_super (see .method_objects_for)
|
96
|
+
# @param sort: (see .method_objects_for)
|
97
|
+
#
|
98
|
+
# @return [Array<Method>]
|
99
|
+
# List of method objects (all bound to `mod`).
|
100
|
+
#
|
101
|
+
def self.class_method_objects_for mod, include_super = true, sort: true
|
102
|
+
method_objects_for mod, include_super, type: :class, sort: sort
|
103
|
+
end # .class_method_objects_for
|
104
|
+
|
105
|
+
# Much shorter name
|
106
|
+
#
|
107
|
+
# @todo Not sure if I like this...
|
108
|
+
#
|
109
|
+
singleton_class.send :alias_method,
|
110
|
+
:class_Methods_for, :class_method_objects_for
|
111
|
+
|
112
|
+
|
113
|
+
# Just get the class methods defined in `mod` itself, omitting inherited
|
114
|
+
# ones.
|
115
|
+
#
|
116
|
+
# Equivalent to
|
117
|
+
#
|
118
|
+
# NRSER.class_method_objects_for false
|
119
|
+
#
|
120
|
+
# @param mod (see .class_method_objects_for)
|
121
|
+
# @param sort: (see .class_method_objects_for)
|
122
|
+
# @return (see .class_method_objects_for)
|
123
|
+
#
|
124
|
+
def self.own_class_method_objects_for mod, sort: true
|
125
|
+
class_method_objects_for mod, false, sort: sort
|
126
|
+
end # .own_class_method_objects_for
|
127
|
+
|
128
|
+
# Much shorter name
|
129
|
+
#
|
130
|
+
# @todo Not sure if I like this...
|
131
|
+
#
|
132
|
+
singleton_class.send :alias_method,
|
133
|
+
:own_class_Methods_for,
|
134
|
+
:own_class_method_objects_for
|
135
|
+
|
136
|
+
|
137
|
+
# Get instance methods for a {Module} ({Class} are also {Module}, so works
|
138
|
+
# same for those).
|
139
|
+
#
|
140
|
+
# @param mod (see .method_objects_for)
|
141
|
+
# @param include_super (see .method_objects_for)
|
142
|
+
# @param sort: (see .method_objects_for)
|
143
|
+
#
|
144
|
+
# @param [Boolean] include_initialize:
|
145
|
+
# When `true`, include `#initialize` method if it's defined, which is
|
146
|
+
# normally excluded from {Module#instance_methods}.
|
147
|
+
#
|
148
|
+
# Respects `include_super` - won't include it if we are only looking for
|
149
|
+
# own instance methods and it's inherited.
|
150
|
+
#
|
151
|
+
# @return [Array<UnboundMethod>]
|
152
|
+
# List of method objects (all unbound).
|
153
|
+
#
|
154
|
+
def self.instance_method_objects_for mod,
|
155
|
+
include_super = true,
|
156
|
+
sort: true,
|
157
|
+
include_initialize: false
|
158
|
+
method_objects_for \
|
159
|
+
mod,
|
160
|
+
include_super,
|
161
|
+
type: :instance,
|
162
|
+
sort: sort,
|
163
|
+
include_initialize: include_initialize
|
164
|
+
end # .instance_method_objects_for
|
165
|
+
|
166
|
+
# Much shorter name
|
167
|
+
#
|
168
|
+
# @todo Not sure if I like this...
|
169
|
+
#
|
170
|
+
singleton_class.send :alias_method,
|
171
|
+
:instance_Methods_for, :instance_method_objects_for
|
172
|
+
|
173
|
+
|
174
|
+
# Just get the instance methods defined in `mod` itself, omitting inherited
|
175
|
+
# ones.
|
176
|
+
#
|
177
|
+
# Equivalent to
|
178
|
+
#
|
179
|
+
# NRSER.instance_method_objects_for false
|
180
|
+
#
|
181
|
+
# @param mod (see .instance_method_objects_for)
|
182
|
+
# @param sort: (see .instance_method_objects_for)
|
183
|
+
# @return (see .instance_method_objects_for)
|
184
|
+
#
|
185
|
+
def self.own_instance_method_objects_for mod,
|
186
|
+
sort: true,
|
187
|
+
include_initialize: false
|
188
|
+
instance_method_objects_for \
|
189
|
+
mod,
|
190
|
+
false,
|
191
|
+
sort: sort,
|
192
|
+
include_initialize: include_initialize
|
193
|
+
end # .own_instance_method_objects_for
|
194
|
+
|
195
|
+
# Much shorter name
|
196
|
+
#
|
197
|
+
# @todo Not sure if I like this...
|
198
|
+
#
|
199
|
+
singleton_class.send :alias_method,
|
200
|
+
:own_instance_Methods_for,
|
201
|
+
:own_instance_method_objects_for
|
202
|
+
|
203
|
+
|
204
|
+
# @!endgroup Module Functions
|
205
|
+
|
206
|
+
end # module NRSER
|