nrser 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- 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
@@ -1,44 +0,0 @@
|
|
1
|
-
# Definitions
|
2
|
-
# =======================================================================
|
3
|
-
|
4
|
-
module NRSER
|
5
|
-
|
6
|
-
# @!group Hash Functions
|
7
|
-
|
8
|
-
# Removes the given keys from hash and returns it.
|
9
|
-
#
|
10
|
-
# Lifted from ActiveSupport.
|
11
|
-
#
|
12
|
-
# @see http://www.rubydoc.info/gems/activesupport/5.1.3/Hash:except!
|
13
|
-
#
|
14
|
-
# @param [Hash] hash
|
15
|
-
# Hash to mutate.
|
16
|
-
#
|
17
|
-
# @return [Hash]
|
18
|
-
#
|
19
|
-
def self.except_keys! hash, *keys
|
20
|
-
keys.each { |key| hash.delete(key) }
|
21
|
-
hash
|
22
|
-
end
|
23
|
-
|
24
|
-
singleton_class.send :alias_method, :omit_keys!, :except_keys!
|
25
|
-
|
26
|
-
|
27
|
-
# Returns a new hash without `keys`.
|
28
|
-
#
|
29
|
-
# Lifted from ActiveSupport.
|
30
|
-
#
|
31
|
-
# @see http://www.rubydoc.info/gems/activesupport/5.1.3/Hash:except
|
32
|
-
#
|
33
|
-
# @param [Hash] hash
|
34
|
-
# Source hash.
|
35
|
-
#
|
36
|
-
# @return [Hash]
|
37
|
-
#
|
38
|
-
def self.except_keys hash, *keys
|
39
|
-
except_keys! hash.dup, *keys
|
40
|
-
end
|
41
|
-
|
42
|
-
singleton_class.send :alias_method, :omit_keys, :except_keys
|
43
|
-
|
44
|
-
end # module NRSER
|
@@ -1,43 +0,0 @@
|
|
1
|
-
# Definitions
|
2
|
-
# =======================================================================
|
3
|
-
|
4
|
-
module NRSER
|
5
|
-
|
6
|
-
# @!group Hash Functions
|
7
|
-
|
8
|
-
# Lifted from ActiveSupport.
|
9
|
-
#
|
10
|
-
# @see http://www.rubydoc.info/gems/activesupport/5.1.3/Hash:slice
|
11
|
-
#
|
12
|
-
#
|
13
|
-
def self.slice_keys hash, *keys
|
14
|
-
# We're not using this, but, whatever, leave it in...
|
15
|
-
if hash.respond_to?(:convert_key, true)
|
16
|
-
keys.map! { |key| hash.send :convert_key, key }
|
17
|
-
end
|
18
|
-
|
19
|
-
keys.each_with_object(hash.class.new) { |k, new_hash|
|
20
|
-
new_hash[k] = hash[k] if hash.has_key?(k)
|
21
|
-
}
|
22
|
-
end
|
23
|
-
|
24
|
-
|
25
|
-
# Meant to be a drop-in replacement for the ActiveSupport version, though
|
26
|
-
# I've changed the implementation a bit... because honestly I didn't
|
27
|
-
# understand why they were doing it the way they do :/
|
28
|
-
#
|
29
|
-
# @see http://www.rubydoc.info/gems/activesupport/5.1.3/Hash:slice!
|
30
|
-
#
|
31
|
-
#
|
32
|
-
def self.slice_keys! hash, *keys
|
33
|
-
# We're not using this, but, whatever, leave it in...
|
34
|
-
if hash.respond_to?(:convert_key, true)
|
35
|
-
keys.map! { |key| hash.send :convert_key, key }
|
36
|
-
end
|
37
|
-
|
38
|
-
slice_keys( hash, *keys ).tap { |slice|
|
39
|
-
except_keys! hash, *keys
|
40
|
-
}
|
41
|
-
end
|
42
|
-
|
43
|
-
end # module NRSER
|
@@ -1,55 +0,0 @@
|
|
1
|
-
# Definitions
|
2
|
-
# =======================================================================
|
3
|
-
|
4
|
-
module NRSER
|
5
|
-
|
6
|
-
# @!group Hash Functions
|
7
|
-
|
8
|
-
# Converts all keys into strings by calling `#to_s` on them. **Mutates the
|
9
|
-
# hash.**
|
10
|
-
#
|
11
|
-
# Lifted from ActiveSupport.
|
12
|
-
#
|
13
|
-
# @param [Hash] hash
|
14
|
-
#
|
15
|
-
# @return [Hash<String, *>]
|
16
|
-
#
|
17
|
-
def self.stringify_keys! hash
|
18
|
-
transform_keys! hash, &:to_s
|
19
|
-
end
|
20
|
-
|
21
|
-
singleton_class.send :alias_method, :to_s_keys!, :stringify_keys!
|
22
|
-
|
23
|
-
|
24
|
-
# Returns a new hash with all keys transformed to strings by calling `#to_s`
|
25
|
-
# on them.
|
26
|
-
#
|
27
|
-
# Lifted from ActiveSupport.
|
28
|
-
#
|
29
|
-
# @param [Hash] hash
|
30
|
-
#
|
31
|
-
# @return [Hash<String, *>]
|
32
|
-
#
|
33
|
-
def self.stringify_keys hash
|
34
|
-
transform_keys hash, &:to_s
|
35
|
-
end
|
36
|
-
|
37
|
-
singleton_class.send :alias_method, :to_s_keys, :stringify_keys
|
38
|
-
|
39
|
-
|
40
|
-
# @todo Document deep_stringify_keys method.
|
41
|
-
#
|
42
|
-
# @param [type] arg_name
|
43
|
-
# @todo Add name param description.
|
44
|
-
#
|
45
|
-
# @return [return_type]
|
46
|
-
# @todo Document return value.
|
47
|
-
#
|
48
|
-
def self.deep_stringify_keys object
|
49
|
-
deep_transform_keys object, &:to_s
|
50
|
-
end # .deep_stringify_keys
|
51
|
-
|
52
|
-
singleton_class.send :alias_method, :to_s_keys_r, :deep_stringify_keys
|
53
|
-
|
54
|
-
|
55
|
-
end # module NRSER
|
@@ -1,57 +0,0 @@
|
|
1
|
-
# Definitions
|
2
|
-
# =======================================================================
|
3
|
-
|
4
|
-
module NRSER
|
5
|
-
|
6
|
-
# @!group Hash Functions
|
7
|
-
|
8
|
-
# Mutates `hash` by converting all keys that respond to `#to_sym` to symbols.
|
9
|
-
#
|
10
|
-
# Lifted from ActiveSupport.
|
11
|
-
#
|
12
|
-
# @see http://www.rubydoc.info/gems/activesupport/5.1.3/Hash:symbolize_keys!
|
13
|
-
#
|
14
|
-
# @param [Hash] hash
|
15
|
-
#
|
16
|
-
# @return [Hash]
|
17
|
-
#
|
18
|
-
def self.symbolize_keys! hash
|
19
|
-
transform_keys!(hash) { |key| key.to_sym rescue key }
|
20
|
-
end # .symbolize_keys!
|
21
|
-
|
22
|
-
singleton_class.send :alias_method, :to_sym_keys!, :symbolize_keys!
|
23
|
-
|
24
|
-
|
25
|
-
# Returns a new hash with all keys that respond to `#to_sym` converted to
|
26
|
-
# symbols.
|
27
|
-
#
|
28
|
-
# Lifted from ActiveSupport.
|
29
|
-
#
|
30
|
-
# @see http://www.rubydoc.info/gems/activesupport/5.1.3/Hash:symbolize_keys
|
31
|
-
#
|
32
|
-
# @param [Hash] hash
|
33
|
-
#
|
34
|
-
# @return [Hash]
|
35
|
-
#
|
36
|
-
def self.symbolize_keys hash
|
37
|
-
# File 'lib/active_support/core_ext/hash/keys.rb', line 54
|
38
|
-
transform_keys(hash) { |key| key.to_sym rescue key }
|
39
|
-
end
|
40
|
-
|
41
|
-
singleton_class.send :alias_method, :to_sym_keys, :symbolize_keys
|
42
|
-
|
43
|
-
|
44
|
-
# @todo Document deep_symbolize_keys method.
|
45
|
-
#
|
46
|
-
# @param [type] arg_name
|
47
|
-
# @todo Add name param description.
|
48
|
-
#
|
49
|
-
# @return [return_type]
|
50
|
-
# @todo Document return value.
|
51
|
-
#
|
52
|
-
def self.deep_symbolize_keys object, &block
|
53
|
-
deep_transform_keys( object ) { |key| key.to_sym rescue key }
|
54
|
-
end # .deep_symbolize_keys
|
55
|
-
|
56
|
-
|
57
|
-
end # module NRSER
|
@@ -1,140 +0,0 @@
|
|
1
|
-
module NRSER
|
2
|
-
|
3
|
-
# @!group Hash Functions
|
4
|
-
|
5
|
-
# Lifted from ActiveSupport.
|
6
|
-
#
|
7
|
-
# @see http://www.rubydoc.info/gems/activesupport/5.1.3/Hash:transform_keys!
|
8
|
-
#
|
9
|
-
# @param [Hash] hash
|
10
|
-
# Hash to mutate keys.
|
11
|
-
#
|
12
|
-
# @return [Hash]
|
13
|
-
# The mutated hash.
|
14
|
-
#
|
15
|
-
def self.transform_keys! hash
|
16
|
-
# File 'lib/active_support/core_ext/hash/keys.rb', line 23
|
17
|
-
hash.keys.each do |key|
|
18
|
-
hash[yield(key)] = hash.delete(key)
|
19
|
-
end
|
20
|
-
hash
|
21
|
-
end
|
22
|
-
|
23
|
-
|
24
|
-
# Returns a new hash with each key transformed by the provided block.
|
25
|
-
#
|
26
|
-
# Lifted from ActiveSupport.
|
27
|
-
#
|
28
|
-
# @see http://www.rubydoc.info/gems/activesupport/5.1.3/Hash:transform_keys
|
29
|
-
#
|
30
|
-
# @param [Hash] hash
|
31
|
-
#
|
32
|
-
# @return [Hash]
|
33
|
-
# New hash with transformed keys.
|
34
|
-
#
|
35
|
-
def self.transform_keys hash, &block
|
36
|
-
# File 'lib/active_support/core_ext/hash/keys.rb', line 12
|
37
|
-
result = {}
|
38
|
-
hash.each_key do |key|
|
39
|
-
result[yield(key)] = hash[key]
|
40
|
-
end
|
41
|
-
result
|
42
|
-
end
|
43
|
-
|
44
|
-
# My-style names
|
45
|
-
singleton_class.send :alias_method, :map_keys, :transform_keys
|
46
|
-
singleton_class.send :alias_method, :rekey, :transform_keys
|
47
|
-
|
48
|
-
|
49
|
-
# Deeply transform Hash keys that we can find by traversing Hash and Array
|
50
|
-
# instances that we can find from `object` and piping keys through `block`.
|
51
|
-
#
|
52
|
-
# @example Hash top node
|
53
|
-
#
|
54
|
-
# NRSER.deep_transform_keys({
|
55
|
-
# people: {
|
56
|
-
# jane: {fav_color: 'red'},
|
57
|
-
# joe: {fav_color: 'blue'},
|
58
|
-
# }
|
59
|
-
# }) { |key| key.to_s.upcase }
|
60
|
-
# # => {
|
61
|
-
# # "PEOPLE" => {
|
62
|
-
# # "JANE" => {"FAV_COLOR" => 'red'},
|
63
|
-
# # "JOE" => {"FAV_COLOR" => 'blue'},
|
64
|
-
# # }
|
65
|
-
#
|
66
|
-
# @example Array top node
|
67
|
-
#
|
68
|
-
# NRSER.deep_transform_keys(
|
69
|
-
# [{x: 2}, {y: 'blue'}, 3]
|
70
|
-
# ) { |key| key.to_s.upcase }
|
71
|
-
# # => [{'X' => 2}, {'Y' => 'blue'}, 3]
|
72
|
-
#
|
73
|
-
#
|
74
|
-
# @example Non-array or hash value
|
75
|
-
#
|
76
|
-
# NRSER.deep_transform_keys 'blah'
|
77
|
-
# # => 'blah'
|
78
|
-
#
|
79
|
-
# From ActiveSupport.
|
80
|
-
#
|
81
|
-
# @see http://www.rubydoc.info/gems/activesupport/5.1.3/Hash:deep_transform_keys
|
82
|
-
#
|
83
|
-
# @todo Maybe this is a tree function?
|
84
|
-
#
|
85
|
-
# @param [Object] object
|
86
|
-
# Anything; see examples.
|
87
|
-
#
|
88
|
-
# @param [Proc] &block
|
89
|
-
# Proc that should accept each key as it's only argument and return the
|
90
|
-
# new key to replace it with.
|
91
|
-
#
|
92
|
-
def self.deep_transform_keys object, &block
|
93
|
-
case object
|
94
|
-
when Hash
|
95
|
-
object.each_with_object( {} ) do |(key, value), result|
|
96
|
-
result[block.call( key )] = deep_transform_keys value, &block
|
97
|
-
end
|
98
|
-
when Array
|
99
|
-
object.map { |entry| deep_transform_keys entry, &block }
|
100
|
-
else
|
101
|
-
object
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
singleton_class.send :alias_method, :deep_map_keys, :deep_transform_keys
|
106
|
-
singleton_class.send :alias_method, :map_keys_r, :deep_transform_keys
|
107
|
-
singleton_class.send :alias_method, :deep_rekey, :deep_transform_keys
|
108
|
-
singleton_class.send :alias_method, :rekey_r, :deep_transform_keys
|
109
|
-
|
110
|
-
|
111
|
-
# Like {NRSER.deep_transform_keys} but mutates the objects (works in place).
|
112
|
-
#
|
113
|
-
# @param object (see NRSER.deep_transform_keys)
|
114
|
-
# @param &block (see NRSER.deep_transform_keys)
|
115
|
-
#
|
116
|
-
# @return [Object]
|
117
|
-
# The `object` that was passed in, post mutations.
|
118
|
-
#
|
119
|
-
def self.deep_transform_keys! object, &block
|
120
|
-
case object
|
121
|
-
when Hash
|
122
|
-
object.keys.each do |key|
|
123
|
-
value = object.delete key
|
124
|
-
object[block.call( key )] = deep_transform_keys! value, &block
|
125
|
-
end
|
126
|
-
object
|
127
|
-
when Array
|
128
|
-
object.map! {|e| deep_transform_keys!(e, &block)}
|
129
|
-
else
|
130
|
-
object
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
singleton_class.send :alias_method, :deep_map_keys!, :deep_transform_keys!
|
135
|
-
singleton_class.send :alias_method, :map_keys_r!, :deep_transform_keys!
|
136
|
-
singleton_class.send :alias_method, :deep_rekey!, :deep_transform_keys!
|
137
|
-
singleton_class.send :alias_method, :rekey_r!, :deep_transform_keys!
|
138
|
-
|
139
|
-
|
140
|
-
end # module NRSER
|
@@ -1,206 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
# frozen_string_literal: true
|
3
|
-
##
|
4
|
-
# Functions to get {Method} and {UnboundMethod} instances for class and
|
5
|
-
# instance methods (respectively) of a {Module}.
|
6
|
-
#
|
7
|
-
# @note
|
8
|
-
# Methods added to {NRSER::Ext::Module} can't be named the same as they will
|
9
|
-
# be in the extension due to shadowing.
|
10
|
-
#
|
11
|
-
##
|
12
|
-
|
13
|
-
|
14
|
-
# Definitions
|
15
|
-
# =======================================================================
|
16
|
-
|
17
|
-
module NRSER
|
18
|
-
|
19
|
-
# @!group Module Functions
|
20
|
-
# ==========================================================================
|
21
|
-
|
22
|
-
# Core private method that supports all the other "method getters".
|
23
|
-
#
|
24
|
-
# @private
|
25
|
-
#
|
26
|
-
# @param [Module] mod
|
27
|
-
# Module in question.
|
28
|
-
#
|
29
|
-
# @param [Boolean] include_super
|
30
|
-
# When `true`, includes inherited class methods.
|
31
|
-
#
|
32
|
-
# @param [:class | :instance] type:
|
33
|
-
# Get class or instance methods.
|
34
|
-
#
|
35
|
-
# @param [Boolean] sort:
|
36
|
-
# If `true`, will sort the methods by name, which is usually
|
37
|
-
# the useful way to look at and use them.
|
38
|
-
#
|
39
|
-
# @return [Array<(Method | UnboundMethod)>]
|
40
|
-
# List of method objects (all bound to `mod`).
|
41
|
-
#
|
42
|
-
def self.method_objects_for mod,
|
43
|
-
include_super,
|
44
|
-
type:,
|
45
|
-
sort:,
|
46
|
-
include_initialize: false
|
47
|
-
initialize_method = nil
|
48
|
-
|
49
|
-
get_names, get_method = case type
|
50
|
-
when :class
|
51
|
-
[:methods, :method]
|
52
|
-
|
53
|
-
when :instance
|
54
|
-
if include_initialize
|
55
|
-
# Only way I can figure out to find out if it is defined it to try
|
56
|
-
# to get the object and handle the error
|
57
|
-
begin
|
58
|
-
initialize_method = mod.instance_method :initialize
|
59
|
-
rescue NameError => error
|
60
|
-
else
|
61
|
-
# Don't want to include it if we're not `include_super` and it's
|
62
|
-
# inherited from a different module
|
63
|
-
unless include_super || initialize_method.owner == mod
|
64
|
-
initialize_method = nil
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
[:instance_methods, :instance_method]
|
70
|
-
|
71
|
-
else
|
72
|
-
raise ArgumentError,
|
73
|
-
"`type:` must be `:class` or `:instance`, found #{ type.inspect }"
|
74
|
-
|
75
|
-
end # case type
|
76
|
-
|
77
|
-
methods = mod.send( get_names, include_super ).map { |name|
|
78
|
-
mod.send get_method, name
|
79
|
-
}
|
80
|
-
|
81
|
-
methods << initialize_method unless initialize_method.nil?
|
82
|
-
|
83
|
-
methods.sort! { |a, b| a.name <=> b.name } if sort
|
84
|
-
|
85
|
-
methods
|
86
|
-
end # .method_objects_for
|
87
|
-
|
88
|
-
private_class_method :method_objects_for
|
89
|
-
|
90
|
-
|
91
|
-
# Get class methods for a {Module} ({Class} are also {Module}, so works
|
92
|
-
# same for those).
|
93
|
-
#
|
94
|
-
# @param mod (see .method_objects_for)
|
95
|
-
# @param include_super (see .method_objects_for)
|
96
|
-
# @param sort: (see .method_objects_for)
|
97
|
-
#
|
98
|
-
# @return [Array<Method>]
|
99
|
-
# List of method objects (all bound to `mod`).
|
100
|
-
#
|
101
|
-
def self.class_method_objects_for mod, include_super = true, sort: true
|
102
|
-
method_objects_for mod, include_super, type: :class, sort: sort
|
103
|
-
end # .class_method_objects_for
|
104
|
-
|
105
|
-
# Much shorter name
|
106
|
-
#
|
107
|
-
# @todo Not sure if I like this...
|
108
|
-
#
|
109
|
-
singleton_class.send :alias_method,
|
110
|
-
:class_Methods_for, :class_method_objects_for
|
111
|
-
|
112
|
-
|
113
|
-
# Just get the class methods defined in `mod` itself, omitting inherited
|
114
|
-
# ones.
|
115
|
-
#
|
116
|
-
# Equivalent to
|
117
|
-
#
|
118
|
-
# NRSER.class_method_objects_for false
|
119
|
-
#
|
120
|
-
# @param mod (see .class_method_objects_for)
|
121
|
-
# @param sort: (see .class_method_objects_for)
|
122
|
-
# @return (see .class_method_objects_for)
|
123
|
-
#
|
124
|
-
def self.own_class_method_objects_for mod, sort: true
|
125
|
-
class_method_objects_for mod, false, sort: sort
|
126
|
-
end # .own_class_method_objects_for
|
127
|
-
|
128
|
-
# Much shorter name
|
129
|
-
#
|
130
|
-
# @todo Not sure if I like this...
|
131
|
-
#
|
132
|
-
singleton_class.send :alias_method,
|
133
|
-
:own_class_Methods_for,
|
134
|
-
:own_class_method_objects_for
|
135
|
-
|
136
|
-
|
137
|
-
# Get instance methods for a {Module} ({Class} are also {Module}, so works
|
138
|
-
# same for those).
|
139
|
-
#
|
140
|
-
# @param mod (see .method_objects_for)
|
141
|
-
# @param include_super (see .method_objects_for)
|
142
|
-
# @param sort: (see .method_objects_for)
|
143
|
-
#
|
144
|
-
# @param [Boolean] include_initialize:
|
145
|
-
# When `true`, include `#initialize` method if it's defined, which is
|
146
|
-
# normally excluded from {Module#instance_methods}.
|
147
|
-
#
|
148
|
-
# Respects `include_super` - won't include it if we are only looking for
|
149
|
-
# own instance methods and it's inherited.
|
150
|
-
#
|
151
|
-
# @return [Array<UnboundMethod>]
|
152
|
-
# List of method objects (all unbound).
|
153
|
-
#
|
154
|
-
def self.instance_method_objects_for mod,
|
155
|
-
include_super = true,
|
156
|
-
sort: true,
|
157
|
-
include_initialize: false
|
158
|
-
method_objects_for \
|
159
|
-
mod,
|
160
|
-
include_super,
|
161
|
-
type: :instance,
|
162
|
-
sort: sort,
|
163
|
-
include_initialize: include_initialize
|
164
|
-
end # .instance_method_objects_for
|
165
|
-
|
166
|
-
# Much shorter name
|
167
|
-
#
|
168
|
-
# @todo Not sure if I like this...
|
169
|
-
#
|
170
|
-
singleton_class.send :alias_method,
|
171
|
-
:instance_Methods_for, :instance_method_objects_for
|
172
|
-
|
173
|
-
|
174
|
-
# Just get the instance methods defined in `mod` itself, omitting inherited
|
175
|
-
# ones.
|
176
|
-
#
|
177
|
-
# Equivalent to
|
178
|
-
#
|
179
|
-
# NRSER.instance_method_objects_for false
|
180
|
-
#
|
181
|
-
# @param mod (see .instance_method_objects_for)
|
182
|
-
# @param sort: (see .instance_method_objects_for)
|
183
|
-
# @return (see .instance_method_objects_for)
|
184
|
-
#
|
185
|
-
def self.own_instance_method_objects_for mod,
|
186
|
-
sort: true,
|
187
|
-
include_initialize: false
|
188
|
-
instance_method_objects_for \
|
189
|
-
mod,
|
190
|
-
false,
|
191
|
-
sort: sort,
|
192
|
-
include_initialize: include_initialize
|
193
|
-
end # .own_instance_method_objects_for
|
194
|
-
|
195
|
-
# Much shorter name
|
196
|
-
#
|
197
|
-
# @todo Not sure if I like this...
|
198
|
-
#
|
199
|
-
singleton_class.send :alias_method,
|
200
|
-
:own_instance_Methods_for,
|
201
|
-
:own_instance_method_objects_for
|
202
|
-
|
203
|
-
|
204
|
-
# @!endgroup Module Functions
|
205
|
-
|
206
|
-
end # module NRSER
|