nrser 0.0.17 → 0.0.18.dev
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nrser/array.rb +37 -0
- data/lib/nrser/enumerable.rb +31 -0
- data/lib/nrser/hash.rb +273 -0
- data/lib/nrser/refinements/array.rb +8 -0
- data/lib/nrser/refinements/hash.rb +51 -6
- data/lib/nrser/refinements/object.rb +26 -0
- data/lib/nrser/refinements.rb +5 -11
- data/lib/nrser/truthy.rb +72 -0
- data/lib/nrser/version.rb +1 -1
- data/lib/nrser.rb +7 -3
- data/spec/nrser/hash_spec.rb +24 -0
- data/spec/nrser/logger/dest_spec.rb +1 -0
- data/spec/nrser/refinements/array_spec.rb +13 -0
- data/spec/nrser/refinements/hash_spec.rb +13 -0
- data/spec/nrser/refinements/object_spec.rb +61 -0
- data/spec/nrser/truthy_spec.rb +75 -0
- metadata +20 -11
- data/.gitignore +0 -34
- data/.rspec +0 -2
- data/.travis.yml +0 -3
- data/Gemfile +0 -4
- data/Rakefile +0 -6
- data/nrser.gemspec +0 -25
- data/tmp/.gitkeep +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f563281d0ec3364841979db690432a92becdfa40
|
4
|
+
data.tar.gz: 7bd430b6f2d99cb4dc5b77f2e4f67abe1147e2f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '00837e3efd93fc72f8a4100d4fa8e42f90bd16b2a8e1458686416494400a326a961cc6c776ad368f8a1d4fd7d85b82d819c4d2bff304a010e49f16582d68cf98'
|
7
|
+
data.tar.gz: c55306b1959c308d7f5580afaf4c368011da81ca54935578489fed9c891df89db29940844a130cd8f26c6893e2be24c2a88c5f7fc99022365e7b17f44484d666
|
data/lib/nrser/array.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module NRSER
|
2
|
+
|
3
|
+
# Return an array given any value in the way that makes most sense:
|
4
|
+
#
|
5
|
+
# 1. If `value` is an array, return it.
|
6
|
+
#
|
7
|
+
# 2. If `value` is `nil`, return `[]`.
|
8
|
+
#
|
9
|
+
# 3. If `value` responds to `#to_a`, try calling it. If it succeeds, return
|
10
|
+
# that.
|
11
|
+
#
|
12
|
+
# 4. Return an array with `value` as it's only item.
|
13
|
+
#
|
14
|
+
# Refinement
|
15
|
+
# ----------
|
16
|
+
#
|
17
|
+
# Added to `Object` in `nrser/refinements`.
|
18
|
+
#
|
19
|
+
# @param [Object] value
|
20
|
+
#
|
21
|
+
# @return [Array]
|
22
|
+
#
|
23
|
+
def self.as_array value
|
24
|
+
return value if value.is_a? Array
|
25
|
+
return [] if value.nil?
|
26
|
+
|
27
|
+
if value.respond_to? :to_a
|
28
|
+
begin
|
29
|
+
return value.to_a
|
30
|
+
rescue
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
[value]
|
35
|
+
end # .as_array
|
36
|
+
|
37
|
+
end # module NRSER
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module NRSER
|
2
|
+
# Maps an enumerable object to a *new* hash with the same keys and values
|
3
|
+
# obtained by calling `block` with the current key and value.
|
4
|
+
#
|
5
|
+
# If `enumerable` *does not* sucessfully respond to `#to_h` then it's
|
6
|
+
# treated as a hash where it's elements are the keys and all the values
|
7
|
+
# are `nil`.
|
8
|
+
#
|
9
|
+
# @return [Hash]
|
10
|
+
#
|
11
|
+
def self.map_values enumerable, &block
|
12
|
+
# Short-cut for Hash itself - though it would of course work through the
|
13
|
+
# next step, it's going to probably be *the* most common argument type,
|
14
|
+
# and there's no reason to do the tests and set up the exception
|
15
|
+
# handler if we already know what's up with it.
|
16
|
+
return NRSER.map_hash_values(enumerable) if enumerable.is_a? Hash
|
17
|
+
|
18
|
+
if enumerable.respond_to? :to_h
|
19
|
+
begin
|
20
|
+
hash = enumerable.to_h
|
21
|
+
rescue TypeError => e
|
22
|
+
else
|
23
|
+
return NRSER.map_hash_values hash
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
result = {}
|
28
|
+
enumerable.each { |key| result[key] = block.call key, nil }
|
29
|
+
result
|
30
|
+
end
|
31
|
+
end # module NRSER
|
data/lib/nrser/hash.rb
ADDED
@@ -0,0 +1,273 @@
|
|
1
|
+
module NRSER
|
2
|
+
|
3
|
+
def self.leaves hash, path: [], results: {}
|
4
|
+
hash.each { |key, value|
|
5
|
+
new_path = [*path, key]
|
6
|
+
|
7
|
+
case value
|
8
|
+
when Hash
|
9
|
+
leaves value, path: new_path, results: results
|
10
|
+
else
|
11
|
+
results[new_path] = value
|
12
|
+
end
|
13
|
+
}
|
14
|
+
|
15
|
+
results
|
16
|
+
end # .leaves
|
17
|
+
|
18
|
+
|
19
|
+
# Treat the value as the value for `key` in a hash if it's not already a
|
20
|
+
# hash and can't be converted to one:
|
21
|
+
#
|
22
|
+
# 1. If the value is a `Hash`, return it.
|
23
|
+
#
|
24
|
+
# 2. If `value` is `nil`, return `{}`.
|
25
|
+
#
|
26
|
+
# 3. If the value responds to `#to_h` and `#to_h` succeeds, return the
|
27
|
+
# resulting hash.
|
28
|
+
#
|
29
|
+
# 4. Otherwise, return a new hash where `key` points to the value.
|
30
|
+
# **`key` MUST be provided in this case.**
|
31
|
+
#
|
32
|
+
# Useful in method overloading and similar situations where you expect a
|
33
|
+
# hash that may specify a host of options, but want to allow the method
|
34
|
+
# to be called with a single value that corresponds to a default key in that
|
35
|
+
# option hash.
|
36
|
+
#
|
37
|
+
# Refinement
|
38
|
+
# ----------
|
39
|
+
#
|
40
|
+
# Added to `Object` in `nrser/refinements`.
|
41
|
+
#
|
42
|
+
#
|
43
|
+
# Example Time!
|
44
|
+
# -------------
|
45
|
+
#
|
46
|
+
# Say you have a method `m` that handles a hash of HTML options that can
|
47
|
+
# look something like
|
48
|
+
#
|
49
|
+
# {class: 'address', data: {confirm: 'Really?'}}
|
50
|
+
#
|
51
|
+
# And can call `m` like
|
52
|
+
#
|
53
|
+
# m({class: 'address', data: {confirm: 'Really?'}})
|
54
|
+
#
|
55
|
+
# but often you are just dealing with the `:class` option. You can use
|
56
|
+
# {NRSER.as_hash} to accept a string and treat it as the `:class` key:
|
57
|
+
#
|
58
|
+
# using NRSER
|
59
|
+
#
|
60
|
+
# def m opts
|
61
|
+
# opts = opts.as_hash :class
|
62
|
+
# # ...
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# If you pass a hash, everything works normally, but if you pass a string
|
66
|
+
# `'address'` it will be converted to `{class: 'address'}`.
|
67
|
+
#
|
68
|
+
#
|
69
|
+
# About `#to_h` Support
|
70
|
+
# ---------------------
|
71
|
+
#
|
72
|
+
# Right now, {.as_hash} also tests if `value` responds to `#to_h`, and will
|
73
|
+
# try to call it, using the result if it doesn't raise. This lets it deal
|
74
|
+
# with Ruby's "I used to be a Hash until someone mapped me" values like
|
75
|
+
# `[[:class, 'address']]`. I'm not sure if this is the best approach, but
|
76
|
+
# I'm going to try it for now and see how it pans out in actual usage.
|
77
|
+
#
|
78
|
+
# @todo
|
79
|
+
# It might be nice to have a `check` option that ensures the resulting
|
80
|
+
# hash has a value for `key`.
|
81
|
+
#
|
82
|
+
# @param [Object] value
|
83
|
+
# The value that we want to be a hash.
|
84
|
+
#
|
85
|
+
# @param [Object] key [default nil]
|
86
|
+
# The key that `value` will be stored under in the result if `value` is
|
87
|
+
# not a hash or can't be turned into one via `#to_h`. If this happens
|
88
|
+
# this value can **NOT** be `nil` or an `ArgumentError` is raised.
|
89
|
+
#
|
90
|
+
# @return [Hash]
|
91
|
+
#
|
92
|
+
# @raise [ArgumentError]
|
93
|
+
# If it comes to constructing a new Hash with `value` as a value and no
|
94
|
+
# argument was provided
|
95
|
+
#
|
96
|
+
def self.as_hash value, key = nil
|
97
|
+
return value if value.is_a? Hash
|
98
|
+
return {} if value.nil?
|
99
|
+
|
100
|
+
if value.respond_to? :to_h
|
101
|
+
begin
|
102
|
+
return value.to_h
|
103
|
+
rescue
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# at this point we need a key argument
|
108
|
+
if key.nil?
|
109
|
+
raise ArgumentError,
|
110
|
+
"Need key to construct hash with value #{ value.inspect }, " +
|
111
|
+
"found nil."
|
112
|
+
end
|
113
|
+
|
114
|
+
{key => value}
|
115
|
+
end # .as_hash
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
# @todo Document map_hash_values method.
|
120
|
+
#
|
121
|
+
# @param [Hash] hash
|
122
|
+
# @todo Add name param description.
|
123
|
+
#
|
124
|
+
# @return [return_type]
|
125
|
+
# @todo Document return value.
|
126
|
+
#
|
127
|
+
def self.map_hash_values hash, &block
|
128
|
+
result = {}
|
129
|
+
hash.each { |key, value| result[key] = block.call key, value }
|
130
|
+
result
|
131
|
+
end # #map_hash_values
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
# Lifted from ActiveSupport
|
136
|
+
# =====================================================================
|
137
|
+
#
|
138
|
+
# Not sure *why* I didn't want to depend on ActiveSupport in the first place,
|
139
|
+
# but I'm guessing it's many other things depending on it and the potential
|
140
|
+
# for dependency hell, but anyways, I didn't, and I'm going to keep it that
|
141
|
+
# way for the moment.
|
142
|
+
#
|
143
|
+
# However, I do want some of that functionality, and I think it makes sense
|
144
|
+
# to keep the names and behaviors the same since ActiveSupport is so wide
|
145
|
+
# spread.
|
146
|
+
#
|
147
|
+
# The methods are modified to operate functionally since we use refinements
|
148
|
+
# instead of global monkey-patching, and Ruby versions before 2.1 (I think)
|
149
|
+
# don't support refinements, so these are useful in environments where you
|
150
|
+
# don't want to mess with the global built-ins and you don't have
|
151
|
+
# refinements available.
|
152
|
+
#
|
153
|
+
|
154
|
+
# Removes the given keys from hash and returns it.
|
155
|
+
#
|
156
|
+
# Lifted from ActiveSupport.
|
157
|
+
#
|
158
|
+
# @see http://www.rubydoc.info/gems/activesupport/5.1.3/Hash:except!
|
159
|
+
#
|
160
|
+
# @param [Hash] hash
|
161
|
+
# Hash to mutate.
|
162
|
+
#
|
163
|
+
# @return [Hash]
|
164
|
+
#
|
165
|
+
def self.except_keys! hash, *keys
|
166
|
+
keys.each { |key| hash.delete(key) }
|
167
|
+
hash
|
168
|
+
end
|
169
|
+
|
170
|
+
singleton_class.send :alias_method, :omit_keys!, :except_keys!
|
171
|
+
|
172
|
+
|
173
|
+
# Returns a new hash without `keys`.
|
174
|
+
#
|
175
|
+
# Lifted from ActiveSupport.
|
176
|
+
#
|
177
|
+
# @see http://www.rubydoc.info/gems/activesupport/5.1.3/Hash:except
|
178
|
+
#
|
179
|
+
# @param [Hash] hash
|
180
|
+
# Source hash.
|
181
|
+
#
|
182
|
+
# @return [Hash]
|
183
|
+
#
|
184
|
+
def self.except_keys hash, *keys
|
185
|
+
except_keys! hash.dup, *keys
|
186
|
+
end
|
187
|
+
|
188
|
+
singleton_class.send :alias_method, :omit_keys, :except_keys
|
189
|
+
|
190
|
+
|
191
|
+
# Lifted from ActiveSupport.
|
192
|
+
#
|
193
|
+
# @see http://www.rubydoc.info/gems/activesupport/5.1.3/Hash:transform_keys!
|
194
|
+
#
|
195
|
+
# @param [Hash] hash
|
196
|
+
# Hash to mutate keys.
|
197
|
+
#
|
198
|
+
# @return [Hash]
|
199
|
+
# The mutated hash.
|
200
|
+
#
|
201
|
+
def self.transform_keys! hash
|
202
|
+
# File 'lib/active_support/core_ext/hash/keys.rb', line 23
|
203
|
+
hash.keys.each do |key|
|
204
|
+
hash[yield(key)] = hash.delete(key)
|
205
|
+
end
|
206
|
+
hash
|
207
|
+
end
|
208
|
+
|
209
|
+
|
210
|
+
# Returns a new hash with each key transformed by the provided block.
|
211
|
+
#
|
212
|
+
# Lifted from ActiveSupport.
|
213
|
+
#
|
214
|
+
# @see http://www.rubydoc.info/gems/activesupport/5.1.3/Hash:transform_keys
|
215
|
+
#
|
216
|
+
# @param [Hash] hash
|
217
|
+
#
|
218
|
+
# @return [Hash]
|
219
|
+
# New hash with transformed keys.
|
220
|
+
#
|
221
|
+
def self.transform_keys hash, &block
|
222
|
+
# File 'lib/active_support/core_ext/hash/keys.rb', line 12
|
223
|
+
result = {}
|
224
|
+
hash.each_key do |key|
|
225
|
+
result[yield(key)] = hash[key]
|
226
|
+
end
|
227
|
+
result
|
228
|
+
end
|
229
|
+
|
230
|
+
# My-style name
|
231
|
+
singleton_class.send :alias_method, :map_hash_keys, :transform_keys
|
232
|
+
|
233
|
+
|
234
|
+
# Mutates `hash` by converting all keys that respond to `#to_sym` to symbols.
|
235
|
+
#
|
236
|
+
# Lifted from ActiveSupport.
|
237
|
+
#
|
238
|
+
# @see http://www.rubydoc.info/gems/activesupport/5.1.3/Hash:symbolize_keys!
|
239
|
+
#
|
240
|
+
# @param [Hash] hash
|
241
|
+
# Hash to mutate.
|
242
|
+
#
|
243
|
+
# @return [return_type]
|
244
|
+
# @todo Document return value.
|
245
|
+
#
|
246
|
+
def self.symbolize_keys! hash
|
247
|
+
transform_keys!(hash) { |key| key.to_sym rescue key }
|
248
|
+
end # .symbolize_keys!
|
249
|
+
|
250
|
+
|
251
|
+
|
252
|
+
def self.symbolize_keys hash
|
253
|
+
# File 'lib/active_support/core_ext/hash/keys.rb', line 54
|
254
|
+
transform_keys(hash) { |key| key.to_sym rescue key }
|
255
|
+
end
|
256
|
+
|
257
|
+
# Lifted from ActiveSupport.
|
258
|
+
#
|
259
|
+
# @see http://www.rubydoc.info/gems/activesupport/5.1.3/Hash:slice
|
260
|
+
#
|
261
|
+
#
|
262
|
+
def self.slice_keys hash, *keys
|
263
|
+
# We're not using this, but, whatever, leave it in...
|
264
|
+
if hash.respond_to?(:convert_key, true)
|
265
|
+
keys.map! { |key| hash.convert_key(key) }
|
266
|
+
end
|
267
|
+
|
268
|
+
keys.each_with_object(hash.class.new) { |k, new_hash|
|
269
|
+
new_hash[k] = hash[k] if hash.has_key?(k)
|
270
|
+
}
|
271
|
+
end
|
272
|
+
|
273
|
+
end # module NRSER
|
@@ -1,18 +1,63 @@
|
|
1
1
|
module NRSER
|
2
|
-
refine Hash do
|
3
|
-
#
|
2
|
+
refine ::Hash do
|
3
|
+
# See {NRSER.except_keys!}.
|
4
4
|
def except! *keys
|
5
|
-
|
6
|
-
self
|
5
|
+
NRSER.except_keys! self, *keys
|
7
6
|
end
|
8
7
|
|
9
8
|
alias_method :omit!, :except!
|
10
9
|
|
11
|
-
|
10
|
+
|
11
|
+
# See {NRSER.except_keys}.
|
12
12
|
def except *keys
|
13
|
-
|
13
|
+
NRSER.except_keys self, *keys
|
14
14
|
end
|
15
15
|
|
16
16
|
alias_method :omit, :except
|
17
|
+
|
18
|
+
|
19
|
+
# See {NRSER.slice_keys}.
|
20
|
+
def slice *keys
|
21
|
+
NRSER.slice_keys self, *keys
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
# See {NRSER.leaves}.
|
26
|
+
def leaves
|
27
|
+
NRSER.leaves self
|
28
|
+
end # #leaves
|
29
|
+
|
30
|
+
|
31
|
+
# See {NRSER.map_hash_values}.
|
32
|
+
def map_values &block
|
33
|
+
NRSER.map_hash_values self, &block
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
def transform_keys! &block
|
38
|
+
return enum_for(:transform_keys!) { size } unless block_given?
|
39
|
+
NRSER.transform_keys! self, &block
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
def transform_keys &block
|
44
|
+
return hash.enum_for(:transform_keys) { size } unless block_given?
|
45
|
+
NRSER.transform_keys self, &block
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def symbolize_keys!
|
50
|
+
NRSER.symbolize_keys! self
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
def symbolize_keys
|
55
|
+
NRSER.symbolize_keys self
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
def map_keys &block
|
60
|
+
NRSER.map_hash_keys self, &block
|
61
|
+
end
|
17
62
|
end # Hash
|
18
63
|
end # NRSER
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module NRSER
|
2
|
+
refine Object do
|
3
|
+
def pipe
|
4
|
+
yield self
|
5
|
+
end
|
6
|
+
|
7
|
+
def truthy?
|
8
|
+
NRSER.truthy? self
|
9
|
+
end
|
10
|
+
|
11
|
+
def falsy?
|
12
|
+
NRSER.falsy? self
|
13
|
+
end
|
14
|
+
|
15
|
+
# Calls {NRSER.as_hash} on `self` with the provided `key`.
|
16
|
+
#
|
17
|
+
def as_hash key = nil
|
18
|
+
NRSER.as_hash self, key
|
19
|
+
end
|
20
|
+
|
21
|
+
# Calls {NRSER.as_array} in `self`.
|
22
|
+
def as_array
|
23
|
+
NRSER.as_array self
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end # NRSER
|
data/lib/nrser/refinements.rb
CHANGED
@@ -1,16 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require_relative 'refinements/
|
4
|
-
require_relative 'refinements/hash'
|
5
|
-
require_relative 'refinements/pathname'
|
1
|
+
require_relative './refinements/object'
|
2
|
+
require_relative './refinements/string'
|
3
|
+
require_relative './refinements/array'
|
4
|
+
require_relative './refinements/hash'
|
5
|
+
require_relative './refinements/pathname'
|
6
6
|
|
7
7
|
module NRSER
|
8
|
-
refine Object do
|
9
|
-
def pipe
|
10
|
-
yield self
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
8
|
refine Exception do
|
15
9
|
def format
|
16
10
|
NRSER.format_exception self
|
data/lib/nrser/truthy.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module NRSER
|
4
|
+
# Down-cased versions of strings that are considered to communicate truth.
|
5
|
+
TRUTHY_STRINGS = Set.new [
|
6
|
+
'true',
|
7
|
+
't',
|
8
|
+
'yes',
|
9
|
+
'y',
|
10
|
+
'on',
|
11
|
+
'1',
|
12
|
+
]
|
13
|
+
|
14
|
+
# Down-cased versions of strings that are considered to communicate false.
|
15
|
+
FALSY_STRINGS = Set.new [
|
16
|
+
'false',
|
17
|
+
'f',
|
18
|
+
'no',
|
19
|
+
'n',
|
20
|
+
'off',
|
21
|
+
'0',
|
22
|
+
'',
|
23
|
+
]
|
24
|
+
|
25
|
+
# Evaluate an object (that probably came from outside Ruby, like an
|
26
|
+
# environment variable) to see if it's meant to represent true or false.
|
27
|
+
#
|
28
|
+
# @param [Nil, String] object
|
29
|
+
# Value to test.
|
30
|
+
#
|
31
|
+
# @return [Boolean]
|
32
|
+
# `true` if the object is "truthy".
|
33
|
+
#
|
34
|
+
def self.truthy? object
|
35
|
+
case object
|
36
|
+
when nil
|
37
|
+
false
|
38
|
+
|
39
|
+
when String
|
40
|
+
downcased = object.downcase
|
41
|
+
|
42
|
+
if TRUTHY_STRINGS.include? downcased
|
43
|
+
true
|
44
|
+
elsif FALSY_STRINGS.include? downcased
|
45
|
+
false
|
46
|
+
else
|
47
|
+
raise ArgumentError,
|
48
|
+
"String #{ object.inspect } not recognized as true or false."
|
49
|
+
end
|
50
|
+
|
51
|
+
when TrueClass, FalseClass
|
52
|
+
object
|
53
|
+
|
54
|
+
else
|
55
|
+
raise TypeError,
|
56
|
+
"Can't evaluate truthiness of #{ object.inspect }"
|
57
|
+
end
|
58
|
+
end # .truthy?
|
59
|
+
|
60
|
+
|
61
|
+
# Opposite of {NRSER.truthy?}.
|
62
|
+
#
|
63
|
+
# @param object (see .truthy?)
|
64
|
+
#
|
65
|
+
# @return [Boolean]
|
66
|
+
# The negation of {NRSER.truthy?}.
|
67
|
+
#
|
68
|
+
def self.falsy? object
|
69
|
+
! truthy?(object)
|
70
|
+
end # .falsy?
|
71
|
+
|
72
|
+
end # module NRSER
|
data/lib/nrser/version.rb
CHANGED
data/lib/nrser.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require_relative './nrser/version'
|
2
|
+
require_relative './nrser/collection'
|
3
|
+
require_relative './nrser/truthy'
|
3
4
|
|
4
5
|
module NRSER
|
5
6
|
class << self
|
@@ -115,4 +116,7 @@ module NRSER
|
|
115
116
|
end # class << self
|
116
117
|
end
|
117
118
|
|
118
|
-
|
119
|
+
require_relative './nrser/enumerable'
|
120
|
+
require_relative './nrser/hash'
|
121
|
+
require_relative './nrser/array'
|
122
|
+
require_relative "./nrser/types"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
using NRSER
|
4
|
+
|
5
|
+
describe NRSER.method(:truncate) do
|
6
|
+
it do
|
7
|
+
expect(NRSER.leaves({a: 1, b: 2})).to eq ({[:a] => 1, [:b] => 2})
|
8
|
+
expect(
|
9
|
+
NRSER.leaves({
|
10
|
+
a: {
|
11
|
+
x: 'ex',
|
12
|
+
y: {
|
13
|
+
z: 'zee'
|
14
|
+
}
|
15
|
+
},
|
16
|
+
b: 'bee',
|
17
|
+
})
|
18
|
+
).to eq({
|
19
|
+
[:a, :x] => 'ex',
|
20
|
+
[:a, :y, :z] => 'zee',
|
21
|
+
[:b] => 'bee',
|
22
|
+
})
|
23
|
+
end
|
24
|
+
end # truncate
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
using NRSER
|
4
|
+
|
5
|
+
describe "Refinements for Object" do
|
6
|
+
|
7
|
+
describe '#as_hash' do
|
8
|
+
context "self is a Hash" do
|
9
|
+
it "returns self" do
|
10
|
+
h = {a: 1}
|
11
|
+
expect(h.as_hash).to be h
|
12
|
+
# key doesn't matter
|
13
|
+
expect(h.as_hash(:x)).to be h
|
14
|
+
end # returns itself when self is a hash
|
15
|
+
end # self is a Hash
|
16
|
+
|
17
|
+
context "self is nil" do
|
18
|
+
it "returns {}" do
|
19
|
+
expect(nil.as_hash).to eq({})
|
20
|
+
end # returns {}
|
21
|
+
end # self is nil
|
22
|
+
|
23
|
+
context "self responds to #to_h" do
|
24
|
+
|
25
|
+
context "#to_h succeeds" do
|
26
|
+
it "returns result of #to_h" do
|
27
|
+
expect([[:a, 1], [:b, 2]].as_hash).to eq({a: 1, b: 2})
|
28
|
+
end # returns result of #to_h
|
29
|
+
end # #to_h succeeds
|
30
|
+
|
31
|
+
context "#to_h fails" do
|
32
|
+
it "returns hash with self keyed as `key`" do
|
33
|
+
expect([1, 2, 3].as_hash(:a)).to eq({a: [1, 2, 3]})
|
34
|
+
end # returns hash with self keyed as `key`
|
35
|
+
|
36
|
+
context "no key provided" do
|
37
|
+
it "raises ArgumentError" do
|
38
|
+
expect { [1, 2, 3].as_hash }.to raise_error ArgumentError
|
39
|
+
end # raises ArgumentErrpr
|
40
|
+
end # no key provided
|
41
|
+
it "raises ArgumentErrpr" do
|
42
|
+
|
43
|
+
end # raises ArgumentErrpr
|
44
|
+
end # #to_h failsexpect { [1, 2, 3].as_hash }.to raise_error ArgumentError
|
45
|
+
|
46
|
+
end # self responds to #to_h
|
47
|
+
|
48
|
+
end # #as_hash
|
49
|
+
|
50
|
+
|
51
|
+
describe '#as_array' do
|
52
|
+
context "self is nil" do
|
53
|
+
it "returns {}" do
|
54
|
+
expect(nil.as_array).to eq([])
|
55
|
+
end # returns {}
|
56
|
+
end # self is nil
|
57
|
+
|
58
|
+
end # #as_array
|
59
|
+
|
60
|
+
|
61
|
+
end # Refinements for Object
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
using NRSER
|
4
|
+
|
5
|
+
truthy_strings = [
|
6
|
+
'true', 'True', 'TRUE',
|
7
|
+
'T', 't',
|
8
|
+
'YES', 'yes', 'Yes',
|
9
|
+
'Y', 'y',
|
10
|
+
'ON', 'On', 'on',
|
11
|
+
'1',
|
12
|
+
]
|
13
|
+
|
14
|
+
falsy_strings = [
|
15
|
+
'false', 'False', 'FALSE',
|
16
|
+
'F', 'f',
|
17
|
+
'NO', 'no',
|
18
|
+
'N', 'n',
|
19
|
+
'OFF', 'Off', 'off',
|
20
|
+
'0',
|
21
|
+
'',
|
22
|
+
]
|
23
|
+
|
24
|
+
undecidable_strings = [
|
25
|
+
'blah!',
|
26
|
+
]
|
27
|
+
|
28
|
+
describe NRSER.method(:truthy?) do
|
29
|
+
context "strings" do
|
30
|
+
context "true truthy strings" do
|
31
|
+
truthy_strings.each do |string|
|
32
|
+
it "recognizes string #{ string } as truthy" do
|
33
|
+
expect(subject.call string).to be true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end # true truthy strings
|
37
|
+
|
38
|
+
context "false truthy strings" do
|
39
|
+
truthy_strings.each do |string|
|
40
|
+
it "recognizes string #{ string } as truthy" do
|
41
|
+
expect(subject.call string).to be true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end # false truthy strings
|
45
|
+
|
46
|
+
context "undecidable truthy strings" do
|
47
|
+
undecidable_strings.each do |string|
|
48
|
+
it "errors on #{ string }" do
|
49
|
+
expect{subject.call string}.to raise_error ArgumentError
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end # undecidable truthy strings
|
53
|
+
end # strings
|
54
|
+
|
55
|
+
context "refinement" do
|
56
|
+
context "strings" do
|
57
|
+
it "recognizes an empty ENV var as falsy" do
|
58
|
+
expect(ENV['sdfaarfsg'].truthy?).to be false
|
59
|
+
end
|
60
|
+
|
61
|
+
it "recognizes nil as falsey" do
|
62
|
+
expect(nil.truthy?).to be false
|
63
|
+
end
|
64
|
+
|
65
|
+
context "true truthy strings" do
|
66
|
+
truthy_strings.each do |string|
|
67
|
+
it "recognizes string #{ string } as truthy" do
|
68
|
+
expect(string.truthy?).to be true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end # true truthy strings
|
72
|
+
end # strings
|
73
|
+
end # refinement
|
74
|
+
|
75
|
+
end # NRSER.truthy?
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nrser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.18.dev
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nrser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -87,20 +87,21 @@ executables: []
|
|
87
87
|
extensions: []
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
|
-
- ".gitignore"
|
91
|
-
- ".rspec"
|
92
|
-
- ".travis.yml"
|
93
|
-
- Gemfile
|
94
90
|
- LICENSE.txt
|
95
91
|
- README.md
|
96
|
-
- Rakefile
|
97
92
|
- lib/nrser.rb
|
93
|
+
- lib/nrser/array.rb
|
98
94
|
- lib/nrser/collection.rb
|
95
|
+
- lib/nrser/enumerable.rb
|
96
|
+
- lib/nrser/hash.rb
|
99
97
|
- lib/nrser/logger.rb
|
100
98
|
- lib/nrser/refinements.rb
|
99
|
+
- lib/nrser/refinements/array.rb
|
101
100
|
- lib/nrser/refinements/hash.rb
|
101
|
+
- lib/nrser/refinements/object.rb
|
102
102
|
- lib/nrser/refinements/pathname.rb
|
103
103
|
- lib/nrser/refinements/string.rb
|
104
|
+
- lib/nrser/truthy.rb
|
104
105
|
- lib/nrser/types.rb
|
105
106
|
- lib/nrser/types/any.rb
|
106
107
|
- lib/nrser/types/array.rb
|
@@ -117,12 +118,12 @@ files:
|
|
117
118
|
- lib/nrser/types/type.rb
|
118
119
|
- lib/nrser/types/where.rb
|
119
120
|
- lib/nrser/version.rb
|
120
|
-
- nrser.gemspec
|
121
121
|
- spec/nrser/collection/each_spec.rb
|
122
122
|
- spec/nrser/collection/map_spec.rb
|
123
123
|
- spec/nrser/common_prefix_spec.rb
|
124
124
|
- spec/nrser/dedent_spec.rb
|
125
125
|
- spec/nrser/format_exception_spec.rb
|
126
|
+
- spec/nrser/hash_spec.rb
|
126
127
|
- spec/nrser/indent_spec.rb
|
127
128
|
- spec/nrser/logger/dest_spec.rb
|
128
129
|
- spec/nrser/logger/die_spec.rb
|
@@ -132,19 +133,22 @@ files:
|
|
132
133
|
- spec/nrser/logger/level_sym_spec.rb
|
133
134
|
- spec/nrser/logger/send_log_spec.rb
|
134
135
|
- spec/nrser/logger/use_spec.rb
|
136
|
+
- spec/nrser/refinements/array_spec.rb
|
135
137
|
- spec/nrser/refinements/erb_spec.rb
|
136
138
|
- spec/nrser/refinements/format_exception_spec.rb
|
139
|
+
- spec/nrser/refinements/hash_spec.rb
|
137
140
|
- spec/nrser/refinements/indent_spec.rb
|
141
|
+
- spec/nrser/refinements/object_spec.rb
|
138
142
|
- spec/nrser/refinements/pathname_spec.rb
|
139
143
|
- spec/nrser/refinements/truncate_spec.rb
|
140
144
|
- spec/nrser/template_spec.rb
|
141
145
|
- spec/nrser/truncate_spec.rb
|
146
|
+
- spec/nrser/truthy_spec.rb
|
142
147
|
- spec/nrser/types/combinators_spec.rb
|
143
148
|
- spec/nrser/types/is_spec.rb
|
144
149
|
- spec/nrser/types_spec.rb
|
145
150
|
- spec/nrser_spec.rb
|
146
151
|
- spec/spec_helper.rb
|
147
|
-
- tmp/.gitkeep
|
148
152
|
homepage: https://github.com/nrser/nrser-ruby
|
149
153
|
licenses:
|
150
154
|
- MIT
|
@@ -160,9 +164,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
160
164
|
version: '0'
|
161
165
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
162
166
|
requirements:
|
163
|
-
- - "
|
167
|
+
- - ">"
|
164
168
|
- !ruby/object:Gem::Version
|
165
|
-
version:
|
169
|
+
version: 1.3.1
|
166
170
|
requirements: []
|
167
171
|
rubyforge_project:
|
168
172
|
rubygems_version: 2.6.11
|
@@ -175,6 +179,7 @@ test_files:
|
|
175
179
|
- spec/nrser/common_prefix_spec.rb
|
176
180
|
- spec/nrser/dedent_spec.rb
|
177
181
|
- spec/nrser/format_exception_spec.rb
|
182
|
+
- spec/nrser/hash_spec.rb
|
178
183
|
- spec/nrser/indent_spec.rb
|
179
184
|
- spec/nrser/logger/dest_spec.rb
|
180
185
|
- spec/nrser/logger/die_spec.rb
|
@@ -184,13 +189,17 @@ test_files:
|
|
184
189
|
- spec/nrser/logger/level_sym_spec.rb
|
185
190
|
- spec/nrser/logger/send_log_spec.rb
|
186
191
|
- spec/nrser/logger/use_spec.rb
|
192
|
+
- spec/nrser/refinements/array_spec.rb
|
187
193
|
- spec/nrser/refinements/erb_spec.rb
|
188
194
|
- spec/nrser/refinements/format_exception_spec.rb
|
195
|
+
- spec/nrser/refinements/hash_spec.rb
|
189
196
|
- spec/nrser/refinements/indent_spec.rb
|
197
|
+
- spec/nrser/refinements/object_spec.rb
|
190
198
|
- spec/nrser/refinements/pathname_spec.rb
|
191
199
|
- spec/nrser/refinements/truncate_spec.rb
|
192
200
|
- spec/nrser/template_spec.rb
|
193
201
|
- spec/nrser/truncate_spec.rb
|
202
|
+
- spec/nrser/truthy_spec.rb
|
194
203
|
- spec/nrser/types/combinators_spec.rb
|
195
204
|
- spec/nrser/types/is_spec.rb
|
196
205
|
- spec/nrser/types_spec.rb
|
data/.gitignore
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
*.gem
|
2
|
-
*.rbc
|
3
|
-
.bundle
|
4
|
-
.config
|
5
|
-
.yardoc
|
6
|
-
Gemfile.lock
|
7
|
-
InstalledFiles
|
8
|
-
_yardoc
|
9
|
-
coverage
|
10
|
-
doc/
|
11
|
-
lib/bundler/man
|
12
|
-
pkg
|
13
|
-
rdoc
|
14
|
-
spec/reports
|
15
|
-
test/tmp
|
16
|
-
test/version_tmp
|
17
|
-
tmp
|
18
|
-
*.bundle
|
19
|
-
*.so
|
20
|
-
*.o
|
21
|
-
*.a
|
22
|
-
mkmf.log
|
23
|
-
tmp/
|
24
|
-
##############################################################################
|
25
|
-
# BEGIN Qb.gitingore
|
26
|
-
#
|
27
|
-
# generated qb playbooks
|
28
|
-
.qb-playbook.yml
|
29
|
-
|
30
|
-
# ansible retry files
|
31
|
-
.qb-playbook.retry
|
32
|
-
#
|
33
|
-
# END Qb.gitingore
|
34
|
-
##############################################################################
|
data/.rspec
DELETED
data/.travis.yml
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
data/nrser.gemspec
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'nrser/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "nrser"
|
8
|
-
spec.version = NRSER::VERSION
|
9
|
-
spec.authors = ["nrser"]
|
10
|
-
spec.email = ["neil@neilsouza.com"]
|
11
|
-
spec.summary = %q{basic ruby utils i use in a lot of stuff.}
|
12
|
-
spec.homepage = "https://github.com/nrser/nrser-ruby"
|
13
|
-
spec.license = "MIT"
|
14
|
-
|
15
|
-
spec.files = `git ls-files -z`.split("\x0")
|
16
|
-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
-
spec.require_paths = ["lib"]
|
19
|
-
|
20
|
-
spec.add_development_dependency "bundler", "~> 1.5"
|
21
|
-
spec.add_development_dependency "rake"
|
22
|
-
spec.add_development_dependency "rspec"
|
23
|
-
spec.add_development_dependency "yard"
|
24
|
-
spec.add_development_dependency "cmds"
|
25
|
-
end
|
data/tmp/.gitkeep
DELETED
File without changes
|