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
@@ -1,213 +0,0 @@
1
- # encoding: UTF-8
2
- # frozen_string_literal: true
3
-
4
- using NRSER
5
-
6
- # Definitions
7
- # =======================================================================
8
-
9
- module NRSER
10
-
11
- # @!group Module Functions
12
- # ==========================================================================
13
-
14
-
15
- # @todo Document map_to_source_locations method.
16
- #
17
- # @param [type] arg_name
18
- # @todo Add name param description.
19
- #
20
- # @return [Hash<#source_location, Array<(String?, Fixnum?)>?>]
21
- # Map of objects
22
- #
23
- def self.map_to_source_locations methods
24
- methods.assoc_to &:source_location
25
- end # .map_to_source_locations
26
-
27
-
28
- # Map class method {Methods} objects to the `#source_location` for `mod`
29
- # class methods.
30
- #
31
- # @see https://ruby-doc.org/core/Method.html#method-i-source_location
32
- #
33
- # @param (see NRSER.class_method_objects_for)
34
- #
35
- # @return [Hash<Symbol, Array<(String?, Fixnum?)>?>]
36
- # Hash mapping method name {Symbol}s to results of calling their
37
- # {Method#source_location}, which seems to be able to be:
38
- #
39
- # 1. `Array<(String, Fixnum)>` - two-entry array of file path,
40
- # line number.
41
- #
42
- # 2. `nil` - if this method was not defined in Ruby (C extension, etc.)
43
- #
44
- # 3. `Array<(nil, nil)>` - Not listed as a possibility in the docs, but
45
- # I swear I've seen it, so watch out.
46
- #
47
- def self.class_method_source_locations_for *args
48
- map_to_source_locations class_method_objects_for( *args )
49
- end # .class_method_source_locations
50
-
51
-
52
- # Map *own* (not inherited) class method {Methods} objects to the
53
- # `#source_location` for `mod` class methods.
54
- #
55
- # @see https://ruby-doc.org/core/Method.html#method-i-source_location
56
- #
57
- # @param (see .own_class_method_objects_for)
58
- # @return (see .class_method_objects_for)
59
- #
60
- def self.own_class_method_source_locations_for *args
61
- map_to_source_locations own_class_method_objects_for( *args )
62
- end # .own_class_method_source_locations_for
63
-
64
-
65
- # Map instance method {Methods} objects to the `#source_location` for `mod`
66
- # class methods.
67
- #
68
- # @see https://ruby-doc.org/core/Method.html#method-i-source_location
69
- #
70
- # @param (see .instance_method_objects_for)
71
- # @return (see .class_method_source_locations_for)
72
- #
73
- def self.instance_method_source_locations_for *args
74
- map_to_source_locations instance_method_objects_for( *args )
75
- end # .class_method_source_locations
76
-
77
-
78
- # Map *own* (not inherited) class method {Methods} objects to the
79
- # `#source_location` for `mod` class methods.
80
- #
81
- # @see https://ruby-doc.org/core/Method.html#method-i-source_location
82
- #
83
- # @param (see NRSER.own_class_method_objects_for)
84
- # @return (see NRSER.class_method_objects_for)
85
- #
86
- def self.own_instance_method_source_locations_for *args
87
- map_to_source_locations own_instance_methods_objects_for( *args )
88
- end # .own_class_method_source_locations_for
89
-
90
-
91
- # @todo Document method_source_locations_for method.
92
- #
93
- # @param [type] mod
94
- # @todo Add name param description.
95
- #
96
- # @return [return_type]
97
- # @todo Document return value.
98
- #
99
- def self.method_source_locations_for mod,
100
- include_super = false,
101
- sort: true
102
- class_method_source_locations_for(
103
- mod,
104
- include_super,
105
- sort: sort,
106
- ).
107
- # map_keys { |name| ".#{ name }" }.
108
- merge instance_method_source_locations_for(
109
- mod,
110
- include_super,
111
- sort: sort,
112
- # Think it makes sense to *always* include `#initialize` since at this
113
- # point we're interested in where *all* the methods are, and that
114
- # should def be included.
115
- include_initialize: true,
116
- ) # .map_keys { |name| "##{ name }" }
117
- end # .method_source_locations_for
118
-
119
-
120
- def self.own_method_source_locations_for mod,
121
- sort: true
122
- method_source_locations_for \
123
- mod,
124
- false,
125
- sort: sort
126
- end # .method_source_locations_for
127
-
128
-
129
-
130
- # @todo Document module_source_locations method.
131
- #
132
- # @param [type] arg_name
133
- # @todo Add name param description.
134
- #
135
- # @return [return_type]
136
- # @todo Document return value.
137
- #
138
- def self.module_source_locations mod
139
- # Get all the src locs for `mod`'s methods
140
- own_method_source_locations_for( mod ).values.
141
- # Filter out any that don't have full src loc info
142
- reject { |src_loc| src_loc.nil? || src_loc.any?( &:nil? ) }
143
- end # .module_source_locations
144
-
145
-
146
-
147
- # Get a reasonable file and line for the class.
148
- #
149
- # @param [Class] cls
150
- # Class to try and locate.
151
- #
152
- # @return [Array<(String, Fixnum)>]
153
- # Two entry array; first entry is the string file path, second is the
154
- # line number.
155
- #
156
- def self.module_source_location mod
157
- # If any files end with the "canonical path" then use that. It's a path
158
- # suffix
159
- #
160
- # "my_mod/sub_mod/some_class.rb"
161
- #
162
- # the for a class
163
- #
164
- # MyMod::SubMod::SomeClass
165
- #
166
- canonical_path_end = \
167
- ActiveSupport::Inflector.underscore( mod.name ) + '.rb'
168
-
169
- # Get all the src locs for `mod`'s methods
170
- src_locs = module_source_locations mod
171
-
172
- # Find first line in canonical path (if any)
173
- canonical_path_src_loc = src_locs.
174
- find_all { |(path, line)| path.end_with? canonical_path_end }.
175
- min_by { |(path, line)| line }
176
-
177
- # If we found one, we're done!
178
- return canonical_path_src_loc if canonical_path_src_loc
179
-
180
- raise "HERE"
181
-
182
- klass.
183
- # Get an array of all instance methods, excluding inherited ones
184
- # (the `false` arg)
185
- instance_methods( false ).
186
- # Add `#initialize` since it isn't in `#instance_methods` for some
187
- # reason
188
- <<( :initialize ).
189
- # Map those to their {UnboundMethod} objects
190
- map { |sym| klass.instance_method sym }.
191
- # Toss any `nil` values (TODO how/why?)
192
- compact.
193
- # Get the source locations
194
- map( &:source_location ).
195
- # Get rid of `[nil, nil]` results, which seems to come from C exts?
196
- reject { |(path, line)| path.nil? || line.nil? }.
197
- # Get the first line in the shortest path
198
- min_by { |(path, line)| [path.length, line] }
199
-
200
- # Another approach I thought of... (untested)
201
- #
202
- # Get the path
203
- # # Get frequency of the paths
204
- # count_by { |(path, line)| path }.
205
- # # Get the one with the most occurrences
206
- # max_by { |path, count| count }.
207
- # # Get just the path (not the count)
208
- # first
209
- end # .module_source_location
210
-
211
- # @!endgroup Module Functions
212
-
213
- end # module NRSER
@@ -1,3 +0,0 @@
1
- module NRSER::Logging::Appender; end
2
-
3
- require_relative './appender/sync'
data/lib/nrser/logging.rb DELETED
@@ -1,353 +0,0 @@
1
- # encoding: UTF-8
2
- # frozen_string_literal: true
3
-
4
- # Requirements
5
- # =======================================================================
6
-
7
- # Stdlib
8
- # -----------------------------------------------------------------------
9
-
10
- # Deps
11
- # -----------------------------------------------------------------------
12
- require 'semantic_logger'
13
-
14
- # Project / Package
15
- # -----------------------------------------------------------------------
16
- require_relative './logging/formatters'
17
- require_relative './logging/appender'
18
-
19
-
20
- # Definitions
21
- # =======================================================================
22
-
23
- module NRSER
24
-
25
- # Mix in {.logger} and {#logger} to NRSER for functions to use
26
- include SemanticLogger::Loggable
27
-
28
- # Unified logging support via {SemanticLogger}.
29
- #
30
- # @see https://rocketjob.github.io/semantic_logger/index.html
31
- #
32
- module Logging
33
-
34
- # Constants
35
- # ============================================================================
36
-
37
- # Include this guy in modules and classes to add `.logger` and `#logger` methods
38
- # that point to their own named logger.
39
- #
40
- # Right now, just points to {SemanticLogger::Loggable}, but may expand on that
41
- # some time in the future, such as to add `.on`/`#on` methods like the old
42
- # `NRSER::Logger` had, etc.
43
- #
44
- # @see http://www.rubydoc.info/gems/semantic_logger/SemanticLogger/Loggable
45
- #
46
- # @return [Module]
47
- #
48
- Mixin = SemanticLogger::Loggable
49
-
50
-
51
- # Mixins
52
- # ============================================================================
53
-
54
- # Mix in {.logger} and {#logger}
55
- include Mixin
56
-
57
- extend SingleForwardable
58
-
59
-
60
- # Delegation
61
- # ============================================================================
62
-
63
- def_single_delegators(
64
- SemanticLogger,
65
- :application,
66
- :application=,
67
- :[],
68
- # NOTE These are funky due to different in SemLog's int level and Ruby
69
- # stdlib / Rails logger int levels, so omit for now.
70
- #
71
- # :index_to_level,
72
- # :level_to_index
73
- )
74
-
75
-
76
- # Module Attributes
77
- # ============================================================================
78
-
79
- @__mutex = Mutex.new
80
-
81
-
82
- # Module (Class) Methods
83
- # =====================================================================
84
-
85
- # Normalize a level name or number to a symbol, raising if it's not valid.
86
- #
87
- # Relies on Semantic Logger's "internal" {SemanticLogger.level_to_index}
88
- # method.
89
- #
90
- # @see https://github.com/rocketjob/semantic_logger/blob/97247126de32e6ecbf74cbccaa3b3732768d52c5/lib/semantic_logger/semantic_logger.rb#L454
91
- #
92
- # @param [Symbol | String | Integer]
93
- # Representation of a level in one of the following formats:
94
- #
95
- # 1. {Symbol} - verified as member of {SemanticLogger::LEVELS} and
96
- # returned.
97
- #
98
- # 2. {String} - accepts string representations of the level symbols,
99
- # case insensitive.
100
- #
101
- # 3. {Integer} - interpreted as a Ruby StdLib Logger / Rails Logger
102
- # level, which are **different** than Semantic Logger's!
103
- #
104
- # @return [:trace | :debug | :info | :warn | :error | :fatal]
105
- # Log level symbol.
106
- #
107
- # @raise
108
- # When `level` is invalid.
109
- #
110
- def self.level_sym_for level
111
- if SemanticLogger::LEVELS.include? level
112
- level
113
- else
114
- SemanticLogger.index_to_level SemanticLogger.level_to_index( level )
115
- end
116
- end
117
-
118
-
119
- # Global / default log level, which we always normalize to a symbol.
120
- #
121
- # @return [:trace | :debug | :info | :warn | :error | :fatal]
122
- #
123
- def self.level
124
- level_sym_for SemanticLogger.default_level
125
- end
126
-
127
-
128
- def self.level_index
129
- SemanticLogger.default_level_index
130
- end
131
-
132
-
133
- # Set the global default log level.
134
- #
135
- # @param level (see .level_sym_for)
136
- # @return (see .level_sym_for)
137
- # @raise (see .level_sym_for)
138
- #
139
- def self.level= level
140
- SemanticLogger.default_level = level_sym_for level
141
- end
142
-
143
-
144
- # Try to set the level, logging a warning and returning `nil` if it fails.
145
- #
146
- # @param level (see .level=)
147
- #
148
- # @return [Symbol]
149
- # The level symbol if it was set successfully.
150
- #
151
- # @return [nil]
152
- # If the set failed (also logs a warning).
153
- #
154
- def self.try_set_level level
155
- begin
156
- self.level = level
157
- rescue Exception => error
158
- logger.warn "Unable to set level, probably bad value",
159
- level: level,
160
- error: error
161
- nil
162
- end
163
- end # .try_set_level
164
-
165
-
166
- def self.level_from_ENV prefix:
167
- if NRSER.truthy? ENV["#{ prefix }_TRACE"]
168
- return :trace
169
- elsif NRSER.truthy? ENV["#{ prefix }_DEBUG"]
170
- return :debug
171
- end
172
-
173
- level = ENV["#{ prefix }_LOG_LEVEL"]
174
-
175
- unless level.nil? || level == ''
176
- return level
177
- end
178
-
179
- nil
180
- end
181
-
182
-
183
- # Setup logging.
184
- #
185
- # @param [type] arg_name
186
- # @todo Add name param description.
187
- #
188
- # @return [nil]
189
- #
190
- def self.setup! level: nil,
191
- dest: nil,
192
- sync: false,
193
- say_hi: :debug,
194
- application: 'NRSER',
195
- env_var_prefix: nil
196
-
197
- unless @__mutex.try_lock
198
- raise ThreadError, <<~END
199
- Mutex is already held.
200
-
201
- You should pretty generally NOT have multiple threads trying to
202
- setup logging at once or re-enter {NRSER::Logging.setup}!
203
- END
204
- end
205
-
206
- # Wrap around everything to make sure we release the mutex
207
- begin
208
- self.appender = dest unless dest.nil?
209
-
210
- # Force synchronous logging
211
- sync! if sync
212
-
213
- # If we didn't receive a level, check the ENV
214
- if level.nil?
215
- if env_var_prefix.nil?
216
- env_var_prefix = application.gsub( /[^a-zA-Z0-0_]+/, '_' ).upcase
217
- end
218
-
219
- level = level_from_ENV prefix: env_var_prefix
220
- end
221
-
222
- # If we ended up with a level, try to set it (will only log a warning
223
- # if it fails, not raise, which could crash things on boot)
224
- try_set_level level unless level.nil?
225
-
226
- self.application = application unless application.nil?
227
-
228
- ensure
229
- # Make sure we release the mutex; don't need to hold it for the rest
230
- @__mutex.unlock
231
- end
232
-
233
- will_say_hi = case say_hi
234
- when true, false
235
- say_hi
236
- when Symbol, String, Fixnum
237
- begin
238
- level_index < SemanticLogger.level_to_index( say_hi )
239
- rescue Exception => error
240
- logger.warn "Bad `say_hi` kwd in {NRSER::Logging.setup}",
241
- say_hi: say_hi,
242
- expected: "Symbol, String, or Fixnum representing log level",
243
- error: error
244
-
245
- false
246
- end
247
- else
248
- logger.warn "Bad `say_hi` kwd in {NRSER::Logging.setup}",
249
- say_hi: say_hi,
250
- expected: [true, false, Symbol, String, Fixnum]
251
-
252
- false
253
- end
254
-
255
- if will_say_hi
256
- logger.info "Hi! Logging is setup",
257
- level: self.level,
258
- dest: dest,
259
- sync: sync
260
- end
261
-
262
- nil
263
- rescue Exception => error
264
- # Suppress errors in favor of a warning
265
-
266
- logger.warn \
267
- message: "Error setting up logging",
268
- payload: {
269
- args: {
270
- level: level,
271
- dest: dest,
272
- env_var_prefix: env_var_prefix,
273
- say_hi: say_hi,
274
- },
275
- },
276
- exception: error
277
-
278
- nil
279
- end # .setup!
280
-
281
-
282
- # Call {.setup!} with some default keywords that are nice for CLI apps.
283
- #
284
- # @param (see .setup!)
285
- # @return (see .setup!)
286
- #
287
- def self.setup_for_cli! dest: $stderr,
288
- sync: true,
289
- **kwds
290
- setup! dest: dest, sync: sync, **kwds
291
- end # .setup_for_cli!
292
-
293
-
294
- # Hack up SemanticLogger to do sync logging in the main thread
295
- #
296
- # @return [nil]
297
- #
298
- def self.sync!
299
- # Create a {Locd::Logging::Appender::Sync}, which implements the
300
- # {SemanticLogger::Appender::Async} interface but just forwards directly
301
- # to it's appender in the same thread, and point it where
302
- # {SemanticLogger::Processor.instance} (which is an Async) points.
303
- #
304
- sync_appender = NRSER::Logging::Appender::Sync.new \
305
- appender: SemanticLogger::Processor.instance.appender
306
-
307
- # Swap our sync in for the async
308
- SemanticLogger::Processor.instance_variable_set \
309
- :@processor,
310
- sync_appender
311
-
312
- nil
313
- end
314
-
315
-
316
- # The current "main" appender (destination), if any.
317
- #
318
- # This is just to simplify things in simple cases, you can always still
319
- # add multiple appenders.
320
- #
321
- # @return [SemanticLogger::Subscriber | nil]
322
- #
323
- def self.appender
324
- @appender
325
- end
326
-
327
-
328
- def self.appender= value
329
- # Save ref to current appender (if any) so we can remove it after adding
330
- # the new one.
331
- old_appender = @appender
332
-
333
- @appender = case value
334
- when Hash
335
- SemanticLogger.add_appender value
336
- when String, Pathname
337
- SemanticLogger.add_appender file_name: value.to_s
338
- else
339
- SemanticLogger.add_appender \
340
- io: value,
341
- formatter: NRSER::Logging::Formatters::Color.new
342
- end
343
-
344
- # Remove the old appender (if there was one). This is done after adding
345
- # the new one so that failing won't result with no appenders.
346
- SemanticLogger.remove_appender( old_appender ) if old_appender
347
-
348
- @appender
349
- end
350
-
351
-
352
- end # module Logging
353
- end # module NRSER
@@ -1,31 +0,0 @@
1
- # Refinements
2
- # =======================================================================
3
-
4
- using NRSER
5
-
6
-
7
- # Definitions
8
- # =======================================================================
9
-
10
- class NRSER::Meta::Props::Base
11
- include NRSER::Meta::Props
12
-
13
- def initialize **values
14
- initialize_props values
15
- end
16
-
17
- # @todo Prob wanna improve this at some point, but it's better than nothing.
18
- #
19
- # @return [String]
20
- # a short string describing the instance.
21
- #
22
- def to_s
23
- props_str = self.class.props( only_primary: true ).sort.map { |name, prop|
24
- "#{ name }=#{ prop.get( self ).inspect }"
25
- }.join ' '
26
-
27
- <<-END.squish
28
- #<#{ self.class.name } #{ props_str }>
29
- END
30
- end # #to_s
31
- end # class NRSER::Meta::Props::Base