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
@@ -0,0 +1,77 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+ module NRSER
5
+
6
+ # @!group Module Functions
7
+ # ==========================================================================
8
+
9
+ # Core private method that supports all the other "method getters".
10
+ #
11
+ # @private
12
+ #
13
+ # @param [Module] mod
14
+ # Module in question.
15
+ #
16
+ # @param [Boolean] include_super
17
+ # When `true`, includes inherited class methods.
18
+ #
19
+ # @param [:class | :instance] type:
20
+ # Get class or instance methods.
21
+ #
22
+ # @param [Boolean] sort:
23
+ # If `true`, will sort the methods by name, which is usually
24
+ # the useful way to look at and use them.
25
+ #
26
+ # @return [Array<(Method | UnboundMethod)>]
27
+ # List of method objects (all bound to `mod`).
28
+ #
29
+ def self.method_objects_for mod,
30
+ include_super,
31
+ type:,
32
+ sort:,
33
+ include_initialize: false
34
+ initialize_method = nil
35
+
36
+ get_names, get_method = case type
37
+ when :class
38
+ [:methods, :method]
39
+
40
+ when :instance
41
+ if include_initialize
42
+ # Only way I can figure out to find out if it is defined it to try
43
+ # to get the object and handle the error
44
+ begin
45
+ initialize_method = mod.instance_method :initialize
46
+ rescue NameError => error
47
+ else
48
+ # Don't want to include it if we're not `include_super` and it's
49
+ # inherited from a different module
50
+ unless include_super || initialize_method.owner == mod
51
+ initialize_method = nil
52
+ end
53
+ end
54
+ end
55
+
56
+ [:instance_methods, :instance_method]
57
+
58
+ else
59
+ raise ArgumentError,
60
+ "`type:` must be `:class` or `:instance`, found #{ type.inspect }"
61
+
62
+ end # case type
63
+
64
+ methods = mod.send( get_names, include_super ).map { |name|
65
+ mod.send get_method, name
66
+ }
67
+
68
+ methods << initialize_method unless initialize_method.nil?
69
+
70
+ methods.sort! { |a, b| a.name <=> b.name } if sort
71
+
72
+ methods
73
+ end # .method_objects_for
74
+
75
+ # @!endgroup Module Functions
76
+
77
+ end # module NRSER
@@ -1,2 +1 @@
1
- require_relative './module/methods'
2
- require_relative './module/source_locations'
1
+ require_relative './module/method_objects'
@@ -1,9 +1,4 @@
1
1
  module NRSER
2
-
3
- # Eigenclass (Singleton Class)
4
- # ========================================================================
5
- #
6
- class << self
7
2
 
8
3
  # Deeply convert a {Hash} to an {OpenStruct}.
9
4
  #
@@ -14,7 +9,7 @@ module NRSER
14
9
  # @raise [TypeError]
15
10
  # If `hash` is not a {Hash}.
16
11
  #
17
- def to_open_struct hash, freeze: false
12
+ def self.to_open_struct hash, freeze: false
18
13
  unless hash.is_a? Hash
19
14
  raise TypeError,
20
15
  "Argument must be hash (found #{ hash.inspect })"
@@ -23,38 +18,33 @@ module NRSER
23
18
  _to_open_struct hash, freeze: freeze
24
19
  end # #to_open_struct
25
20
 
26
-
27
- private
28
-
29
- def _to_open_struct value, freeze:
30
- result = case value
31
- when OpenStruct
32
- # Just assume it's already taken care of if it's already an OpenStruct
33
- value
34
-
35
- when Hash
36
- OpenStruct.new(
37
- map_values(value) { |k, v| _to_open_struct v, freeze: freeze }
38
- )
39
-
40
- when Array
41
- value.map { |v| _to_open_struct v, freeze: freeze }
42
-
43
- when Set
44
- Set.new value.map { |v| _to_open_struct v, freeze: freeze }
21
+
22
+ def self._to_open_struct value, freeze:
23
+ result = case value
24
+ when OpenStruct
25
+ # Just assume it's already taken care of if it's already an OpenStruct
26
+ value
45
27
 
46
- else
47
- value
48
- end
28
+ when Hash
29
+ OpenStruct.new(
30
+ value.transform_values { |v| _to_open_struct v, freeze: freeze }
31
+ )
49
32
 
50
- if freeze
51
- result.freeze
52
- end
33
+ when Array
34
+ value.map { |v| _to_open_struct v, freeze: freeze }
53
35
 
54
- result
55
- end # ._to_open_struct
56
- # end private
36
+ when Set
37
+ Set.new value.map { |v| _to_open_struct v, freeze: freeze }
38
+
39
+ else
40
+ value
41
+ end
42
+
43
+ result.freeze if freeze
44
+
45
+ result
46
+ end # ._to_open_struct
57
47
 
58
- end # class < self (Eigenclass)
48
+ private_class_method :_to_open_struct
59
49
 
60
50
  end # module NRSER
@@ -41,7 +41,6 @@ module NRSER
41
41
  end # .public_sender
42
42
 
43
43
  singleton_class.send :alias_method, :sender, :public_sender
44
- singleton_class.send :alias_method, :sndr, :public_sender
45
44
 
46
45
 
47
46
  # Create a {Proc} that sends the arguments to a receiver via `#send`,
@@ -94,8 +93,6 @@ module NRSER
94
93
  }
95
94
  end # .chainer
96
95
 
97
- singleton_class.send :alias_method, :chnr, :chainer
98
-
99
96
 
100
97
  # Return a {Proc} that accepts a single argument that must respond to `#[]`
101
98
  # and retrieves `key` from it.
@@ -107,9 +104,7 @@ module NRSER
107
104
  #
108
105
  def self.retriever key
109
106
  ->( indexed ) { indexed[key] }
110
- end # .getter
111
-
112
- singleton_class.send :alias_method, :rtvr, :retriever
107
+ end # .retriever
113
108
 
114
109
 
115
110
  end # module NRSER
@@ -23,10 +23,16 @@ module NRSER
23
23
  JSON_ARRAY_RE = /\A\s*\[.*\]\s*\z/m.freeze
24
24
 
25
25
 
26
+ # Regexp used to guess if a string is a JSON-encoded object.
27
+ #
28
+ # @return [Regexp]
29
+ #
30
+ JSON_OBJECT_RE = /\A\s*\{.*\}\s*\z/m.freeze
31
+
32
+
26
33
  # Functions
27
34
  # ============================================================================
28
35
 
29
-
30
36
  # Test if a string looks like it might encode an array in JSON format by
31
37
  # seeing if it's first non-whitespace character is `[` and last
32
38
  # non-whitespace character is `]`.
@@ -41,4 +47,29 @@ module NRSER
41
47
  !!( string =~ JSON_ARRAY_RE )
42
48
  end # #looks_like_json_array
43
49
 
50
+
51
+ # Test if a string looks like it might encode an object in JSON format
52
+ # (JSON object becomes a {Hash} in Ruby) by seeing if it's first
53
+ # non-whitespace character is `{` and last non-whitespace character is `}`.
54
+ #
55
+ # @param [String] string
56
+ # String to test.
57
+ #
58
+ # @return [Boolean]
59
+ # `true` if we think `string` encodes a JSON object.
60
+ #
61
+ def self.looks_like_json_object? string
62
+ !!( string =~ JSON_OBJECT_RE )
63
+ end # .looks_like_json_object?
64
+
65
+
66
+ def self.looks_like_yaml_object? string
67
+ # YAML is (now) a super-set of JSON, so anything that looks like a JSON
68
+ # object is kosh
69
+ looks_like_json_object?( string ) || string.lines.all? { |line|
70
+ line.start_with?( '---', ' ', '#' ) || line =~ /[^\ ].*\:/
71
+ }
72
+ end
73
+
74
+
44
75
  end # module NRSER
@@ -95,45 +95,6 @@ module NRSER
95
95
 
96
96
  end # .lazy_filter_repeated_blank_lines
97
97
 
98
-
99
- # Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>:
100
- #
101
- # 'Once upon a time in a world far far away'.truncate(27)
102
- # # => "Once upon a time in a wo..."
103
- #
104
- # Pass a string or regexp <tt>:separator</tt> to truncate +text+ at a natural break:
105
- #
106
- # 'Once upon a time in a world far far away'.truncate(27, separator: ' ')
107
- # # => "Once upon a time in a..."
108
- #
109
- # 'Once upon a time in a world far far away'.truncate(27, separator: /\s/)
110
- # # => "Once upon a time in a..."
111
- #
112
- # The last characters will be replaced with the <tt>:omission</tt> string (defaults to "...")
113
- # for a total length not exceeding <tt>length</tt>:
114
- #
115
- # 'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)')
116
- # # => "And they f... (continued)"
117
- #
118
- # adapted from
119
- #
120
- # <https://github.com/rails/rails/blob/7847a19f476fb9bee287681586d872ea43785e53/activesupport/lib/active_support/core_ext/string/filters.rb#L46>
121
- #
122
- def self.truncate(str, truncate_at, options = {})
123
- return str.dup unless str.length > truncate_at
124
-
125
- omission = options[:omission] || '...'
126
- length_with_room_for_omission = truncate_at - omission.length
127
- stop = \
128
- if options[:separator]
129
- str.rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
130
- else
131
- length_with_room_for_omission
132
- end
133
-
134
- "#{str[0, stop]}#{omission}"
135
- end # .truncate
136
-
137
98
 
138
99
  # Cut the middle out of a sliceable object with length and stick an ellipsis
139
100
  # in there instead.
@@ -141,7 +102,7 @@ module NRSER
141
102
  # Categorized with {String} functions 'cause that's where it started, and
142
103
  # that's probably how it will primarily continue to be used, but tested to
143
104
  # work on {Array} and should for other classes that satisfy the same
144
- # slice and interface.
105
+ # slice and interface.
145
106
  #
146
107
  # @param [V & #length & #slice & #<< & #+] source
147
108
  # Source object. In practice, {String} and {Array} work. In theory,
@@ -44,7 +44,8 @@ module NRSER
44
44
  when Array
45
45
  text
46
46
  else
47
- raise TypeError, "Expected String or Array, found #{ text.class.name }"
47
+ raise TypeError,
48
+ "Expected String or Array, found #{ text.class.safe_name }"
48
49
  end
49
50
  end
50
51
 
@@ -7,7 +7,6 @@ require_relative './functions/proc'
7
7
  require_relative './functions/object'
8
8
  require_relative './functions/string'
9
9
  require_relative './functions/text'
10
- require_relative './functions/binding'
11
10
  require_relative './functions/exception'
12
11
  require_relative './functions/enumerable'
13
12
  require_relative './functions/hash'
@@ -0,0 +1,41 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+ # Requirements
5
+ # =======================================================================
6
+
7
+ # Stdlib
8
+ # -----------------------------------------------------------------------
9
+
10
+ # Topological sorting
11
+ require 'tsort'
12
+
13
+
14
+ # Declarations
15
+ # =======================================================================
16
+
17
+ module NRSER; end
18
+ module NRSER::Graph; end
19
+
20
+
21
+ # Definitions
22
+ # =======================================================================
23
+
24
+ # Topologically sorts an {Enumerable} by a user-provided `child_node` block.
25
+ #
26
+ class NRSER::Graph::TSorter
27
+ include TSort
28
+
29
+ def initialize entries, &each_child
30
+ @entries = entries
31
+ @each_child = each_child
32
+ end
33
+
34
+ def tsort_each_node &block
35
+ @entries.each &block
36
+ end
37
+
38
+ def tsort_each_child node, &block
39
+ @each_child.call node, &block
40
+ end
41
+ end # class NRSER::Graph::TSorter
@@ -0,0 +1,37 @@
1
+ class Binding
2
+
3
+ # Experimental "adaptive" call from local variable names.
4
+ #
5
+ def call_block *args, &block
6
+ arg_count = 0
7
+ arg_rest = false
8
+ call_kwds = {}
9
+ block.parameters.each do |type, name|
10
+ logger.debug "Processing", type: type, name: name
11
+
12
+ case type
13
+ when :req, :opt
14
+ arg_count += 1
15
+ when :keyreq, :key
16
+ if self.local_variable_defined? name
17
+ call_kwds[name] = self.local_variable_get name
18
+ end
19
+ when :rest
20
+ arg_rest = true
21
+ end
22
+ end
23
+
24
+ call_args = if arg_rest
25
+ args
26
+ else
27
+ args[0...arg_count]
28
+ end
29
+
30
+ logger.debug "CALLING WITH",
31
+ args: call_args,
32
+ kwds: call_kwds
33
+
34
+ block.call *call_args, **call_kwds
35
+ end
36
+
37
+ end # class Binding