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
data/lib/nrser/types/where.rb
CHANGED
@@ -1,22 +1,107 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative './type'
|
3
5
|
|
4
6
|
module NRSER::Types
|
7
|
+
|
8
|
+
# {Where} instances are predicate functions¹ as a type.
|
9
|
+
#
|
10
|
+
# They have a {#predicate} block, and {#test?} calls it with values and
|
11
|
+
# returns the boolean of the result (double-bangs it `!!`).
|
12
|
+
#
|
13
|
+
# Super simple, right? And easy! Why don't we just use these things all over
|
14
|
+
# the place?
|
15
|
+
#
|
16
|
+
# If you're been around the programing block a few times, you probably saw
|
17
|
+
# this coming a mile away: you should avoid using them.
|
18
|
+
#
|
19
|
+
# Yeah, sorry. Here's the reasons:
|
20
|
+
#
|
21
|
+
# 1. They're **opaque** - it's hard to see inside a {Proc}... even if you
|
22
|
+
# got the source code (which seems like it requires gems and involves
|
23
|
+
# some amount of hackery), that wouldn't really give you the whole
|
24
|
+
# picture because you need to look at the binding as well... Ruby
|
25
|
+
# procs capture their entire environment.
|
26
|
+
#
|
27
|
+
# Essentially, they suck to easily and/or comprehensively communicate
|
28
|
+
# what they hell they do.
|
29
|
+
#
|
30
|
+
# 2. Like {When}, they're totally Ruby-centric... we can't really serialize
|
31
|
+
# them and pass them off anywhere, so they're shitty for APIs and
|
32
|
+
# property types and stuff that you may want or need to expose outside
|
33
|
+
# the runtime.
|
34
|
+
#
|
35
|
+
# In this sense they're ok as implementations of types like {.file_path}
|
36
|
+
# that represent an *idea* to be communicated to the outside world,
|
37
|
+
# where each system that handles that idea will need to have it's own
|
38
|
+
# implementation of it.
|
39
|
+
#
|
40
|
+
# Lit addresses a lot of this with serializable functions, but that's
|
41
|
+
# nowhere near ready to rock, and support for it would probably be
|
42
|
+
# added along side {Where}, not in place of it (since {Where} is
|
43
|
+
# probably still going to be used and useful).
|
44
|
+
#
|
45
|
+
# So please be aware of those, and be reasonable about your {Where}s.
|
46
|
+
#
|
47
|
+
# > ¹ I say *functions*, because they really *should* be functions (same
|
48
|
+
# > input always gets same output, pure, etc.).
|
49
|
+
# >
|
50
|
+
# > Yeah, there's not much stopping you from making them state-based or
|
51
|
+
# > random or whatever, but please don't do that shit unless you've really
|
52
|
+
# > thought it through. And if you still do, please write me and tell me
|
53
|
+
# > what you thought and why it's a reasonable idea and I'll update this.
|
54
|
+
#
|
5
55
|
class Where < NRSER::Types::Type
|
56
|
+
|
57
|
+
# Predicate {Proc} used to test value for membership.
|
58
|
+
#
|
59
|
+
# @return [Proc<(V) => Boolean>]
|
60
|
+
# Really, we double-bang (`!!`) whatever the predicate returns to
|
61
|
+
# get the result in {#test?}, but you get the idea... the response will
|
62
|
+
# be evaluated on its truthiness.
|
63
|
+
#
|
6
64
|
attr_reader :predicate
|
7
65
|
|
8
|
-
|
66
|
+
|
67
|
+
# Make a new {Where}.
|
68
|
+
#
|
69
|
+
# @param [Proc<(V) => Boolean>] &predicate
|
70
|
+
# See {#predicate}.
|
71
|
+
#
|
72
|
+
# @param **options (see NRSER::Types::Type#initialize)
|
73
|
+
#
|
74
|
+
def initialize **options, &predicate
|
9
75
|
super **options
|
76
|
+
|
77
|
+
unless predicate.arity == 1
|
78
|
+
raise NRSER::ArgumentError.new \
|
79
|
+
"Predicate block must have arity 1",
|
80
|
+
predicate: predicate,
|
81
|
+
options: options
|
82
|
+
end
|
83
|
+
|
10
84
|
@predicate = predicate
|
11
85
|
end
|
12
86
|
|
13
|
-
|
87
|
+
|
88
|
+
# Test a value for membership.
|
89
|
+
#
|
90
|
+
# @param (see NRSER::Types::Type#test?)
|
91
|
+
# @return (see NRSER::Types::Type#test?)
|
92
|
+
# @raise (see NRSER::Types::Type#test?)
|
93
|
+
#
|
94
|
+
def test? value
|
14
95
|
!!@predicate.call(value)
|
15
96
|
end
|
16
|
-
|
97
|
+
|
98
|
+
end # class Where
|
17
99
|
|
18
|
-
|
19
|
-
|
20
|
-
|
100
|
+
|
101
|
+
# Get a type based on a predicate.
|
102
|
+
#
|
103
|
+
def_factory :where do |**options, &predicate|
|
104
|
+
Where.new **options, &predicate
|
21
105
|
end
|
22
|
-
|
106
|
+
|
107
|
+
end # NRSER::Types
|
data/lib/nrser/types.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
1
4
|
# Abstract infrastructure for type creation - stuff that doesn't define any
|
2
5
|
# concrete type instances.
|
3
6
|
#
|
@@ -6,13 +9,9 @@
|
|
6
9
|
# uncontrollable mutability of Ruby and the importance of type checks)
|
7
10
|
# need to be required in the "Post-Processing" section at the bottom.
|
8
11
|
#
|
9
|
-
require_relative './types/type'
|
10
|
-
|
11
|
-
# Refinements
|
12
|
-
# =======================================================================
|
13
|
-
|
14
|
-
using NRSER
|
15
12
|
|
13
|
+
# Want to mix logging in
|
14
|
+
require 'nrser/log'
|
16
15
|
|
17
16
|
# Stuff to help you define, test, check and match types in Ruby.
|
18
17
|
#
|
@@ -20,9 +19,31 @@ using NRSER
|
|
20
19
|
#
|
21
20
|
module NRSER::Types
|
22
21
|
|
22
|
+
# Sub-Tree Requirements
|
23
|
+
# ========================================================================
|
24
|
+
|
25
|
+
require_relative './types/type'
|
26
|
+
require_relative './types/factory'
|
27
|
+
|
28
|
+
|
29
|
+
# Mixins
|
30
|
+
# ========================================================================
|
31
|
+
|
32
|
+
extend Factory
|
33
|
+
include NRSER::Log::Mixin
|
34
|
+
|
35
|
+
|
36
|
+
# Constants
|
37
|
+
# ========================================================================
|
38
|
+
|
23
39
|
L_PAREN = '(' # '❪'
|
24
40
|
R_PAREN = ')' # '❫'
|
25
41
|
RESPONDS_WITH = '→'
|
42
|
+
ASSOC = '=>'
|
43
|
+
|
44
|
+
|
45
|
+
# Module Methods
|
46
|
+
# ========================================================================
|
26
47
|
|
27
48
|
# Make a {NRSER::Types::Type} from a value.
|
28
49
|
#
|
@@ -55,17 +76,56 @@ module NRSER::Types
|
|
55
76
|
end
|
56
77
|
|
57
78
|
|
58
|
-
#
|
59
|
-
|
60
|
-
|
79
|
+
# The {.make} method reference; for easy map and such.
|
80
|
+
#
|
81
|
+
# @return [Method]
|
82
|
+
#
|
83
|
+
def self.maker
|
84
|
+
method :make
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
# Create a {NRSER::Types::Type} from `type` with {.make} and check that
|
89
|
+
# `value` satisfies it, raising if it doesn't.
|
90
|
+
#
|
91
|
+
# @param [*] value
|
92
|
+
# Value to type check.
|
93
|
+
#
|
94
|
+
# @param [*] type
|
95
|
+
# Type to check value against.
|
96
|
+
#
|
97
|
+
# @return
|
98
|
+
# The `value` parameter.
|
99
|
+
#
|
100
|
+
# @raise [NRSER::Types::CheckError]
|
101
|
+
# If the value fails the type check.
|
102
|
+
#
|
103
|
+
def self.check! value, type
|
104
|
+
make( type ).check! value
|
61
105
|
end
|
62
106
|
|
107
|
+
# Old name
|
108
|
+
singleton_class.send :alias_method, :check, :check!
|
109
|
+
|
63
110
|
|
64
|
-
|
111
|
+
# Create a {NRSER::Types::Type} from `type` with {.make} and test if
|
112
|
+
# `value` satisfies it.
|
113
|
+
#
|
114
|
+
# @return [Boolean]
|
115
|
+
# `true` if `value` satisfies `type`.
|
116
|
+
#
|
117
|
+
def self.test? value, type
|
65
118
|
make(type).test value
|
66
119
|
end
|
67
120
|
|
121
|
+
# Old name
|
122
|
+
singleton_class.send :alias_method, :test, :test?
|
123
|
+
|
68
124
|
|
125
|
+
# @todo
|
126
|
+
# Switch {NRSER::Types.match} to use `===`! Should allow us to avoid
|
127
|
+
# making types for everything?
|
128
|
+
#
|
69
129
|
def self.match value, *clauses
|
70
130
|
if clauses.empty?
|
71
131
|
raise ArgumentError.new NRSER.dedent <<-END
|
@@ -95,7 +155,7 @@ module NRSER::Types
|
|
95
155
|
end
|
96
156
|
|
97
157
|
enum.each { |type, expression|
|
98
|
-
if test value, type
|
158
|
+
if test? value, type
|
99
159
|
# OK, we matched! Is the corresponding expression callable?
|
100
160
|
if expression.respond_to? :call
|
101
161
|
# It is; invoke and return result.
|
@@ -138,57 +198,6 @@ module NRSER::Types
|
|
138
198
|
}
|
139
199
|
end # .from_repr
|
140
200
|
|
141
|
-
|
142
|
-
# Define a type factory.
|
143
|
-
#
|
144
|
-
# @!macro [attach] factory
|
145
|
-
# @param [Hash] **options
|
146
|
-
# Common type construction options, see {Type#initialize}.
|
147
|
-
#
|
148
|
-
# @return [NRSER::Types::Type]
|
149
|
-
# The type.
|
150
|
-
#
|
151
|
-
def self.def_factory name, maybe: true, aliases: [], &body
|
152
|
-
define_singleton_method name, &body
|
153
|
-
|
154
|
-
aliases.each do |alias_name|
|
155
|
-
singleton_class.send :alias_method, alias_name, name
|
156
|
-
end
|
157
|
-
|
158
|
-
if maybe && !name.to_s.end_with?( '?' )
|
159
|
-
maybe_name = "#{ name }?".to_sym
|
160
|
-
|
161
|
-
# HACK Ugh maybe I wrote this quick to fix it, not sure if it's a decent
|
162
|
-
# idea.. basically, need to figure out what `options` keys go
|
163
|
-
# to {.maybe} and which ones go to the regular factory... matters
|
164
|
-
# for shit like {.attrs} and {.hash_type} 'cause they use option
|
165
|
-
# keys (whether they *should* is something I've debated... sigh,
|
166
|
-
# it is what it is for now).
|
167
|
-
#
|
168
|
-
# So they options that go to {.maybe} just go strait through to
|
169
|
-
# {Type#initialize}, so just grab that method, see what keys it
|
170
|
-
# takes, and then can slice and dice off that...
|
171
|
-
#
|
172
|
-
maybe_option_keys = Set.new \
|
173
|
-
Type.
|
174
|
-
instance_method( :initialize ).
|
175
|
-
parameters.
|
176
|
-
select { |param_type, name| param_type == :key }.
|
177
|
-
map { |param_type, name| name }
|
178
|
-
|
179
|
-
define_singleton_method maybe_name do |*args, **options|
|
180
|
-
maybe_options = options.slice *maybe_option_keys
|
181
|
-
factory_options = options.except *maybe_option_keys
|
182
|
-
|
183
|
-
maybe public_send( name, *args, **factory_options ), **maybe_options
|
184
|
-
end
|
185
|
-
|
186
|
-
aliases.each do |alias_name|
|
187
|
-
singleton_class.send :alias_method, "#{ alias_name }?", maybe_name
|
188
|
-
end
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
201
|
end # NRSER::Types
|
193
202
|
|
194
203
|
|
@@ -218,7 +227,7 @@ require_relative './types/numbers'
|
|
218
227
|
require_relative './types/strings'
|
219
228
|
require_relative './types/symbols'
|
220
229
|
require_relative './types/labels'
|
221
|
-
require_relative './types/
|
230
|
+
require_relative './types/arrays'
|
222
231
|
require_relative './types/hashes'
|
223
232
|
require_relative './types/paths'
|
224
233
|
require_relative './types/tuples'
|
data/lib/nrser/version.rb
CHANGED
data/lib/nrser.rb
CHANGED
@@ -14,13 +14,20 @@ require 'pp'
|
|
14
14
|
require 'ostruct'
|
15
15
|
require 'json'
|
16
16
|
require 'yaml'
|
17
|
-
require 'logger'
|
18
17
|
require 'singleton'
|
19
18
|
|
20
19
|
# Deps
|
21
20
|
# -----------------------------------------------------------------------
|
22
21
|
require 'hamster'
|
23
22
|
|
23
|
+
### Active Support
|
24
|
+
#
|
25
|
+
# We're not going to import all of it, but here we'll import the stuff we
|
26
|
+
# always want to use and then add pieces in places as needed.
|
27
|
+
#
|
28
|
+
require 'active_support/json'
|
29
|
+
# require 'active_support/core_ext/object/json'
|
30
|
+
|
24
31
|
|
25
32
|
# Hi there!
|
26
33
|
#
|
@@ -47,33 +54,26 @@ require 'hamster'
|
|
47
54
|
# Enjoy!
|
48
55
|
#
|
49
56
|
|
50
|
-
#
|
51
|
-
# else, then logging
|
57
|
+
# Load up version, which has {NRSER::ROOT} in it and depends on nothing else
|
52
58
|
require_relative './nrser/version'
|
53
|
-
require_relative './nrser/logging'
|
54
59
|
|
55
|
-
#
|
56
|
-
|
57
|
-
require_relative './nrser/ext'
|
60
|
+
# {Module.safe_name} is really useful all around, including in logging
|
61
|
+
require_relative './nrser/core_ext/module/names'
|
58
62
|
|
59
|
-
#
|
60
|
-
|
61
|
-
#
|
62
|
-
# This way everything else should be able to use them.
|
63
|
-
#
|
64
|
-
require_relative './nrser/refinements'
|
63
|
+
# Then logging can come in...
|
64
|
+
require_relative './nrser/log'
|
65
65
|
|
66
|
-
#
|
66
|
+
# Tons need this for error messages
|
67
|
+
require_relative './nrser/core_ext/binding'
|
68
|
+
|
69
|
+
# Then everything else...
|
67
70
|
require_relative './nrser/char'
|
68
71
|
require_relative './nrser/errors'
|
69
72
|
require_relative './nrser/no_arg'
|
70
73
|
require_relative './nrser/message'
|
71
|
-
require_relative './nrser/env'
|
72
74
|
require_relative './nrser/collection'
|
73
75
|
require_relative './nrser/functions'
|
74
76
|
require_relative './nrser/types'
|
75
77
|
require_relative './nrser/meta'
|
78
|
+
require_relative './nrser/props'
|
76
79
|
require_relative './nrser/mean_streak'
|
77
|
-
|
78
|
-
# 5. Stuff that *uses* the refinements *at require time* (usually defining
|
79
|
-
# constants or meta-programming)
|
@@ -1,11 +1,11 @@
|
|
1
|
-
require '
|
1
|
+
require 'nrser/core_ext/binding'
|
2
2
|
|
3
|
-
describe '
|
3
|
+
describe 'Binding#erb' do
|
4
4
|
it "processes a simple template" do
|
5
5
|
x = 1
|
6
6
|
|
7
7
|
expect(
|
8
|
-
|
8
|
+
binding.erb <<-BLOCK
|
9
9
|
x is <%= x %>
|
10
10
|
BLOCK
|
11
11
|
).to eq NRSER.dedent <<-BLOCK
|
@@ -14,7 +14,7 @@ describe 'NRSER.template' do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
it "handles edge cases" do
|
17
|
-
expect(
|
18
|
-
expect(
|
17
|
+
expect( binding.erb '' ).to eq ''
|
18
|
+
expect( binding.erb 'blah' ).to eq 'blah'
|
19
19
|
end
|
20
20
|
end # template
|
@@ -1,13 +1,15 @@
|
|
1
|
+
require 'nrser/core_ext/enumerable/find_map'
|
2
|
+
|
1
3
|
describe_spec_file(
|
2
4
|
spec_path: __FILE__,
|
3
|
-
module:
|
4
|
-
|
5
|
+
module: Enumerable,
|
6
|
+
instance_method: :find_map,
|
5
7
|
) do
|
6
8
|
|
7
9
|
describe "when a result is found" do
|
8
10
|
it "should return the block result" do
|
9
11
|
expect(
|
10
|
-
|
12
|
+
[1, 2, 3, 4].find_map do |i|
|
11
13
|
if i.even?
|
12
14
|
"#{ i } is even!"
|
13
15
|
end
|
@@ -20,7 +22,7 @@ describe_spec_file(
|
|
20
22
|
describe "when block returns `false`" do
|
21
23
|
it "should not be considered 'found'" do
|
22
24
|
expect(
|
23
|
-
|
25
|
+
[1, 2, 3].find_map { |i| false }
|
24
26
|
).to be nil
|
25
27
|
end
|
26
28
|
end
|
@@ -29,10 +31,10 @@ describe_spec_file(
|
|
29
31
|
describe "when none found and `ifnone` provided" do
|
30
32
|
it "should call `ifnone` and return the response" do
|
31
33
|
expect(
|
32
|
-
|
34
|
+
[].find_map( -> { 'blah' } ) { |i| false }
|
33
35
|
).to eq 'blah'
|
34
36
|
end
|
35
37
|
end # "when none found and `ifnone` provided"
|
36
38
|
|
37
39
|
|
38
|
-
end
|
40
|
+
end # spec_file
|
@@ -1,19 +1,6 @@
|
|
1
|
-
require '
|
1
|
+
require 'nrser/core_ext/hash'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
describe 'Refinement Hash#map_values' do
|
6
|
-
it do
|
7
|
-
expect(
|
8
|
-
{x: 1, y: 2}.map_values { |key, value| value + 1 }
|
9
|
-
).to eq(
|
10
|
-
{x: 2, y: 3}
|
11
|
-
)
|
12
|
-
end
|
13
|
-
end # 'Hash#map_values'
|
14
|
-
|
15
|
-
|
16
|
-
describe 'Refinement Hash#find_bounded' do
|
3
|
+
describe 'Hash#find_bounded' do
|
17
4
|
subject { {a: 1, b: 2, c: 3} }
|
18
5
|
|
19
6
|
it do
|
@@ -25,10 +12,10 @@ describe 'Refinement Hash#find_bounded' do
|
|
25
12
|
subject.find_bounded(length: 2) { |k, v| v == 2 }
|
26
13
|
}.to raise_error TypeError
|
27
14
|
end
|
28
|
-
end #
|
15
|
+
end # Hash#find_bounded
|
29
16
|
|
30
17
|
|
31
|
-
describe '
|
18
|
+
describe 'Hash#find_only' do
|
32
19
|
subject { {a: 1, b: 2, c: 3} }
|
33
20
|
|
34
21
|
it do
|
@@ -40,17 +27,17 @@ describe 'Refinement Hash#find_only' do
|
|
40
27
|
subject.find_only { |k, i| i >= 2 }
|
41
28
|
}.to raise_error TypeError
|
42
29
|
end
|
43
|
-
end #
|
30
|
+
end # Hash#find_only
|
44
31
|
|
45
32
|
|
46
|
-
describe "
|
33
|
+
describe "Hash#leaves" do
|
47
34
|
subject { {a: 1, b: 2, c: 3}.leaves }
|
48
35
|
|
49
36
|
it { is_expected.to eq( {[:a] => 1, [:b] => 2, [:c] => 3}) }
|
50
|
-
end #
|
37
|
+
end # Hash#leaves
|
51
38
|
|
52
39
|
|
53
|
-
describe "
|
40
|
+
describe "Hash#each_branch" do
|
54
41
|
let( :abc_hash ) { {a: 1, b: 2, c: 3} }
|
55
42
|
|
56
43
|
context "called with no block" do
|
@@ -74,4 +61,4 @@ describe "Refinement Hash#each_branch" do
|
|
74
61
|
it { is_expected.to eq(a: 1, b: 4, c: 9) }
|
75
62
|
end # called with a block
|
76
63
|
|
77
|
-
end #
|
64
|
+
end # Hash#each_branch
|
@@ -23,7 +23,7 @@ describe_class NRSER::AbstractMethodError do
|
|
23
23
|
NRSER::TestFixtures::AbstractMethodError::Base.new.f
|
24
24
|
}.to raise_error(
|
25
25
|
NRSER::AbstractMethodError,
|
26
|
-
/
|
26
|
+
/Method #f is abstract/
|
27
27
|
)
|
28
28
|
end
|
29
29
|
|
@@ -34,10 +34,17 @@ describe_class NRSER::AbstractMethodError do
|
|
34
34
|
it "explains that an implementing class needs to be found or written" do
|
35
35
|
expect {
|
36
36
|
NRSER::TestFixtures::AbstractMethodError::Sub.new.f
|
37
|
-
}.to raise_error
|
38
|
-
|
39
|
-
|
40
|
-
|
37
|
+
}.to raise_error NRSER::AbstractMethodError
|
38
|
+
|
39
|
+
|
40
|
+
message = begin
|
41
|
+
NRSER::TestFixtures::AbstractMethodError::Sub.new.f
|
42
|
+
rescue Exception => e
|
43
|
+
e.to_s
|
44
|
+
end
|
45
|
+
|
46
|
+
expect( message ).to match \
|
47
|
+
/find a subclass of NRSER::TestFixtures::AbstractMethodError::Sub to instantiate/
|
41
48
|
end
|
42
49
|
|
43
50
|
end # when raising method is invoked through instance of a subclass
|
@@ -9,14 +9,14 @@ describe "NRSER.each_branch" do
|
|
9
9
|
|
10
10
|
context "called without a block" do
|
11
11
|
context "called with arrays" do
|
12
|
-
|
12
|
+
describe_called_with( [] ) {
|
13
13
|
it {
|
14
14
|
is_expected.to be_a( Enumerator ).
|
15
15
|
and have_attributes size: 0
|
16
16
|
}
|
17
17
|
}
|
18
18
|
|
19
|
-
|
19
|
+
describe_called_with( [:x, :y, :z] ) {
|
20
20
|
it {
|
21
21
|
is_expected.to be_a( Enumerator ).
|
22
22
|
and have_attributes size: 3
|
@@ -25,7 +25,7 @@ describe "NRSER.each_branch" do
|
|
25
25
|
end # called with arrays
|
26
26
|
|
27
27
|
context "called with hashes" do
|
28
|
-
|
28
|
+
describe_called_with( {} ) {
|
29
29
|
it {
|
30
30
|
is_expected.to be_a( Enumerator ).
|
31
31
|
and have_attributes size: 0
|
@@ -13,8 +13,8 @@ describe "NRSER.transform" do
|
|
13
13
|
|
14
14
|
let( :tree ) {
|
15
15
|
{
|
16
|
-
x: :y.
|
17
|
-
y: :x.
|
16
|
+
x: :y.to_retriever,
|
17
|
+
y: :x.to_retriever,
|
18
18
|
}
|
19
19
|
} # let :tree
|
20
20
|
|
@@ -35,8 +35,8 @@ describe "NRSER.transform" do
|
|
35
35
|
let :tree do
|
36
36
|
{
|
37
37
|
users: {
|
38
|
-
{ id: :id.
|
39
|
-
name: :name.
|
38
|
+
{ id: :id.to_retriever } => {
|
39
|
+
name: :name.to_retriever,
|
40
40
|
}
|
41
41
|
}
|
42
42
|
}
|
@@ -65,8 +65,8 @@ describe "NRSER.transform" do
|
|
65
65
|
let :tree do
|
66
66
|
{
|
67
67
|
list: [
|
68
|
-
{ name: :name.
|
69
|
-
{ age: :age.
|
68
|
+
{ name: :name.to_retriever },
|
69
|
+
{ age: :age.to_retriever },
|
70
70
|
]
|
71
71
|
}
|
72
72
|
end
|
@@ -119,15 +119,15 @@ describe "NRSER.transform" do
|
|
119
119
|
let :tree do
|
120
120
|
{
|
121
121
|
users: {
|
122
|
-
{ contact_id: [ :parent, :id ].
|
122
|
+
{ contact_id: [ :parent, :id ].to_chainer } => {
|
123
123
|
addresses: {
|
124
|
-
{
|
125
|
-
address_id: [ :id ].
|
126
|
-
district: :address.
|
127
|
-
line_1: :street2.
|
128
|
-
city: :city.
|
129
|
-
province: :state.
|
130
|
-
post_code: :zip.
|
124
|
+
{
|
125
|
+
address_id: [ :id ].to_sender } => {
|
126
|
+
district: :address.to_retriever,
|
127
|
+
line_1: :street2.to_retriever,
|
128
|
+
city: :city.to_retriever,
|
129
|
+
province: :state.to_retriever,
|
130
|
+
post_code: :zip.to_retriever,
|
131
131
|
}
|
132
132
|
}
|
133
133
|
}
|
@@ -159,4 +159,3 @@ describe "NRSER.transform" do
|
|
159
159
|
|
160
160
|
|
161
161
|
end # NRSER.transform
|
162
|
-
|