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.
- checksums.yaml +4 -4
- data/lib/nrser/char/alpha_numeric_sub.rb +1 -2
- data/lib/nrser/char.rb +0 -6
- data/lib/nrser/core_ext/array.rb +120 -0
- data/lib/nrser/core_ext/binding.rb +44 -0
- data/lib/nrser/{functions → core_ext}/enumerable/find_map.rb +18 -15
- data/lib/nrser/{ext → core_ext}/enumerable.rb +10 -24
- data/lib/nrser/core_ext/exception.rb +30 -0
- data/lib/nrser/core_ext/hash/extract_values_at.rb +49 -0
- data/lib/nrser/core_ext/hash/transform_values_with_keys.rb +24 -0
- data/lib/nrser/core_ext/hash.rb +50 -0
- data/lib/nrser/core_ext/module/method_objects.rb +96 -0
- data/lib/nrser/core_ext/module/names.rb +69 -0
- data/lib/nrser/core_ext/module/source_locations.rb +214 -0
- data/lib/nrser/core_ext/module.rb +2 -0
- data/lib/nrser/core_ext/object/lazy_var.rb +31 -0
- data/lib/nrser/core_ext/object.rb +46 -0
- data/lib/nrser/core_ext/open_struct.rb +6 -0
- data/lib/nrser/{ext → core_ext}/pathname.rb +8 -5
- data/lib/nrser/{ext → core_ext}/string.rb +6 -12
- data/lib/nrser/core_ext/symbol.rb +13 -0
- data/lib/nrser/core_ext/time.rb +46 -0
- data/lib/nrser/core_ext.rb +13 -0
- data/lib/nrser/errors/abstract_method_error.rb +150 -0
- data/lib/nrser/errors/argument_error.rb +42 -0
- data/lib/nrser/errors/nicer_error.rb +298 -72
- data/lib/nrser/errors/type_error.rb +46 -0
- data/lib/nrser/errors.rb +4 -53
- data/lib/nrser/ext/tree.rb +3 -0
- data/lib/nrser/functions/enumerable/associate.rb +6 -9
- data/lib/nrser/functions/enumerable/include_slice.rb +2 -3
- data/lib/nrser/functions/enumerable.rb +1 -3
- data/lib/nrser/functions/exception.rb +1 -1
- data/lib/nrser/functions/hash.rb +0 -6
- data/lib/nrser/functions/merge_by.rb +2 -2
- data/lib/nrser/functions/module/method_objects.rb +77 -0
- data/lib/nrser/functions/module.rb +1 -2
- data/lib/nrser/functions/open_struct.rb +25 -35
- data/lib/nrser/functions/proc.rb +1 -6
- data/lib/nrser/functions/string/looks_like.rb +32 -1
- data/lib/nrser/functions/string.rb +1 -40
- data/lib/nrser/functions/text/lines.rb +2 -1
- data/lib/nrser/functions.rb +0 -1
- data/lib/nrser/graph/tsorter.rb +41 -0
- data/lib/nrser/labs/core_ext/binding.rb +37 -0
- data/lib/nrser/labs/stash.rb +372 -0
- data/lib/nrser/{logging → log}/appender/sync.rb +3 -3
- data/lib/nrser/log/appender.rb +3 -0
- data/lib/nrser/{logging → log}/formatters/color.rb +47 -20
- data/lib/nrser/log/formatters/mixin.rb +270 -0
- data/lib/nrser/{logging → log}/formatters.rb +0 -0
- data/lib/nrser/log/logger.rb +229 -0
- data/lib/nrser/log/mixin.rb +56 -0
- data/lib/nrser/log.rb +723 -0
- data/lib/nrser/message.rb +24 -3
- data/lib/nrser/meta/source/location.rb +158 -0
- data/lib/nrser/meta.rb +1 -1
- data/lib/nrser/props/class_methods.rb +118 -0
- data/lib/nrser/props/immutable/hash.rb +111 -0
- data/lib/nrser/props/immutable/hash_variable.rb +82 -0
- data/lib/nrser/props/immutable/instance_variables.rb +48 -0
- data/lib/nrser/props/immutable/vector.rb +107 -0
- data/lib/nrser/props/instance_methods.rb +184 -0
- data/lib/nrser/props/metadata.rb +359 -0
- data/lib/nrser/props/mutable/instance_variables.rb +60 -0
- data/lib/nrser/props/mutable/stash.rb +199 -0
- data/lib/nrser/{meta/props → props}/prop.rb +217 -112
- data/lib/nrser/props/storage/instance_variable.rb +85 -0
- data/lib/nrser/props/storage/instance_variables.rb +67 -0
- data/lib/nrser/props/storage/key.rb +88 -0
- data/lib/nrser/props.rb +9 -0
- data/lib/nrser/refinements/sugar.rb +41 -0
- data/lib/nrser/refinements/types.rb +2 -2
- data/lib/nrser/refinements.rb +14 -16
- data/lib/nrser/rspex/example_group/describe_attribute.rb +24 -0
- data/lib/nrser/rspex/example_group/describe_called_with.rb +1 -6
- data/lib/nrser/rspex/example_group/{describe_use_case.rb → describe_case.rb} +6 -3
- data/lib/nrser/rspex/example_group/describe_class.rb +1 -0
- data/lib/nrser/rspex/example_group/describe_group.rb +29 -0
- data/lib/nrser/rspex/example_group/describe_instance_method.rb +2 -2
- data/lib/nrser/rspex/example_group/describe_message.rb +35 -0
- data/lib/nrser/rspex/example_group/describe_method.rb +23 -2
- data/lib/nrser/rspex/example_group/describe_module.rb +19 -0
- data/lib/nrser/rspex/example_group/describe_response_to.rb +32 -0
- data/lib/nrser/rspex/example_group/describe_section.rb +38 -0
- data/lib/nrser/rspex/example_group/describe_sent_to.rb +52 -0
- data/lib/nrser/rspex/example_group/describe_source_file.rb +49 -0
- data/lib/nrser/rspex/example_group/describe_spec_file.rb +41 -108
- data/lib/nrser/rspex/example_group/describe_when.rb +14 -7
- data/lib/nrser/rspex/example_group/describe_x.rb +39 -12
- data/lib/nrser/rspex/example_group/overrides.rb +66 -0
- data/lib/nrser/rspex/example_group.rb +20 -252
- data/lib/nrser/rspex/format.rb +83 -17
- data/lib/nrser/rspex.rb +4 -34
- data/lib/nrser/sugar/method_missing_forwarder.rb +50 -0
- data/lib/nrser/{env → sys/env}/path.rb +1 -2
- data/lib/nrser/{env.rb → sys/env.rb} +2 -1
- data/lib/nrser/sys.rb +5 -0
- data/lib/nrser/types/any.rb +36 -7
- data/lib/nrser/types/{array.rb → arrays.rb} +32 -81
- data/lib/nrser/types/attrs.rb +68 -15
- data/lib/nrser/types/booleans.rb +95 -34
- data/lib/nrser/types/bounded.rb +12 -10
- data/lib/nrser/types/combinators.rb +74 -37
- data/lib/nrser/types/errors/check_error.rb +86 -0
- data/lib/nrser/types/errors/from_string_error.rb +82 -0
- data/lib/nrser/types/factory.rb +91 -0
- data/lib/nrser/types/hashes.rb +171 -26
- data/lib/nrser/types/in.rb +25 -12
- data/lib/nrser/types/is.rb +50 -18
- data/lib/nrser/types/is_a.rb +52 -33
- data/lib/nrser/types/labels.rb +6 -33
- data/lib/nrser/types/maybe.rb +12 -4
- data/lib/nrser/types/nil.rb +24 -4
- data/lib/nrser/types/not.rb +6 -16
- data/lib/nrser/types/numbers.rb +94 -57
- data/lib/nrser/types/pairs.rb +57 -57
- data/lib/nrser/types/paths.rb +112 -133
- data/lib/nrser/types/responds.rb +64 -74
- data/lib/nrser/types/shape.rb +29 -24
- data/lib/nrser/types/strings.rb +25 -17
- data/lib/nrser/types/symbols.rb +19 -17
- data/lib/nrser/types/trees.rb +18 -70
- data/lib/nrser/types/tuples.rb +36 -40
- data/lib/nrser/types/type.rb +342 -91
- data/lib/nrser/types/when.rb +40 -18
- data/lib/nrser/types/where.rb +94 -9
- data/lib/nrser/types.rb +72 -63
- data/lib/nrser/version.rb +1 -1
- data/lib/nrser.rb +18 -18
- data/spec/lib/nrser/{functions/binding/template_spec.rb → core_ext/binding/erb_spec.rb} +5 -5
- data/spec/lib/nrser/{functions → core_ext}/enumerable/find_map_spec.rb +8 -6
- data/spec/lib/nrser/{refinements → core_ext}/hash_spec.rb +9 -22
- data/spec/lib/nrser/errors/abstract_method_error_spec.rb +12 -5
- data/spec/lib/nrser/functions/enumerable/{to_h_by_spec.rb → associate_spec.rb} +1 -1
- data/spec/lib/nrser/functions/merge_by_spec.rb +1 -1
- data/spec/lib/nrser/functions/tree/each_branch_spec.rb +3 -3
- data/spec/lib/nrser/functions/tree/transform_spec.rb +14 -15
- data/spec/lib/nrser/gem_ext/hamster/json_spec.rb +4 -0
- data/spec/lib/nrser/meta/source/location_spec.rb +86 -0
- data/spec/lib/nrser/props/immutable/hash_spec.rb +297 -0
- data/spec/lib/nrser/props/immutable/vector_spec.rb +296 -0
- data/spec/lib/nrser/{meta/props_spec.rb → props/original_props_spec.rb} +11 -16
- data/spec/lib/nrser/{meta/props → props}/to_and_from_data_spec.rb +10 -8
- data/spec/lib/nrser/refinements/array_spec.rb +2 -15
- data/spec/lib/nrser/refinements/erb_spec.rb +5 -7
- data/spec/lib/nrser/refinements/set_spec.rb +2 -15
- data/spec/lib/nrser/{env → sys/env}/path/insert_spec.rb +4 -2
- data/spec/lib/nrser/{env → sys/env}/path_spec.rb +4 -2
- data/spec/lib/nrser/types/array_spec.rb +8 -8
- data/spec/lib/nrser/types/paths_spec.rb +15 -18
- data/spec/spec_helper.rb +4 -0
- metadata +109 -69
- data/lib/nrser/ext/binding.rb +0 -36
- data/lib/nrser/ext/module.rb +0 -62
- data/lib/nrser/ext.rb +0 -8
- data/lib/nrser/functions/binding.rb +0 -76
- data/lib/nrser/functions/enumerable/map_keys.rb +0 -0
- data/lib/nrser/functions/enumerable/map_values.rb +0 -94
- data/lib/nrser/functions/hash/deep_merge.rb +0 -57
- data/lib/nrser/functions/hash/except_keys.rb +0 -44
- data/lib/nrser/functions/hash/slice_keys.rb +0 -43
- data/lib/nrser/functions/hash/stringify_keys.rb +0 -55
- data/lib/nrser/functions/hash/symbolize_keys.rb +0 -57
- data/lib/nrser/functions/hash/transform_keys.rb +0 -140
- data/lib/nrser/functions/module/methods.rb +0 -206
- data/lib/nrser/functions/module/source_locations.rb +0 -213
- data/lib/nrser/logging/appender.rb +0 -3
- data/lib/nrser/logging.rb +0 -353
- data/lib/nrser/meta/props/base.rb +0 -31
- data/lib/nrser/meta/props.rb +0 -357
- data/lib/nrser/refinements/array.rb +0 -133
- data/lib/nrser/refinements/binding.rb +0 -6
- data/lib/nrser/refinements/enumerator.rb +0 -5
- data/lib/nrser/refinements/exception.rb +0 -35
- data/lib/nrser/refinements/hash.rb +0 -150
- data/lib/nrser/refinements/module.rb +0 -5
- data/lib/nrser/refinements/object.rb +0 -42
- data/lib/nrser/refinements/open_struct.rb +0 -28
- data/lib/nrser/refinements/pathname.rb +0 -5
- data/lib/nrser/refinements/set.rb +0 -5
- data/lib/nrser/refinements/string.rb +0 -5
- data/lib/nrser/refinements/symbol.rb +0 -20
- data/lib/nrser/rspex/described.rb +0 -99
- data/spec/design/mapping_spec.rb +0 -42
- data/spec/lib/nrser/functions/hash_spec.rb +0 -41
- data/spec/lib/nrser/functions/string/truncate_spec.rb +0 -11
- data/spec/lib/nrser/refinements/truncate_spec.rb +0 -10
@@ -0,0 +1,372 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
# Requirements
|
5
|
+
# =======================================================================
|
6
|
+
|
7
|
+
# Deps
|
8
|
+
# -----------------------------------------------------------------------
|
9
|
+
|
10
|
+
require "active_support/core_ext/hash/keys"
|
11
|
+
require "active_support/core_ext/hash/reverse_merge"
|
12
|
+
|
13
|
+
|
14
|
+
# Definitions
|
15
|
+
# ============================================================================
|
16
|
+
|
17
|
+
# Abstract generalization of {ActiveSupport::HashWithIndifferentAccess}.
|
18
|
+
# Extends {Hash} and provides simple hooks for handling keys and values
|
19
|
+
# on write.
|
20
|
+
#
|
21
|
+
#
|
22
|
+
class NRSER::Stash < ::Hash
|
23
|
+
|
24
|
+
# Construction
|
25
|
+
# ==========================================================================
|
26
|
+
|
27
|
+
def initialize(constructor = {})
|
28
|
+
if constructor.respond_to?(:to_hash)
|
29
|
+
super()
|
30
|
+
update(constructor)
|
31
|
+
|
32
|
+
hash = constructor.to_hash
|
33
|
+
self.default = hash.default if hash.default
|
34
|
+
self.default_proc = hash.default_proc if hash.default_proc
|
35
|
+
else
|
36
|
+
super(constructor)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
# Instance Methods
|
42
|
+
# ==========================================================================
|
43
|
+
|
44
|
+
# @!group Internal Interface
|
45
|
+
# --------------------------------------------------------------------------
|
46
|
+
|
47
|
+
# Save {Hash#[]=} as {#_raw_put} for directly writing keys and values.
|
48
|
+
#
|
49
|
+
# @protected
|
50
|
+
#
|
51
|
+
alias_method :_raw_put, :[]=
|
52
|
+
protected :_raw_put
|
53
|
+
|
54
|
+
|
55
|
+
# Save {Hash#update} as {#_raw_update} for directly writing many key/value
|
56
|
+
# pairs.
|
57
|
+
#
|
58
|
+
# @protected
|
59
|
+
#
|
60
|
+
# @note
|
61
|
+
# This is a hold-over from {HashWithIndifferentAccess}; not currently in
|
62
|
+
# use. Note sure if it will be kept around.
|
63
|
+
#
|
64
|
+
alias_method :_raw_update, :update
|
65
|
+
protected :_raw_update
|
66
|
+
|
67
|
+
|
68
|
+
# Save {Hash#key?} direct querying of key presence.
|
69
|
+
#
|
70
|
+
# @protected
|
71
|
+
#
|
72
|
+
alias_method :_raw_key?, :key?
|
73
|
+
protected :_raw_key?
|
74
|
+
|
75
|
+
|
76
|
+
protected
|
77
|
+
# ========================================================================
|
78
|
+
|
79
|
+
# A wrapper for {#_raw_put} that converts keys and values first.
|
80
|
+
#
|
81
|
+
# @todo
|
82
|
+
# Note sure if I want to keep this yet...
|
83
|
+
#
|
84
|
+
def _convert_and_put key, value
|
85
|
+
_raw_put convert_key(key), convert_value(value, for: :assignment)
|
86
|
+
end
|
87
|
+
|
88
|
+
public # end protected ***************************************************
|
89
|
+
|
90
|
+
|
91
|
+
# @!endgroup Internal Interface # ******************************************
|
92
|
+
|
93
|
+
|
94
|
+
# @!group The Stuff You Care About
|
95
|
+
# --------------------------------------------------------------------------
|
96
|
+
#
|
97
|
+
# Most of the class is overriding {::Hash} methods to hook into these
|
98
|
+
# methods correctly... they are what you most likely care about.
|
99
|
+
#
|
100
|
+
|
101
|
+
# Convert an external key to the internal representation.
|
102
|
+
#
|
103
|
+
# @param [*] key
|
104
|
+
# The externally provided key.
|
105
|
+
#
|
106
|
+
# @return [*]
|
107
|
+
# The key to use internally.
|
108
|
+
#
|
109
|
+
def convert_key key
|
110
|
+
key
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
def convert_value value, options = {}
|
115
|
+
value
|
116
|
+
end
|
117
|
+
|
118
|
+
|
119
|
+
def put key, value
|
120
|
+
_convert_and_put key, value
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
def []= key, value
|
125
|
+
put key, value
|
126
|
+
end
|
127
|
+
|
128
|
+
# @!endgroup The Stuff You Care About # ************************************
|
129
|
+
|
130
|
+
|
131
|
+
alias_method :store, :[]=
|
132
|
+
|
133
|
+
|
134
|
+
def set_defaults(target)
|
135
|
+
if default_proc
|
136
|
+
target.default_proc = default_proc.dup
|
137
|
+
else
|
138
|
+
target.default = default
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
# Returns +true+ so that <tt>Array#extract_options!</tt> finds members of
|
144
|
+
# this class.
|
145
|
+
def extractable_options?
|
146
|
+
true
|
147
|
+
end
|
148
|
+
|
149
|
+
|
150
|
+
def self.[] *args
|
151
|
+
new.merge! ::Hash[*args]
|
152
|
+
end
|
153
|
+
|
154
|
+
|
155
|
+
# Updates the receiver in-place, merging in the hash passed as argument:
|
156
|
+
#
|
157
|
+
# hash_1 = ActiveSupport::HashWithIndifferentAccess.new
|
158
|
+
# hash_1[:key] = 'value'
|
159
|
+
#
|
160
|
+
# hash_2 = ActiveSupport::HashWithIndifferentAccess.new
|
161
|
+
# hash_2[:key] = 'New Value!'
|
162
|
+
#
|
163
|
+
# hash_1.update(hash_2) # => {"key"=>"New Value!"}
|
164
|
+
#
|
165
|
+
# The argument can be either an
|
166
|
+
# <tt>ActiveSupport::HashWithIndifferentAccess</tt> or a regular +Hash+.
|
167
|
+
# In either case the merge respects the semantics of indifferent access.
|
168
|
+
#
|
169
|
+
# If the argument is a regular hash with keys +:key+ and +"key"+ only one
|
170
|
+
# of the values end up in the receiver, but which one is unspecified.
|
171
|
+
#
|
172
|
+
# When given a block, the value for duplicated keys will be determined
|
173
|
+
# by the result of invoking the block with the duplicated key, the value
|
174
|
+
# in the receiver, and the value in +other_hash+. The rules for duplicated
|
175
|
+
# keys follow the semantics of indifferent access:
|
176
|
+
#
|
177
|
+
# hash_1[:key] = 10
|
178
|
+
# hash_2['key'] = 12
|
179
|
+
# hash_1.update(hash_2) { |key, old, new| old + new } # => {"key"=>22}
|
180
|
+
#
|
181
|
+
# @param [Proc<(KEY, CURRENT, UPDATE) => VALUE>] &block
|
182
|
+
# Optional block to handle key conflicts.
|
183
|
+
#
|
184
|
+
# @return [self]
|
185
|
+
#
|
186
|
+
def update other_hash, &block
|
187
|
+
other_hash.to_hash.each_pair do |key, value|
|
188
|
+
key = convert_key key
|
189
|
+
if block && _raw_key?( key )
|
190
|
+
value = yield key, _raw_get( key ), value
|
191
|
+
end
|
192
|
+
put key, value
|
193
|
+
end
|
194
|
+
self
|
195
|
+
end
|
196
|
+
|
197
|
+
alias_method :merge!, :update
|
198
|
+
|
199
|
+
|
200
|
+
# Checks the hash for a key matching the argument passed in:
|
201
|
+
#
|
202
|
+
# hash = ActiveSupport::HashWithIndifferentAccess.new
|
203
|
+
# hash['key'] = 'value'
|
204
|
+
# hash.key?(:key) # => true
|
205
|
+
# hash.key?('key') # => true
|
206
|
+
def key? key
|
207
|
+
_raw_key? convert_key( key )
|
208
|
+
end
|
209
|
+
|
210
|
+
alias_method :include?, :key?
|
211
|
+
alias_method :has_key?, :key?
|
212
|
+
alias_method :member?, :key?
|
213
|
+
|
214
|
+
|
215
|
+
alias_method :_raw_get, :[]
|
216
|
+
|
217
|
+
# Same as <tt>Hash#[]</tt> where the key passed as argument can be
|
218
|
+
# either a string or a symbol:
|
219
|
+
#
|
220
|
+
# counters = ActiveSupport::HashWithIndifferentAccess.new
|
221
|
+
# counters[:foo] = 1
|
222
|
+
#
|
223
|
+
# counters['foo'] # => 1
|
224
|
+
# counters[:foo] # => 1
|
225
|
+
# counters[:zoo] # => nil
|
226
|
+
def [] key
|
227
|
+
_raw_get convert_key( key )
|
228
|
+
end
|
229
|
+
|
230
|
+
# Same as <tt>Hash#fetch</tt> where the key passed as argument can be
|
231
|
+
# either a string or a symbol:
|
232
|
+
#
|
233
|
+
# counters = ActiveSupport::HashWithIndifferentAccess.new
|
234
|
+
# counters[:foo] = 1
|
235
|
+
#
|
236
|
+
# counters.fetch('foo') # => 1
|
237
|
+
# counters.fetch(:bar, 0) # => 0
|
238
|
+
# counters.fetch(:bar) { |key| 0 } # => 0
|
239
|
+
# counters.fetch(:zoo) # => KeyError: key not found: "zoo"
|
240
|
+
def fetch key, *extras
|
241
|
+
super convert_key(key), *extras
|
242
|
+
end
|
243
|
+
|
244
|
+
# Same as <tt>Hash#dig</tt> where the key passed as argument can be
|
245
|
+
# either a string or a symbol:
|
246
|
+
#
|
247
|
+
# counters = ActiveSupport::HashWithIndifferentAccess.new
|
248
|
+
# counters[:foo] = { bar: 1 }
|
249
|
+
#
|
250
|
+
# counters.dig('foo', 'bar') # => 1
|
251
|
+
# counters.dig(:foo, :bar) # => 1
|
252
|
+
# counters.dig(:zoo) # => nil
|
253
|
+
def dig *args
|
254
|
+
args[0] = convert_key( args[0] ) if args.size > 0
|
255
|
+
super *args
|
256
|
+
end
|
257
|
+
|
258
|
+
|
259
|
+
# Same as <tt>Hash#default</tt> where the key passed as argument can be
|
260
|
+
# either a string or a symbol:
|
261
|
+
#
|
262
|
+
# hash = ActiveSupport::HashWithIndifferentAccess.new(1)
|
263
|
+
# hash.default # => 1
|
264
|
+
#
|
265
|
+
# hash = ActiveSupport::HashWithIndifferentAccess.new { |hash, key| key }
|
266
|
+
# hash.default # => nil
|
267
|
+
# hash.default('foo') # => 'foo'
|
268
|
+
# hash.default(:foo) # => 'foo'
|
269
|
+
def default(*args)
|
270
|
+
super(*args.map { |arg| convert_key(arg) })
|
271
|
+
end
|
272
|
+
|
273
|
+
# Returns an array of the values at the specified indices:
|
274
|
+
#
|
275
|
+
# hash = ActiveSupport::HashWithIndifferentAccess.new
|
276
|
+
# hash[:a] = 'x'
|
277
|
+
# hash[:b] = 'y'
|
278
|
+
# hash.values_at('a', 'b') # => ["x", "y"]
|
279
|
+
def values_at(*indices)
|
280
|
+
indices.collect { |key| self[convert_key(key)] }
|
281
|
+
end
|
282
|
+
|
283
|
+
# Returns a shallow copy of the hash.
|
284
|
+
#
|
285
|
+
# hash = ActiveSupport::HashWithIndifferentAccess.new({ a: { b: 'b' } })
|
286
|
+
# dup = hash.dup
|
287
|
+
# dup[:a][:c] = 'c'
|
288
|
+
#
|
289
|
+
# hash[:a][:c] # => "c"
|
290
|
+
# dup[:a][:c] # => "c"
|
291
|
+
def dup
|
292
|
+
self.class.new(self).tap do |new_hash|
|
293
|
+
set_defaults(new_hash)
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
# This method has the same semantics of +update+, except it does not
|
298
|
+
# modify the receiver but rather returns a new hash with indifferent
|
299
|
+
# access with the result of the merge.
|
300
|
+
def merge(hash, &block)
|
301
|
+
dup.update(hash, &block)
|
302
|
+
end
|
303
|
+
|
304
|
+
# Like +merge+ but the other way around: Merges the receiver into the
|
305
|
+
# argument and returns a new hash with indifferent access as result:
|
306
|
+
#
|
307
|
+
# hash = ActiveSupport::HashWithIndifferentAccess.new
|
308
|
+
# hash['a'] = nil
|
309
|
+
# hash.reverse_merge(a: 0, b: 1) # => {"a"=>nil, "b"=>1}
|
310
|
+
def reverse_merge(other_hash)
|
311
|
+
super(self.class.new(other_hash))
|
312
|
+
end
|
313
|
+
|
314
|
+
# Same semantics as +reverse_merge+ but modifies the receiver in-place.
|
315
|
+
def reverse_merge!(other_hash)
|
316
|
+
replace(reverse_merge(other_hash))
|
317
|
+
end
|
318
|
+
|
319
|
+
# Replaces the contents of this hash with other_hash.
|
320
|
+
#
|
321
|
+
# h = { "a" => 100, "b" => 200 }
|
322
|
+
# h.replace({ "c" => 300, "d" => 400 }) # => {"c"=>300, "d"=>400}
|
323
|
+
def replace(other_hash)
|
324
|
+
super(self.class.new(other_hash))
|
325
|
+
end
|
326
|
+
|
327
|
+
# Removes the specified key from the hash.
|
328
|
+
def delete(key)
|
329
|
+
super(convert_key(key))
|
330
|
+
end
|
331
|
+
|
332
|
+
# def stringify_keys!; self end
|
333
|
+
# def deep_stringify_keys!; self end
|
334
|
+
# def stringify_keys; dup end
|
335
|
+
# def deep_stringify_keys; dup end
|
336
|
+
# undef :symbolize_keys!
|
337
|
+
# undef :deep_symbolize_keys!
|
338
|
+
# def symbolize_keys; to_hash.symbolize_keys! end
|
339
|
+
# def deep_symbolize_keys; to_hash.deep_symbolize_keys! end
|
340
|
+
# def to_options!; self end
|
341
|
+
|
342
|
+
def select(*args, &block)
|
343
|
+
return to_enum(:select) unless block_given?
|
344
|
+
dup.tap { |hash| hash.select!(*args, &block) }
|
345
|
+
end
|
346
|
+
|
347
|
+
def reject(*args, &block)
|
348
|
+
return to_enum(:reject) unless block_given?
|
349
|
+
dup.tap { |hash| hash.reject!(*args, &block) }
|
350
|
+
end
|
351
|
+
|
352
|
+
def transform_values(*args, &block)
|
353
|
+
return to_enum(:transform_values) unless block_given?
|
354
|
+
dup.tap { |hash| hash.transform_values!(*args, &block) }
|
355
|
+
end
|
356
|
+
|
357
|
+
def compact
|
358
|
+
dup.tap(&:compact!)
|
359
|
+
end
|
360
|
+
|
361
|
+
# Convert to a regular hash with string keys.
|
362
|
+
def to_hash
|
363
|
+
_new_hash = ::Hash.new
|
364
|
+
set_defaults(_new_hash)
|
365
|
+
|
366
|
+
each do |key, value|
|
367
|
+
_new_hash[key] = convert_value(value, for: :to_hash)
|
368
|
+
end
|
369
|
+
_new_hash
|
370
|
+
end
|
371
|
+
|
372
|
+
end
|
@@ -16,7 +16,7 @@
|
|
16
16
|
# Useful for CLI applications where you want to see output in sync with
|
17
17
|
# operations.
|
18
18
|
#
|
19
|
-
class NRSER::
|
19
|
+
class NRSER::Log::Appender::Sync
|
20
20
|
|
21
21
|
# Mixins
|
22
22
|
# ============================================================================
|
@@ -80,7 +80,7 @@ class NRSER::Logging::Appender::Sync
|
|
80
80
|
# lag_check_interval: [Integer]
|
81
81
|
# Number of messages to process before checking for slow logging.
|
82
82
|
# Default: 1,000
|
83
|
-
def initialize appender:, name: appender.class.
|
83
|
+
def initialize appender:, name: appender.class.safe_name
|
84
84
|
@appender = appender
|
85
85
|
end
|
86
86
|
|
@@ -145,4 +145,4 @@ class NRSER::Logging::Appender::Sync
|
|
145
145
|
# Sync appender is always active
|
146
146
|
def active?; true; end
|
147
147
|
|
148
|
-
end # class NRSER::
|
148
|
+
end # class NRSER::Log::Appender::Sync
|
@@ -12,6 +12,7 @@ require 'semantic_logger'
|
|
12
12
|
# Project / Package
|
13
13
|
# -----------------------------------------------------------------------
|
14
14
|
|
15
|
+
require_relative './mixin'
|
15
16
|
|
16
17
|
# Refinements
|
17
18
|
# =======================================================================
|
@@ -20,14 +21,14 @@ require 'semantic_logger'
|
|
20
21
|
# Declarations
|
21
22
|
# =======================================================================
|
22
23
|
|
23
|
-
module NRSER::
|
24
|
-
module NRSER::
|
24
|
+
module NRSER::Log; end
|
25
|
+
module NRSER::Log::Formatters; end
|
25
26
|
|
26
27
|
|
27
28
|
# Definitions
|
28
29
|
# =======================================================================
|
29
30
|
|
30
|
-
class NRSER::
|
31
|
+
class NRSER::Log::Formatters::Color < ::SemanticLogger::Formatters::Color
|
31
32
|
|
32
33
|
# Constants
|
33
34
|
# ======================================================================
|
@@ -39,6 +40,12 @@ class NRSER::Logging::Formatters::Color < ::SemanticLogger::Formatters::Color
|
|
39
40
|
ANSI_ESC_DARK_GRAY = "\e[1;30m"
|
40
41
|
|
41
42
|
|
43
|
+
# Mixins
|
44
|
+
# ========================================================================
|
45
|
+
|
46
|
+
include NRSER::Log::Formatters::Mixin
|
47
|
+
|
48
|
+
|
42
49
|
# Class Methods
|
43
50
|
# ======================================================================
|
44
51
|
|
@@ -53,10 +60,36 @@ class NRSER::Logging::Formatters::Color < ::SemanticLogger::Formatters::Color
|
|
53
60
|
SemanticLogger::Formatters::Color::ColorMap.new(
|
54
61
|
debug: SemanticLogger::AnsiColors::MAGENTA,
|
55
62
|
trace: ANSI_ESC_DARK_GRAY,
|
63
|
+
warn: SemanticLogger::AnsiColors::YELLOW,
|
56
64
|
)
|
57
65
|
end # .default_color_map
|
58
66
|
|
59
67
|
|
68
|
+
# A **HACK** to get an {AwesomePrint::Formatter} to
|
69
|
+
# {AwesomePrint::Formatter#colorize} with by constructing a new
|
70
|
+
# {AwesomePrint::Inspector} and fishing it out of the `@formatter`
|
71
|
+
# instance variable.
|
72
|
+
#
|
73
|
+
# This is only done once and the value is cached in a class variable.
|
74
|
+
#
|
75
|
+
# @see .colorize
|
76
|
+
#
|
77
|
+
# @private
|
78
|
+
# @return [AwesomePrint::Formatter]
|
79
|
+
#
|
80
|
+
def self.ap_formatter
|
81
|
+
@@ap_formatter ||= \
|
82
|
+
AwesomePrint::Inspector.new.instance_variable_get :@formatter
|
83
|
+
end
|
84
|
+
|
85
|
+
private_class_method :ap_formatter
|
86
|
+
|
87
|
+
|
88
|
+
def self.colorize string, type
|
89
|
+
ap_formatter.colorize string, type
|
90
|
+
end
|
91
|
+
|
92
|
+
|
60
93
|
# Attributes
|
61
94
|
# ======================================================================
|
62
95
|
|
@@ -65,7 +98,7 @@ class NRSER::Logging::Formatters::Color < ::SemanticLogger::Formatters::Color
|
|
65
98
|
# ======================================================================
|
66
99
|
|
67
100
|
# Instantiate a new `ColorFormatter`.
|
68
|
-
def initialize ap: {multiline: true},
|
101
|
+
def initialize ap: {multiline: true, raw: true},
|
69
102
|
color_map: self.class.default_color_map,
|
70
103
|
time_format: ::SemanticLogger::Formatters::Base::TIME_FORMAT,
|
71
104
|
log_host: false,
|
@@ -81,6 +114,10 @@ class NRSER::Logging::Formatters::Color < ::SemanticLogger::Formatters::Color
|
|
81
114
|
# Instance Methods
|
82
115
|
# ======================================================================
|
83
116
|
|
117
|
+
def name
|
118
|
+
self.class.colorize log.name, :class
|
119
|
+
end
|
120
|
+
|
84
121
|
|
85
122
|
# Upcase the log level.
|
86
123
|
#
|
@@ -91,6 +128,11 @@ class NRSER::Logging::Formatters::Color < ::SemanticLogger::Formatters::Color
|
|
91
128
|
end
|
92
129
|
|
93
130
|
|
131
|
+
def time
|
132
|
+
"#{ color }#{ super() }#{ color_map.clear }"
|
133
|
+
end
|
134
|
+
|
135
|
+
|
94
136
|
# Create the log entry text. Overridden to customize appearance -
|
95
137
|
# generally reduce amount of info and put payload on it's own line.
|
96
138
|
#
|
@@ -142,22 +184,7 @@ class NRSER::Logging::Formatters::Color < ::SemanticLogger::Formatters::Color
|
|
142
184
|
self.log = log
|
143
185
|
self.logger = logger
|
144
186
|
|
145
|
-
|
146
|
-
time, # annoyingly noisy and don't really need for local CLI app
|
147
|
-
level,
|
148
|
-
process_info,
|
149
|
-
tags,
|
150
|
-
named_tags,
|
151
|
-
duration,
|
152
|
-
name,
|
153
|
-
].compact.join( ' ' ) +
|
154
|
-
"\n" +
|
155
|
-
[
|
156
|
-
message,
|
157
|
-
payload,
|
158
|
-
exception,
|
159
|
-
].compact.join(' ') +
|
160
|
-
"\n" # I like extra newline to space shit out
|
187
|
+
render_log
|
161
188
|
|
162
189
|
end # #call
|
163
190
|
|