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,214 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
# Requirements
|
5
|
+
# =======================================================================
|
6
|
+
|
7
|
+
# Deps
|
8
|
+
# -----------------------------------------------------------------------
|
9
|
+
|
10
|
+
# Need {Module#anonymous?}
|
11
|
+
require 'active_support/core_ext/module/anonymous'
|
12
|
+
|
13
|
+
# Project / Package
|
14
|
+
# -----------------------------------------------------------------------
|
15
|
+
|
16
|
+
# Need {String#underscore}, etc.
|
17
|
+
require 'nrser/core_ext/string'
|
18
|
+
|
19
|
+
# Need {NRSER::Meta::Source::Location.for_methods}
|
20
|
+
require 'nrser/meta/source/location'
|
21
|
+
|
22
|
+
# Need {Module.class_method_objects}, etc.
|
23
|
+
require_relative './method_objects'
|
24
|
+
|
25
|
+
|
26
|
+
# Definitions
|
27
|
+
# =======================================================================
|
28
|
+
|
29
|
+
# Extension methods for {Module}
|
30
|
+
#
|
31
|
+
class Module
|
32
|
+
|
33
|
+
# @!group Source Location Readers
|
34
|
+
# ==========================================================================
|
35
|
+
|
36
|
+
# Map class method names to the their source locations.
|
37
|
+
#
|
38
|
+
# @see NRSER::Meta::Source::Location.for_methods
|
39
|
+
#
|
40
|
+
# @param include_super (see Module#class_method_objects)
|
41
|
+
# @param sort: (see Module#class_method_objects)
|
42
|
+
# @param only_valid: (see NRSER::Meta::Source::Location.for_methods)
|
43
|
+
#
|
44
|
+
# @return (see NRSER::Meta::Source::Location.for_methods)
|
45
|
+
#
|
46
|
+
def class_method_locations include_super = true,
|
47
|
+
sort: true,
|
48
|
+
only_valid: false
|
49
|
+
NRSER::Meta::Source::Location.for_methods \
|
50
|
+
class_method_objects( include_super, sort: sort ),
|
51
|
+
only_valid: only_valid
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
# Just calls {#class_method_locations} with `include_super = false`.
|
56
|
+
#
|
57
|
+
# @param sort: (see #class_method_locations)
|
58
|
+
# @param only_valid: (see #class_method_locations)
|
59
|
+
#
|
60
|
+
# @return (see #class_method_locations)
|
61
|
+
#
|
62
|
+
def own_class_method_locations sort: true,
|
63
|
+
only_valid: false
|
64
|
+
class_method_locations false, sort: sort, only_valid: only_valid
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
# Map instance method names to the their source locations.
|
69
|
+
#
|
70
|
+
# @see NRSER::Meta::Source::Location.for_methods
|
71
|
+
#
|
72
|
+
# @param include_super (see Module#instance_method_objects)
|
73
|
+
# @param sort: (see Module#instance_method_objects)
|
74
|
+
# @param include_initialize: (see Module#instance_method_objects)
|
75
|
+
# @param only_valid: (see NRSER::Meta::Source::Location.for_methods)
|
76
|
+
#
|
77
|
+
# @return (see NRSER::Meta::Source::Location.for_methods)
|
78
|
+
#
|
79
|
+
def instance_method_locations include_super = true,
|
80
|
+
sort: true,
|
81
|
+
include_initialize: false,
|
82
|
+
only_valid: false
|
83
|
+
NRSER::Meta::Source::Location.for_methods \
|
84
|
+
instance_method_objects(
|
85
|
+
include_super,
|
86
|
+
sort: sort,
|
87
|
+
include_initialize: include_initialize,
|
88
|
+
),
|
89
|
+
only_valid: only_valid
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
# Just calls {#instance_method_locations} with `include_super = false`.
|
94
|
+
#
|
95
|
+
# @param sort: (see #instance_method_locations)
|
96
|
+
# @param include_initialize: (see #instance_method_locations)
|
97
|
+
# @param only_valid: (see NRSER::Meta::Source::Location.for_methods)
|
98
|
+
#
|
99
|
+
# @return (see #instance_method_locations)
|
100
|
+
#
|
101
|
+
def own_instance_method_locations sort: true,
|
102
|
+
include_initialize: false,
|
103
|
+
only_valid: false
|
104
|
+
instance_method_locations sort: sort,
|
105
|
+
include_initialize: include_initialize,
|
106
|
+
only_valid: only_valid
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
# Get *all* source locations for that module (or class) methods.
|
111
|
+
#
|
112
|
+
# @param [type] arg_name
|
113
|
+
# @todo Add name param description.
|
114
|
+
#
|
115
|
+
# @return [Hash<Method, Array<String, Fixnum>>]
|
116
|
+
# @todo Document return value.
|
117
|
+
#
|
118
|
+
def method_locations only_valid: false
|
119
|
+
# Get all the src locs for own methods
|
120
|
+
own_class_method_locations( only_valid: only_valid ).
|
121
|
+
map { |name, location|
|
122
|
+
[".#{ name }", location]
|
123
|
+
}.
|
124
|
+
to_h.
|
125
|
+
merge! \
|
126
|
+
own_instance_method_locations( only_valid: only_valid,
|
127
|
+
include_initialize: true ).
|
128
|
+
map { |name, location|
|
129
|
+
["##{ name }", location]
|
130
|
+
}.to_h
|
131
|
+
end # .module_source_locations
|
132
|
+
|
133
|
+
|
134
|
+
# Get the "canonical" lib-relative path for this module based off it's
|
135
|
+
# {#name} (via {String#underscore}, with `'.rb'` suffixed).
|
136
|
+
#
|
137
|
+
# @todo
|
138
|
+
# I bet ActiveSupport has some method for this re auto-loading.
|
139
|
+
#
|
140
|
+
# @return [nil]
|
141
|
+
# If this module is {#anonymous?}.
|
142
|
+
#
|
143
|
+
# @return [Pathname]
|
144
|
+
# If this module is not {#anonymous?}.
|
145
|
+
#
|
146
|
+
def canonical_rel_path
|
147
|
+
if anonymous?
|
148
|
+
nil
|
149
|
+
else
|
150
|
+
Pathname.new( name.underscore + '.rb' )
|
151
|
+
end
|
152
|
+
end # #canocical_rel_path
|
153
|
+
|
154
|
+
|
155
|
+
# Try to find a reasonable file and line for the module (or class) by
|
156
|
+
# looking at the locations of it's methods.
|
157
|
+
#
|
158
|
+
# @return [NRSER::Meta::Source::Location]
|
159
|
+
# Two entry array; first entry is the string file path, second is the
|
160
|
+
# line number.
|
161
|
+
#
|
162
|
+
# Note that both will be `nil` if we can't find a source location
|
163
|
+
# (the location will not be {NRSER::Meta::Source::Location#valid?}).
|
164
|
+
#
|
165
|
+
def source_location
|
166
|
+
# Get all the src locs for all methods
|
167
|
+
locations = method_locations only_valid: true
|
168
|
+
|
169
|
+
# Short circuit if we don't have shit to work with...
|
170
|
+
return NRSER::Meta::Source::Location.new if locations.empty?
|
171
|
+
|
172
|
+
# If any files end with the "canonical path" then use that. It's a path
|
173
|
+
# suffix
|
174
|
+
#
|
175
|
+
# "my_mod/sub_mod/some_class.rb"
|
176
|
+
#
|
177
|
+
# the for a class
|
178
|
+
#
|
179
|
+
# MyMod::SubMod::SomeClass
|
180
|
+
#
|
181
|
+
canonical_rel_path = self.canonical_rel_path
|
182
|
+
|
183
|
+
unless canonical_rel_path.nil?
|
184
|
+
|
185
|
+
# Find first line in canonical path (if any)
|
186
|
+
canonical_path_location = locations.
|
187
|
+
values.
|
188
|
+
find_all { |(path, line)| path.end_with? canonical_rel_path.to_s }.
|
189
|
+
min_by { |(path, line)| line }
|
190
|
+
|
191
|
+
# If we found one, we're done!
|
192
|
+
return canonical_path_location if canonical_path_location
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
# OK, that didn't work, so...
|
197
|
+
|
198
|
+
# If it's a {Class} and it has an `#initialize` method, point there.
|
199
|
+
#
|
200
|
+
if is_a?( Class ) && locations['#initialize']
|
201
|
+
return locations['#initialize']
|
202
|
+
end
|
203
|
+
|
204
|
+
# No dice. Moving on...
|
205
|
+
|
206
|
+
# Get the first line on the shortest path
|
207
|
+
locations.values.min_by { |(path, line)|
|
208
|
+
[path.length, line]
|
209
|
+
}
|
210
|
+
end # .module_source_location
|
211
|
+
|
212
|
+
# @!endgroup Source Location Readers
|
213
|
+
|
214
|
+
end # class Module
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
|
5
|
+
class Object
|
6
|
+
|
7
|
+
# If the instance variable `name` is not defined, sets it to the result
|
8
|
+
# of `&block`. Always returns the instance variable's value.
|
9
|
+
#
|
10
|
+
# Useful for lazy values that can be `nil` or `false`, since `||=` will
|
11
|
+
# always re-evaluate in their cases.
|
12
|
+
#
|
13
|
+
# @param [Symbol] name
|
14
|
+
# The name of the instance variable. Needs to have that `@` on the
|
15
|
+
# front, like `:@x`.
|
16
|
+
#
|
17
|
+
# @param [Proc<() => VALUE>] &block
|
18
|
+
# The block to call to get the value.
|
19
|
+
#
|
20
|
+
# @return [VALUE]
|
21
|
+
# The value of the instance variable.
|
22
|
+
#
|
23
|
+
def lazy_var name, &block
|
24
|
+
unless instance_variable_defined? name
|
25
|
+
instance_variable_set name, block.call
|
26
|
+
end
|
27
|
+
|
28
|
+
instance_variable_get name
|
29
|
+
end # #lazy_instance_var
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
class Object
|
3
|
+
# Yield `self`. Analogous to {#tap} but returns the result of the invoked
|
4
|
+
# block.
|
5
|
+
def thru
|
6
|
+
yield self
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
# Just an alias for `#equal?` that is easier for to remember.
|
11
|
+
#
|
12
|
+
# @param [*] other
|
13
|
+
# Something else.
|
14
|
+
#
|
15
|
+
# @return [Boolean]
|
16
|
+
# `true` if `self` and `other` are the same object.
|
17
|
+
#
|
18
|
+
def is? other
|
19
|
+
equal? other
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
# See {NRSER.truthy?}.
|
24
|
+
def truthy?
|
25
|
+
NRSER.truthy? self
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
# See {NRSER.falsy?}.
|
30
|
+
def falsy?
|
31
|
+
NRSER.falsy? self
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
# Calls {NRSER.as_hash} on `self` with the provided `key`.
|
36
|
+
def as_hash key = nil
|
37
|
+
NRSER.as_hash self, key
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
# Call {NRSER.as_array} on `self`.
|
42
|
+
def as_array
|
43
|
+
NRSER.as_array self
|
44
|
+
end
|
45
|
+
|
46
|
+
end # class Object
|
@@ -1,6 +1,6 @@
|
|
1
|
+
require 'pathname'
|
1
2
|
|
2
|
-
|
3
|
-
module NRSER::Ext::Pathname
|
3
|
+
class Pathname
|
4
4
|
|
5
5
|
# override to accept Pathname instances.
|
6
6
|
#
|
@@ -15,6 +15,9 @@ module NRSER::Ext::Pathname
|
|
15
15
|
end
|
16
16
|
|
17
17
|
|
18
|
+
alias_method :_original_sub, :sub
|
19
|
+
|
20
|
+
|
18
21
|
# override sub to support Pathname instances as patterns.
|
19
22
|
#
|
20
23
|
# @param [String | Regexp | Pathname] pattern
|
@@ -29,9 +32,9 @@ module NRSER::Ext::Pathname
|
|
29
32
|
def sub pattern, replacement
|
30
33
|
case pattern
|
31
34
|
when Pathname
|
32
|
-
|
35
|
+
_original_sub pattern.to_s, replacement
|
33
36
|
else
|
34
|
-
|
37
|
+
_original_sub pattern, replacement
|
35
38
|
end
|
36
39
|
end
|
37
40
|
|
@@ -71,4 +74,4 @@ module NRSER::Ext::Pathname
|
|
71
74
|
NRSER.find_up! rel_path, **kwds, from: self
|
72
75
|
end # #find_root
|
73
76
|
|
74
|
-
end #
|
77
|
+
end # class Pathname
|
@@ -1,11 +1,10 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require 'active_support/core_ext/string/filters'
|
3
|
+
require 'active_support/core_ext/string/inflections'
|
4
|
+
|
1
5
|
# Extension methods for {String}
|
2
6
|
#
|
3
|
-
|
4
|
-
|
5
|
-
def squish
|
6
|
-
NRSER.squish self
|
7
|
-
end
|
8
|
-
|
7
|
+
class String
|
9
8
|
|
10
9
|
def unblock
|
11
10
|
NRSER.unblock self
|
@@ -22,11 +21,6 @@ module NRSER::Ext::String
|
|
22
21
|
end
|
23
22
|
|
24
23
|
|
25
|
-
def truncate *args
|
26
|
-
NRSER.truncate self, *args
|
27
|
-
end
|
28
|
-
|
29
|
-
|
30
24
|
# See {NRSER.constantize}
|
31
25
|
def constantize
|
32
26
|
NRSER.constantize self
|
@@ -88,4 +82,4 @@ module NRSER::Ext::String
|
|
88
82
|
|
89
83
|
# @!endgroup Unicode Stylization
|
90
84
|
|
91
|
-
end #
|
85
|
+
end # class String
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
# Requirements
|
5
|
+
# =======================================================================
|
6
|
+
|
7
|
+
# Stdlib
|
8
|
+
# -----------------------------------------------------------------------
|
9
|
+
require 'time'
|
10
|
+
|
11
|
+
# Deps
|
12
|
+
# ------------------------------------------------------------------------
|
13
|
+
|
14
|
+
require 'active_support/core_ext/time'
|
15
|
+
|
16
|
+
|
17
|
+
# Definitions
|
18
|
+
# =======================================================================
|
19
|
+
|
20
|
+
class Time
|
21
|
+
|
22
|
+
# `#iso8601` with the `-` and `:` characters removed. Intended to be more
|
23
|
+
# agreeable to a wider range of file systems and programs than the standard
|
24
|
+
# format, while still adhering to ISO 8601 (as far as I can tell) and
|
25
|
+
# acceptable to {Time.parse}.
|
26
|
+
#
|
27
|
+
# There is nothing tricky about this method; I just wanted to standardize a
|
28
|
+
# format for these situations. I *hate* reading date-times like this, but
|
29
|
+
# it seems like the best and safest approach :/
|
30
|
+
#
|
31
|
+
# @example
|
32
|
+
# time = Time.now
|
33
|
+
#
|
34
|
+
# time.iso8601
|
35
|
+
# # => "2018-04-19T03:00:30+08:00" # Nice, but potentially problematic
|
36
|
+
#
|
37
|
+
# Time.now.iso8601_for_files
|
38
|
+
# # => "20180419T030030+8000" # Fucking ugly, but easier on stupid systems
|
39
|
+
#
|
40
|
+
# @return [String]
|
41
|
+
#
|
42
|
+
def iso8601_for_idiots
|
43
|
+
iso8601.gsub /\-\:/, ''
|
44
|
+
end # #iso8601_for_idiots
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# Requires *ALL* of the core extensions
|
2
|
+
|
3
|
+
require_relative './core_ext/array'
|
4
|
+
require_relative './core_ext/binding'
|
5
|
+
require_relative './core_ext/enumerable'
|
6
|
+
require_relative './core_ext/exception'
|
7
|
+
require_relative './core_ext/hash'
|
8
|
+
require_relative './core_ext/module'
|
9
|
+
require_relative './core_ext/object'
|
10
|
+
require_relative './core_ext/open_struct'
|
11
|
+
require_relative './core_ext/pathname'
|
12
|
+
require_relative './core_ext/string'
|
13
|
+
require_relative './core_ext/symbol'
|
@@ -0,0 +1,150 @@
|
|
1
|
+
require 'nrser/core_ext/object/lazy_var'
|
2
|
+
|
3
|
+
require_relative './nicer_error'
|
4
|
+
|
5
|
+
# Extension of Ruby's {NotImplementedError} to provide a useful message
|
6
|
+
# and convenient constructor for abstract methods.
|
7
|
+
#
|
8
|
+
# This is a {NRSER::NicerError}.
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
#
|
12
|
+
# def f
|
13
|
+
# raise NRSER::AbstractMethodError.new( self, __method__ )
|
14
|
+
#
|
15
|
+
#
|
16
|
+
class NRSER::AbstractMethodError < NotImplementedError
|
17
|
+
|
18
|
+
# Mixins
|
19
|
+
# ========================================================================
|
20
|
+
|
21
|
+
# Be nicer!
|
22
|
+
include NRSER::NicerError
|
23
|
+
|
24
|
+
# Be log!
|
25
|
+
include NRSER::Log::Mixin
|
26
|
+
|
27
|
+
|
28
|
+
# Attributes
|
29
|
+
# ========================================================================
|
30
|
+
|
31
|
+
# The abstract method's name that was called¹.
|
32
|
+
#
|
33
|
+
# > ¹ I mean, that's what it *should* be, it's really just what was passed
|
34
|
+
# > as the `method_name:` keyword to {#initialize}.
|
35
|
+
#
|
36
|
+
# @return [Symbol]
|
37
|
+
#
|
38
|
+
attr_reader :method_name
|
39
|
+
|
40
|
+
|
41
|
+
# TODO document `method_instance` attribute.
|
42
|
+
#
|
43
|
+
# @return [Method]
|
44
|
+
#
|
45
|
+
attr_reader :method_instance
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
# TODO document `instance` attribute.
|
50
|
+
#
|
51
|
+
# @return [attr_type]
|
52
|
+
#
|
53
|
+
attr_reader :instance
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
# Construct a new `AbstractMethodError`.
|
58
|
+
#
|
59
|
+
# @param [Object] instance
|
60
|
+
# Instance that invoked the abstract method.
|
61
|
+
#
|
62
|
+
# @param [Symbol | String] method_name
|
63
|
+
# Name of abstract method.
|
64
|
+
#
|
65
|
+
def initialize instance, method_name
|
66
|
+
@instance = instance
|
67
|
+
@method_name = method_name
|
68
|
+
@method_instance = instance.method @method_name
|
69
|
+
|
70
|
+
super()
|
71
|
+
end # #initialize
|
72
|
+
|
73
|
+
|
74
|
+
def method_instance
|
75
|
+
lazy_var :@method_instance do
|
76
|
+
# Just drop a warning if we can't get the method object
|
77
|
+
logger.catch.warn(
|
78
|
+
"Failed to get method",
|
79
|
+
instance: instance,
|
80
|
+
method_name: method_name,
|
81
|
+
) do
|
82
|
+
instance.method method_name
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
def method_owner
|
89
|
+
lazy_var :@method_owner do
|
90
|
+
method_instance && method_instance.owner
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
def method_owner_name
|
96
|
+
lazy_var :@method_owner_name do
|
97
|
+
if method_owner
|
98
|
+
method_owner.safe_name
|
99
|
+
else
|
100
|
+
'???'
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
def method_full_name
|
107
|
+
lazy_var :@method_full_name do
|
108
|
+
"#{ method_owner_name }##{ method_name.to_s }"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
def context
|
114
|
+
{
|
115
|
+
instance: instance,
|
116
|
+
method_name: method_name,
|
117
|
+
}
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
def default_message
|
122
|
+
"Method ##{ method_name.to_s } is abstract"
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
def details
|
127
|
+
@details ||= if method_owner == instance.class
|
128
|
+
<<~END
|
129
|
+
Method #{ method_full_name } is abstract, meaning
|
130
|
+
#{ method_owner_name } is an abstract class and the invoking
|
131
|
+
instance #{ instance } should NOT have been constructed.
|
132
|
+
END
|
133
|
+
else
|
134
|
+
<<~END
|
135
|
+
Method #{ method_full_name } is abstract and
|
136
|
+
has not been implemented in invoking class #{ instance.class }.
|
137
|
+
|
138
|
+
If you *are* developing the invoking class #{ instance.class } it
|
139
|
+
(or a parent class between it and #{ method_owner_name }) must
|
140
|
+
implement ##{ method_name.to_s }.
|
141
|
+
|
142
|
+
If you *are not* developing #{ instance.class } it should be treated
|
143
|
+
as an abstract base class and should NOT be constructed. You need to
|
144
|
+
find a subclass of #{ instance.class } to instantiate or write
|
145
|
+
your own.
|
146
|
+
END
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
end # class NRSER::AbstractMethodError
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
# Requirements
|
5
|
+
# =======================================================================
|
6
|
+
|
7
|
+
# Stdlib
|
8
|
+
# -----------------------------------------------------------------------
|
9
|
+
|
10
|
+
# Deps
|
11
|
+
# -----------------------------------------------------------------------
|
12
|
+
|
13
|
+
# Project / Package
|
14
|
+
# -----------------------------------------------------------------------
|
15
|
+
|
16
|
+
require_relative './nicer_error'
|
17
|
+
|
18
|
+
|
19
|
+
# Declarations
|
20
|
+
# =======================================================================
|
21
|
+
|
22
|
+
|
23
|
+
# Definitions
|
24
|
+
# =======================================================================
|
25
|
+
|
26
|
+
|
27
|
+
# Extension of {::ArgumentError} that includes {NRSER::NicerError} and
|
28
|
+
# supports optional
|
29
|
+
#
|
30
|
+
class NRSER::ArgumentError < ::ArgumentError
|
31
|
+
|
32
|
+
include NRSER::NicerError
|
33
|
+
|
34
|
+
|
35
|
+
# Instance Methods
|
36
|
+
# ========================================================================
|
37
|
+
|
38
|
+
# def default_message
|
39
|
+
# ["Expected", name, ""]
|
40
|
+
# end
|
41
|
+
|
42
|
+
end # class NRSER::ArgumentError
|