nrser 0.2.2 → 0.3.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.
Files changed (188) hide show
  1. checksums.yaml +4 -4
  2. data/lib/nrser/char/alpha_numeric_sub.rb +1 -2
  3. data/lib/nrser/char.rb +0 -6
  4. data/lib/nrser/core_ext/array.rb +120 -0
  5. data/lib/nrser/core_ext/binding.rb +44 -0
  6. data/lib/nrser/{functions → core_ext}/enumerable/find_map.rb +18 -15
  7. data/lib/nrser/{ext → core_ext}/enumerable.rb +10 -24
  8. data/lib/nrser/core_ext/exception.rb +30 -0
  9. data/lib/nrser/core_ext/hash/extract_values_at.rb +49 -0
  10. data/lib/nrser/core_ext/hash/transform_values_with_keys.rb +24 -0
  11. data/lib/nrser/core_ext/hash.rb +50 -0
  12. data/lib/nrser/core_ext/module/method_objects.rb +96 -0
  13. data/lib/nrser/core_ext/module/names.rb +69 -0
  14. data/lib/nrser/core_ext/module/source_locations.rb +214 -0
  15. data/lib/nrser/core_ext/module.rb +2 -0
  16. data/lib/nrser/core_ext/object/lazy_var.rb +31 -0
  17. data/lib/nrser/core_ext/object.rb +46 -0
  18. data/lib/nrser/core_ext/open_struct.rb +6 -0
  19. data/lib/nrser/{ext → core_ext}/pathname.rb +8 -5
  20. data/lib/nrser/{ext → core_ext}/string.rb +6 -12
  21. data/lib/nrser/core_ext/symbol.rb +13 -0
  22. data/lib/nrser/core_ext/time.rb +46 -0
  23. data/lib/nrser/core_ext.rb +13 -0
  24. data/lib/nrser/errors/abstract_method_error.rb +150 -0
  25. data/lib/nrser/errors/argument_error.rb +42 -0
  26. data/lib/nrser/errors/nicer_error.rb +298 -72
  27. data/lib/nrser/errors/type_error.rb +46 -0
  28. data/lib/nrser/errors.rb +4 -53
  29. data/lib/nrser/ext/tree.rb +3 -0
  30. data/lib/nrser/functions/enumerable/associate.rb +6 -9
  31. data/lib/nrser/functions/enumerable/include_slice.rb +2 -3
  32. data/lib/nrser/functions/enumerable.rb +1 -3
  33. data/lib/nrser/functions/exception.rb +1 -1
  34. data/lib/nrser/functions/hash.rb +0 -6
  35. data/lib/nrser/functions/merge_by.rb +2 -2
  36. data/lib/nrser/functions/module/method_objects.rb +77 -0
  37. data/lib/nrser/functions/module.rb +1 -2
  38. data/lib/nrser/functions/open_struct.rb +25 -35
  39. data/lib/nrser/functions/proc.rb +1 -6
  40. data/lib/nrser/functions/string/looks_like.rb +32 -1
  41. data/lib/nrser/functions/string.rb +1 -40
  42. data/lib/nrser/functions/text/lines.rb +2 -1
  43. data/lib/nrser/functions.rb +0 -1
  44. data/lib/nrser/graph/tsorter.rb +41 -0
  45. data/lib/nrser/labs/core_ext/binding.rb +37 -0
  46. data/lib/nrser/labs/stash.rb +372 -0
  47. data/lib/nrser/{logging → log}/appender/sync.rb +3 -3
  48. data/lib/nrser/log/appender.rb +3 -0
  49. data/lib/nrser/{logging → log}/formatters/color.rb +47 -20
  50. data/lib/nrser/log/formatters/mixin.rb +270 -0
  51. data/lib/nrser/{logging → log}/formatters.rb +0 -0
  52. data/lib/nrser/log/logger.rb +229 -0
  53. data/lib/nrser/log/mixin.rb +56 -0
  54. data/lib/nrser/log.rb +723 -0
  55. data/lib/nrser/message.rb +24 -3
  56. data/lib/nrser/meta/source/location.rb +158 -0
  57. data/lib/nrser/meta.rb +1 -1
  58. data/lib/nrser/props/class_methods.rb +118 -0
  59. data/lib/nrser/props/immutable/hash.rb +111 -0
  60. data/lib/nrser/props/immutable/hash_variable.rb +82 -0
  61. data/lib/nrser/props/immutable/instance_variables.rb +48 -0
  62. data/lib/nrser/props/immutable/vector.rb +107 -0
  63. data/lib/nrser/props/instance_methods.rb +184 -0
  64. data/lib/nrser/props/metadata.rb +359 -0
  65. data/lib/nrser/props/mutable/instance_variables.rb +60 -0
  66. data/lib/nrser/props/mutable/stash.rb +199 -0
  67. data/lib/nrser/{meta/props → props}/prop.rb +217 -112
  68. data/lib/nrser/props/storage/instance_variable.rb +85 -0
  69. data/lib/nrser/props/storage/instance_variables.rb +67 -0
  70. data/lib/nrser/props/storage/key.rb +88 -0
  71. data/lib/nrser/props.rb +9 -0
  72. data/lib/nrser/refinements/sugar.rb +41 -0
  73. data/lib/nrser/refinements/types.rb +2 -2
  74. data/lib/nrser/refinements.rb +14 -16
  75. data/lib/nrser/rspex/example_group/describe_attribute.rb +24 -0
  76. data/lib/nrser/rspex/example_group/describe_called_with.rb +1 -6
  77. data/lib/nrser/rspex/example_group/{describe_use_case.rb → describe_case.rb} +6 -3
  78. data/lib/nrser/rspex/example_group/describe_class.rb +1 -0
  79. data/lib/nrser/rspex/example_group/describe_group.rb +29 -0
  80. data/lib/nrser/rspex/example_group/describe_instance_method.rb +2 -2
  81. data/lib/nrser/rspex/example_group/describe_message.rb +35 -0
  82. data/lib/nrser/rspex/example_group/describe_method.rb +23 -2
  83. data/lib/nrser/rspex/example_group/describe_module.rb +19 -0
  84. data/lib/nrser/rspex/example_group/describe_response_to.rb +32 -0
  85. data/lib/nrser/rspex/example_group/describe_section.rb +38 -0
  86. data/lib/nrser/rspex/example_group/describe_sent_to.rb +52 -0
  87. data/lib/nrser/rspex/example_group/describe_source_file.rb +49 -0
  88. data/lib/nrser/rspex/example_group/describe_spec_file.rb +41 -108
  89. data/lib/nrser/rspex/example_group/describe_when.rb +14 -7
  90. data/lib/nrser/rspex/example_group/describe_x.rb +39 -12
  91. data/lib/nrser/rspex/example_group/overrides.rb +66 -0
  92. data/lib/nrser/rspex/example_group.rb +20 -252
  93. data/lib/nrser/rspex/format.rb +83 -17
  94. data/lib/nrser/rspex.rb +4 -34
  95. data/lib/nrser/sugar/method_missing_forwarder.rb +50 -0
  96. data/lib/nrser/{env → sys/env}/path.rb +1 -2
  97. data/lib/nrser/{env.rb → sys/env.rb} +2 -1
  98. data/lib/nrser/sys.rb +5 -0
  99. data/lib/nrser/types/any.rb +36 -7
  100. data/lib/nrser/types/{array.rb → arrays.rb} +32 -81
  101. data/lib/nrser/types/attrs.rb +68 -15
  102. data/lib/nrser/types/booleans.rb +95 -34
  103. data/lib/nrser/types/bounded.rb +12 -10
  104. data/lib/nrser/types/combinators.rb +74 -37
  105. data/lib/nrser/types/errors/check_error.rb +86 -0
  106. data/lib/nrser/types/errors/from_string_error.rb +82 -0
  107. data/lib/nrser/types/factory.rb +91 -0
  108. data/lib/nrser/types/hashes.rb +171 -26
  109. data/lib/nrser/types/in.rb +25 -12
  110. data/lib/nrser/types/is.rb +50 -18
  111. data/lib/nrser/types/is_a.rb +52 -33
  112. data/lib/nrser/types/labels.rb +6 -33
  113. data/lib/nrser/types/maybe.rb +12 -4
  114. data/lib/nrser/types/nil.rb +24 -4
  115. data/lib/nrser/types/not.rb +6 -16
  116. data/lib/nrser/types/numbers.rb +94 -57
  117. data/lib/nrser/types/pairs.rb +57 -57
  118. data/lib/nrser/types/paths.rb +112 -133
  119. data/lib/nrser/types/responds.rb +64 -74
  120. data/lib/nrser/types/shape.rb +29 -24
  121. data/lib/nrser/types/strings.rb +25 -17
  122. data/lib/nrser/types/symbols.rb +19 -17
  123. data/lib/nrser/types/trees.rb +18 -70
  124. data/lib/nrser/types/tuples.rb +36 -40
  125. data/lib/nrser/types/type.rb +342 -91
  126. data/lib/nrser/types/when.rb +40 -18
  127. data/lib/nrser/types/where.rb +94 -9
  128. data/lib/nrser/types.rb +72 -63
  129. data/lib/nrser/version.rb +1 -1
  130. data/lib/nrser.rb +18 -18
  131. data/spec/lib/nrser/{functions/binding/template_spec.rb → core_ext/binding/erb_spec.rb} +5 -5
  132. data/spec/lib/nrser/{functions → core_ext}/enumerable/find_map_spec.rb +8 -6
  133. data/spec/lib/nrser/{refinements → core_ext}/hash_spec.rb +9 -22
  134. data/spec/lib/nrser/errors/abstract_method_error_spec.rb +12 -5
  135. data/spec/lib/nrser/functions/enumerable/{to_h_by_spec.rb → associate_spec.rb} +1 -1
  136. data/spec/lib/nrser/functions/merge_by_spec.rb +1 -1
  137. data/spec/lib/nrser/functions/tree/each_branch_spec.rb +3 -3
  138. data/spec/lib/nrser/functions/tree/transform_spec.rb +14 -15
  139. data/spec/lib/nrser/gem_ext/hamster/json_spec.rb +4 -0
  140. data/spec/lib/nrser/meta/source/location_spec.rb +86 -0
  141. data/spec/lib/nrser/props/immutable/hash_spec.rb +297 -0
  142. data/spec/lib/nrser/props/immutable/vector_spec.rb +296 -0
  143. data/spec/lib/nrser/{meta/props_spec.rb → props/original_props_spec.rb} +11 -16
  144. data/spec/lib/nrser/{meta/props → props}/to_and_from_data_spec.rb +10 -8
  145. data/spec/lib/nrser/refinements/array_spec.rb +2 -15
  146. data/spec/lib/nrser/refinements/erb_spec.rb +5 -7
  147. data/spec/lib/nrser/refinements/set_spec.rb +2 -15
  148. data/spec/lib/nrser/{env → sys/env}/path/insert_spec.rb +4 -2
  149. data/spec/lib/nrser/{env → sys/env}/path_spec.rb +4 -2
  150. data/spec/lib/nrser/types/array_spec.rb +8 -8
  151. data/spec/lib/nrser/types/paths_spec.rb +15 -18
  152. data/spec/spec_helper.rb +4 -0
  153. metadata +109 -69
  154. data/lib/nrser/ext/binding.rb +0 -36
  155. data/lib/nrser/ext/module.rb +0 -62
  156. data/lib/nrser/ext.rb +0 -8
  157. data/lib/nrser/functions/binding.rb +0 -76
  158. data/lib/nrser/functions/enumerable/map_keys.rb +0 -0
  159. data/lib/nrser/functions/enumerable/map_values.rb +0 -94
  160. data/lib/nrser/functions/hash/deep_merge.rb +0 -57
  161. data/lib/nrser/functions/hash/except_keys.rb +0 -44
  162. data/lib/nrser/functions/hash/slice_keys.rb +0 -43
  163. data/lib/nrser/functions/hash/stringify_keys.rb +0 -55
  164. data/lib/nrser/functions/hash/symbolize_keys.rb +0 -57
  165. data/lib/nrser/functions/hash/transform_keys.rb +0 -140
  166. data/lib/nrser/functions/module/methods.rb +0 -206
  167. data/lib/nrser/functions/module/source_locations.rb +0 -213
  168. data/lib/nrser/logging/appender.rb +0 -3
  169. data/lib/nrser/logging.rb +0 -353
  170. data/lib/nrser/meta/props/base.rb +0 -31
  171. data/lib/nrser/meta/props.rb +0 -357
  172. data/lib/nrser/refinements/array.rb +0 -133
  173. data/lib/nrser/refinements/binding.rb +0 -6
  174. data/lib/nrser/refinements/enumerator.rb +0 -5
  175. data/lib/nrser/refinements/exception.rb +0 -35
  176. data/lib/nrser/refinements/hash.rb +0 -150
  177. data/lib/nrser/refinements/module.rb +0 -5
  178. data/lib/nrser/refinements/object.rb +0 -42
  179. data/lib/nrser/refinements/open_struct.rb +0 -28
  180. data/lib/nrser/refinements/pathname.rb +0 -5
  181. data/lib/nrser/refinements/set.rb +0 -5
  182. data/lib/nrser/refinements/string.rb +0 -5
  183. data/lib/nrser/refinements/symbol.rb +0 -20
  184. data/lib/nrser/rspex/described.rb +0 -99
  185. data/spec/design/mapping_spec.rb +0 -42
  186. data/spec/lib/nrser/functions/hash_spec.rb +0 -41
  187. data/spec/lib/nrser/functions/string/truncate_spec.rb +0 -11
  188. data/spec/lib/nrser/refinements/truncate_spec.rb +0 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d16770ce888fe9a347449f09975b9ed97eb7ab5
4
- data.tar.gz: ae0979f9123be24d08b8c45ac6d8c99cc4273603
3
+ metadata.gz: 866f9b21a10a8770d932228f5ad747e12782a4ba
4
+ data.tar.gz: bc50260a343986944cfe31e16d7d0e495360ab03
5
5
  SHA512:
6
- metadata.gz: 9b5106ee53cbf739e2b0b549b5273a6a4057679d261abd0a710679a0e9b6cd95b66794f4afe6a883ccea56fc58afb76d56f38f0b3de385e9dc5da22caa3bc450
7
- data.tar.gz: e58f6730e248c028c521295b53568485c277abb7ed6191c285b8a44515a0db97f1fe20054e85b4c6c9f2bc38511f5994dbd7cd715c0d2002c06b6e592b6603f0
6
+ metadata.gz: 0a77767059fb6d66420094f0e20f3fbad8e01e2cf37720df0ef0d20bd86f01193f85df842966a7f1e6fb8175b436f7abc0b5ee9cec6b802cec693ff02186cf0d
7
+ data.tar.gz: 93f5875dc928e78c86206b3552c5a3b7ac7b84b5af4dff1456498cc48a56b6d62d0582420e6a20cc64115f37c4968321bf1993dec1deb38ea8b5eec6901aa90f
@@ -2,11 +2,10 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
 
5
-
6
5
  # Refinements
7
6
  # =======================================================================
8
7
 
9
- using NRSER
8
+ require 'nrser/refinements/types'
10
9
  using NRSER::Types
11
10
 
12
11
 
data/lib/nrser/char.rb CHANGED
@@ -2,12 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
 
5
- # Refinements
6
- # =======================================================================
7
-
8
- using NRSER
9
-
10
-
11
5
  # Declarations
12
6
  # ============================================================================
13
7
 
@@ -0,0 +1,120 @@
1
+ class Array
2
+ include NRSER::Ext::Tree
3
+
4
+
5
+ # @return [Array]
6
+ # new array consisting of all elements after the first (which may be
7
+ # none, resulting in an empty array).
8
+ #
9
+ def rest
10
+ NRSER.rest self
11
+ end # #rest
12
+
13
+
14
+ def extract! &block
15
+ NRSER.extract_from_array! self, &block
16
+ end
17
+
18
+
19
+ # Calls {NRSER.ellipsis} on `self`.
20
+ def ellipsis *args
21
+ NRSER.ellipsis self, *args
22
+ end
23
+
24
+
25
+ # `to_*` Converters
26
+ # =====================================================================
27
+
28
+ # Checks that length is 2 and returns `self`.
29
+ #
30
+ # @return [Array]
31
+ # Array of length 2.
32
+ #
33
+ # @raise [TypeError]
34
+ # If length is not 2.
35
+ #
36
+ def to_pair
37
+ unless length == 2
38
+ raise TypeError,
39
+ "Array is not of length 2: #{ self.inspect }"
40
+ end
41
+
42
+ self
43
+ end # #to_pair
44
+
45
+
46
+ # To Operation Objects
47
+ # ---------------------------------------------------------------------
48
+
49
+ # Creates a new {NRSER::Message} from the array.
50
+ #
51
+ # @example
52
+ #
53
+ # message = [:fetch, :x].to_message
54
+ # message.send_to x: 'ex', y: 'why?'
55
+ # # => 'ex'
56
+ #
57
+ # @return [NRSER::Message]
58
+ #
59
+ def to_message
60
+ NRSER::Message.new *self
61
+ end # #to_message
62
+
63
+ alias_method :to_m, :to_message
64
+
65
+
66
+ # Create a {Proc} that accepts a single `receiver` and provides this array's
67
+ # entries as the arguments to `#public_send` (or `#send` if the `publicly`
68
+ # option is `false`).
69
+ #
70
+ # Equivalent to
71
+ #
72
+ # to_message.to_proc publicly: boolean
73
+ #
74
+ # @example
75
+ #
76
+ # [:fetch, :x].sender.call x: 'ex'
77
+ # # => 'ex'
78
+ #
79
+ # @param [Boolean] publicly:
80
+ # When `true`, uses `#public_send` in liu of `#send`.
81
+ #
82
+ # @return [Proc]
83
+ #
84
+ def to_sender publicly: true
85
+ to_message.to_proc publicly: publicly
86
+ end
87
+
88
+
89
+ # See {NRSER.chainer}.
90
+ #
91
+ def to_chainer publicly: true
92
+ NRSER.chainer self, publicly: publicly
93
+ end # #to_chainer
94
+
95
+
96
+ # Returns a lambda that calls accepts a single arg and calls `#dig` on it
97
+ # with the elements of *this* array as arguments.
98
+ #
99
+ # @example
100
+ # list = [{id: 1, name: "Neil"}, {id: 2, name: "Mica"}]
101
+ # list.assoc_by &[:id].to_digger
102
+ # # => {
103
+ # # 1 => {id: 1, name: "Neil"},
104
+ # # 2 => {id: 2, name: "Mica"},
105
+ # # }
106
+ #
107
+ # @todo
108
+ # I wanted to use `#to_proc` so that you could use `&[:id]`, but unary
109
+ # `&` doesn't invoke refinements, and I don't really want to monkey-patch
110
+ # anything, especially something as core as `#to_proc` and `Array`.
111
+ #
112
+ # @return [Proc]
113
+ # Lambda proc that accepts a single argument and calls `#dig` with this
114
+ # array's contents as the `#dig` arguments.
115
+ #
116
+ def to_digger
117
+ NRSER::Message.new( :dig, *self ).to_proc
118
+ end # #to_digger
119
+
120
+ end # class Array
@@ -0,0 +1,44 @@
1
+ require_relative './string'
2
+ require_relative './enumerable'
3
+
4
+ # Extension methods for {Binding}
5
+ #
6
+ class Binding
7
+
8
+ # Calls {NRSER.template} with `self` prepended to `*args`
9
+ #
10
+ # @param (see NRSER.erb)
11
+ # @return (see NRSER.erb)
12
+ #
13
+ def erb source
14
+ require 'erb'
15
+
16
+ NRSER.filter_repeated_blank_lines(
17
+ NRSER.with_indent_tagged( NRSER.dedent( source ) ) { |tagged_str|
18
+ ERB.new( tagged_str ).result( self )
19
+ },
20
+ remove_leading: true
21
+ )
22
+ end
23
+
24
+ alias_method :template, :erb
25
+
26
+
27
+ # Get a {Hash} of all local variable names (as {Symbol}) to values.
28
+ #
29
+ # @return [Hash<Symbol, Object>]
30
+ #
31
+ def locals
32
+ self.local_variables.assoc_to { |symbol| local_variable_get symbol }
33
+ end
34
+
35
+
36
+ # Get a {Array} of all local variable values.
37
+ #
38
+ # @return [Array<Object>]
39
+ #
40
+ def local_values
41
+ self.local_variables.map { |symbol| local_variable_get symbol }
42
+ end
43
+
44
+ end # class Binding
@@ -1,25 +1,20 @@
1
- module NRSER
2
-
3
- # @!group Enumerable Functions
1
+ module Enumerable
4
2
 
5
3
  # Find the first truthy (not `nil` or `false`) result of calling `&block`
6
- # with entries from `enum`.
4
+ # on entries.
7
5
  #
8
- # Like {Enumerable#find}, accept an optional `ifnone` procedure to call if
6
+ # Like {Enumerable#find}, accepts an optional `ifnone` procedure to call if
9
7
  # no match is found.
10
8
  #
11
9
  # @example
12
10
  #
13
- # NRSER.find_map( [1, 2, 3, 4] ) do |i|
11
+ # [1, 2, 3, 4].find_map do |i|
14
12
  # if i.even?
15
13
  # "#{ i } is even!"
16
14
  # end
17
15
  # end
18
16
  # # => "2 is even!"
19
17
  #
20
- # @param [Enumerable<E>] enum
21
- # Entries to search (in order).
22
- #
23
18
  # @param [nil | Proc<()=>DEFAULT>] ifnone
24
19
  # Optional lambda to call for the return value when no match is found.
25
20
  #
@@ -27,19 +22,27 @@ module NRSER
27
22
  # Block mapping entires to results.
28
23
  #
29
24
  # @return [nil]
30
- # When `block.call( E )` is `nil` or `false` for all `E` in `enum`
25
+ # When `block.call( E )` is `nil` or `false` for all entries `E`
31
26
  # *and* `ifnone` is `nil` or not provided.
32
27
  #
33
28
  # @return [V]
34
- # When `block.call( E )` is `nil` or `false` for all `E` in `enum`
29
+ # When `block.call( E )` is `nil` or `false` for all entries `E`
35
30
  # *and* `ifnone` is a lambda that returns `DEFAULT`.
36
31
  #
37
- # @return [R]
32
+ # @return [RESULT]
38
33
  # The first result `RESLUT = block.call( E )`
39
34
  # where `RESLUT` is not `nil` or `false`.
40
35
  #
41
- def self.find_map enum, ifnone = nil, &block
42
- enum.each do |entry|
36
+ # @return [DEFAULT]
37
+ # When `ifnone` procedure is provided and `&block` returns `nil` or
38
+ # `false` for all entries.
39
+ #
40
+ # @return [nil]
41
+ # When `ifnone` procedure is *not* provided and `&block` returns `nil` or
42
+ # `false` for all entries.
43
+ #
44
+ def find_map ifnone = nil, &block
45
+ each do |entry|
43
46
  if result = block.call( entry )
44
47
  # Found a match, short-circuit
45
48
  return result
@@ -48,6 +51,6 @@ module NRSER
48
51
 
49
52
  # No matches, return `ifnone`
50
53
  ifnone.call if ifnone
51
- end # .find_map
54
+ end # #find_map
52
55
 
53
56
  end # module NRSER
@@ -1,15 +1,9 @@
1
- # Instance methods to extend {Enumerable} objects.
1
+ require_relative './enumerable/find_map'
2
+
3
+
4
+ # Instance methods to extend {Enumerable}.
2
5
  #
3
- # Refined into many of them, including {Array}, {Set}, {Hash} and {OpenStruct},
4
- # and may be independently used as well.
5
- #
6
- module NRSER::Ext::Enumerable
7
-
8
- # See {NRSER.map_values}
9
- def map_values &block
10
- NRSER.map_values self, &block
11
- end
12
-
6
+ module Enumerable
13
7
 
14
8
  # See {NRSER.find_bounded}
15
9
  def find_bounded bounds, &block
@@ -24,17 +18,14 @@ module NRSER::Ext::Enumerable
24
18
 
25
19
 
26
20
  # See {NRSER.assoc_by}
27
- def assoc_by &block
28
- NRSER.assoc_by self, &block
21
+ def assoc_by *args, &block
22
+ NRSER.assoc_by self, *args, &block
29
23
  end
30
24
 
31
- # Old name
32
- alias_method :to_h_by, :assoc_by
33
-
34
25
 
35
26
  # See {NRSER.assoc_to}
36
- def assoc_to &block
37
- NRSER.assoc_to self, &block
27
+ def assoc_to *args, &block
28
+ NRSER.assoc_to self, *args, &block
38
29
  end
39
30
 
40
31
 
@@ -68,15 +59,10 @@ module NRSER::Ext::Enumerable
68
59
  end
69
60
 
70
61
 
71
- # See {NRSER.find_map}
72
- def find_map *args, &block
73
- NRSER.find_map self, *args, &block
74
- end
75
-
76
62
  # See {NRSER.slice?}
77
63
  def slice? *args, &block
78
64
  NRSER.slice? self, *args, &block
79
65
  end
80
66
 
81
67
 
82
- end # module NRSER::Ext::Enumerable
68
+ end # module Enumerable
@@ -0,0 +1,30 @@
1
+ class Exception
2
+ def format
3
+ NRSER.format_exception self
4
+ end
5
+
6
+ # Create a new instance from the squished message.
7
+ #
8
+ # See {NRSER.squish}.
9
+ #
10
+ # @param [String] message
11
+ #
12
+ # @return [Exception]
13
+ #
14
+ def self.squished message
15
+ new NRSER.squish( message )
16
+ end
17
+
18
+ # Create a new instance from the dedented message.
19
+ #
20
+ # See {NRSER.dedent}.
21
+ #
22
+ # @param [String] message
23
+ #
24
+ # @return [Exception]
25
+ #
26
+ def self.dedented message
27
+ new NRSER.dedent( message )
28
+ end
29
+
30
+ end # NRSER
@@ -0,0 +1,49 @@
1
+ class Hash
2
+ # Like {#extract!} combined with {Hash#values_at} - extracts `keys` and
3
+ # appends (via `#<<`) the values to `into` (in order of `keys`).
4
+ #
5
+ # `into` default to an empty Array.
6
+ #
7
+ # @example Basic Usage
8
+ # hash = { a: 1, b: 2, c: 3, d: 4 }
9
+ #
10
+ # hash.extract_values_at! :a, :b
11
+ # # => [1, 2]
12
+ #
13
+ # hash
14
+ # # => {c: 3, d: 4}
15
+ #
16
+ # hash = { a: 1, b: 2, c: 3, d: 4 }
17
+ #
18
+ # hash.extract_values_at! :b, :a
19
+ # # => [2, 1]
20
+ #
21
+ # hash
22
+ # # => {c: 3, d: 4}
23
+ #
24
+ # @example Custom `into
25
+ # hash = { a: 1, b: 2, c: 3, d: 4 }
26
+ # into = Set[1, 3, 5]
27
+ #
28
+ # hash.extract_values_at! :a, :b, into: into
29
+ # # => #<Set: {1, 3, 5, 2}>
30
+ #
31
+ # hash
32
+ # # => {:c=>3, :d=>4}
33
+ #
34
+ # @param *keys
35
+ # Hash keys to extract.
36
+ #
37
+ # @param [#<<] into:
38
+ # Object to extract values at `keys` into.
39
+ #
40
+ # @return [into]
41
+ # The `into` object with the extracted values (if any are found).
42
+ #
43
+ def extract_values_at! *keys, into: []
44
+ keys.each_with_object( into ) { |key, result|
45
+ result << delete(key) if has_key?( key )
46
+ }
47
+ end
48
+
49
+ end
@@ -0,0 +1,24 @@
1
+ class Hash
2
+ # Just like {Hash#transform_values} but yields `key, value`.
3
+ #
4
+ def transform_values_with_keys &block
5
+ return enum_for( __method__ ) { size } unless block_given?
6
+ return {} if empty?
7
+ result = self.class.new
8
+ each do |key, value|
9
+ result[key] = block.call key, value
10
+ end
11
+ result
12
+ end
13
+
14
+
15
+ # Just like {#transform_values_with_keys} but mutates `self`.
16
+ #
17
+ def transform_values_with_keys! &block
18
+ return enum_for( __method__ ) { size } unless block_given?
19
+ each do |key, value|
20
+ self[key] = block.call key, value
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,50 @@
1
+ require 'active_support/core_ext/hash'
2
+
3
+ require 'nrser/ext/tree'
4
+
5
+ require_relative './hash/extract_values_at'
6
+ require_relative './hash/transform_values_with_keys'
7
+
8
+ class Hash
9
+ include NRSER::Ext::Tree
10
+
11
+ # Short names
12
+ alias_method :sym_keys!, :symbolize_keys!
13
+ alias_method :sym_keys, :symbolize_keys
14
+
15
+ alias_method :str_keys!, :stringify_keys!
16
+ alias_method :str_keys, :stringify_keys
17
+
18
+
19
+ # See {NRSER.bury!}
20
+ def bury! key_path,
21
+ value,
22
+ parsed_key_type: :guess,
23
+ clobber: false
24
+ NRSER.bury! self,
25
+ key_path,
26
+ value,
27
+ parsed_key_type: parsed_key_type,
28
+ clobber: clobber
29
+ end
30
+
31
+
32
+ # Checks that `self` contains a single key/value pair (`#length` of 1)
33
+ # and returns it as an array of length 2.
34
+ #
35
+ # @return [Array]
36
+ # Array of length 2.
37
+ #
38
+ # @raise [TypeError]
39
+ # If `self` has more than one key/value pair.
40
+ #
41
+ def to_pair
42
+ unless length == 1
43
+ raise TypeError,
44
+ "Hash has more than one pair: #{ self.inspect }"
45
+ end
46
+
47
+ first
48
+ end
49
+
50
+ end
@@ -0,0 +1,96 @@
1
+ # Extension methods for {Module}
2
+ #
3
+ class Module
4
+
5
+ # @!group {Method} Object Readers
6
+ # ==========================================================================
7
+
8
+ # Get class methods for this {Module} ({Class} are also {Module}, so works
9
+ # same for those).
10
+ #
11
+ # @param include_super (see NRSER.method_objects_for)
12
+ # @param sort: (see NRSER.method_objects_for)
13
+ #
14
+ # @return [Array<Method>]
15
+ # List of method objects (all bound to `self`).
16
+ #
17
+ def class_method_objects include_super = true, sort: true
18
+ NRSER.method_objects_for self,
19
+ include_super,
20
+ type: :class,
21
+ sort: sort
22
+ end
23
+
24
+ alias_method :class_Methods, :class_method_objects
25
+
26
+
27
+ # Just get the class methods defined in this module (or class) itself,
28
+ # omitting inherited ones.
29
+ #
30
+ # Equivalent to
31
+ #
32
+ # #class_method_objects false
33
+ #
34
+ # @param sort: (see .class_method_objects)
35
+ # @return (see .class_method_objects)
36
+ #
37
+ def own_class_method_objects sort: true
38
+ class_method_objects false, sort: sort
39
+ end
40
+
41
+ alias_method :own_class_Methods, :own_class_method_objects
42
+
43
+
44
+ # Get instance methods for this {Module} (or {Class}).
45
+ #
46
+ # @param include_super (see NRSER.method_objects_for)
47
+ # @param sort: (see NRSER.method_objects_for)
48
+ #
49
+ # @param [Boolean] include_initialize:
50
+ # When `true`, include `#initialize` method if it's defined, which is
51
+ # normally excluded from {Module#instance_methods}.
52
+ #
53
+ # Respects `include_super` - won't include it if we are only looking for
54
+ # own instance methods and it's inherited.
55
+ #
56
+ # @return [Array<UnboundMethod>]
57
+ # List of method objects (all unbound).
58
+ #
59
+ def instance_method_objects include_super = true,
60
+ sort: true,
61
+ include_initialize: false
62
+ NRSER.method_objects_for \
63
+ self,
64
+ include_super,
65
+ type: :instance,
66
+ sort: sort,
67
+ include_initialize: include_initialize
68
+ end # #instance_method_objects
69
+
70
+ alias_method :instance_Methods, :instance_method_objects
71
+
72
+
73
+ # Just get the instance methods defined in this {Module} (or {Class}) itself,
74
+ # omitting inherited ones.
75
+ #
76
+ # Equivalent to
77
+ #
78
+ # #instance_method_objects false
79
+ #
80
+ # @param sort: (see #instance_method_objects)
81
+ # @param include_initialize: (see #instance_method_objects)
82
+ #
83
+ # @return (see #instance_method_objects)
84
+ #
85
+ def own_instance_method_objects sort: true,
86
+ include_initialize: false
87
+ instance_method_objects false,
88
+ sort: sort,
89
+ include_initialize: include_initialize
90
+ end
91
+
92
+ alias_method :own_instance_Methods, :own_instance_method_objects
93
+
94
+ # @!endgroup {Method} Object Readers
95
+
96
+ end # class Module
@@ -0,0 +1,69 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+ # Requirements
5
+ # =======================================================================
6
+
7
+ # Stdlib
8
+ # -----------------------------------------------------------------------
9
+
10
+ # Deps
11
+ # -----------------------------------------------------------------------
12
+
13
+ # Need {String#demodulize}
14
+ require 'active_support/core_ext/string/inflections'
15
+
16
+
17
+ # Project / Package
18
+ # -----------------------------------------------------------------------
19
+
20
+
21
+ # Definitions
22
+ # =======================================================================
23
+
24
+ class Module
25
+
26
+ # Like {Module#name} but also returns a {String} for anonymous classes.
27
+ #
28
+ # So you don't need to do any testing or trying when you want to work
29
+ # with the name of a module (or class, which are modules).
30
+ #
31
+ # @return [String]
32
+ #
33
+ def safe_name
34
+ name = self.name
35
+ return name if name.is_a? String
36
+
37
+ # Slice out whatever that hex thingy that anon modules dump in their
38
+ # `#to_s`... `"#<Class:0x00007fa6958c1700>" => "0x00007fa6958c1700"`
39
+ #
40
+ # Might as well use that as an identifier so it matches their `#to_s`,
41
+ # and this should still succeed in whatever funky way even if `#to_s`
42
+ # returns something totally unexpected.
43
+ #
44
+ to_s_hex = self.to_s.split( ':' ).last[0...-1]
45
+
46
+ type_name = if self.is_a?( Class ) then "Class" else "Module" end
47
+
48
+ "Anon#{ type_name }_#{ to_s_hex }"
49
+ end # #safe_name
50
+
51
+
52
+ # Get the {#safe_name} and run ActiveSupport's {String#demodulize} on it
53
+ # to get the module (or class) name without the namespace.
54
+ #
55
+ # @example
56
+ # NRSER::Types.demod_name
57
+ # # => 'Types'
58
+ #
59
+ # @return [String]
60
+ #
61
+ def demodulize_name
62
+ safe_name.demodulize
63
+ end # #demodulize_name
64
+
65
+ # Because I always screw up spelling 'demodulize'
66
+ alias_method :demod_name, :demodulize_name
67
+
68
+
69
+ end