nrser 0.2.0.pre.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/lib/nrser/ext/enumerable.rb +12 -3
  3. data/lib/nrser/ext/module.rb +62 -0
  4. data/lib/nrser/ext.rb +1 -0
  5. data/lib/nrser/functions/binding.rb +33 -0
  6. data/lib/nrser/functions/enumerable/associate.rb +103 -0
  7. data/lib/nrser/functions/enumerable/map_keys.rb +0 -0
  8. data/lib/nrser/functions/enumerable/map_values.rb +94 -0
  9. data/lib/nrser/functions/enumerable.rb +2 -87
  10. data/lib/nrser/functions/module/methods.rb +206 -0
  11. data/lib/nrser/functions/module/source_locations.rb +213 -0
  12. data/lib/nrser/functions/module.rb +2 -0
  13. data/lib/nrser/functions.rb +1 -0
  14. data/lib/nrser/logging/appender/sync.rb +148 -0
  15. data/lib/nrser/logging/appender.rb +3 -0
  16. data/lib/nrser/logging/formatters/color.rb +165 -0
  17. data/lib/nrser/logging/formatters.rb +1 -0
  18. data/lib/nrser/logging.rb +353 -0
  19. data/lib/nrser/refinements/module.rb +5 -0
  20. data/lib/nrser/refinements.rb +1 -0
  21. data/lib/nrser/rspex/described.rb +99 -0
  22. data/lib/nrser/rspex/example_group/describe_called_with.rb +2 -2
  23. data/lib/nrser/rspex/example_group/describe_class.rb +31 -0
  24. data/lib/nrser/rspex/example_group/describe_instance.rb +1 -1
  25. data/lib/nrser/rspex/example_group/describe_method.rb +40 -0
  26. data/lib/nrser/rspex/example_group.rb +2 -34
  27. data/lib/nrser/rspex/format.rb +19 -6
  28. data/lib/nrser/rspex.rb +1 -1
  29. data/lib/nrser/types/numbers.rb +16 -16
  30. data/lib/nrser/version.rb +1 -1
  31. data/lib/nrser.rb +2 -5
  32. data/spec/design/mapping_spec.rb +42 -0
  33. data/spec/lib/nrser/mean_streak/identity_instance_spec.rb +7 -5
  34. data/spec/spec_helper.rb +23 -105
  35. data/spec/support/shared/types.rb +92 -0
  36. data/spec/support/shared.rb +1 -0
  37. metadata +27 -24
  38. data/lib/nrser/labs/unicode_math.rb +0 -48
  39. data/lib/nrser/labs/where.rb +0 -50
  40. data/lib/nrser/logger.rb +0 -457
  41. data/spec/lib/nrser/logger/dest_spec.rb +0 -15
  42. data/spec/lib/nrser/logger/die_spec.rb +0 -41
  43. data/spec/lib/nrser/logger/install_spec.rb +0 -98
  44. data/spec/lib/nrser/logger/level_int_spec.rb +0 -22
  45. data/spec/lib/nrser/logger/level_name_spec.rb +0 -23
  46. data/spec/lib/nrser/logger/level_sym_spec.rb +0 -22
  47. data/spec/lib/nrser/logger/send_log_spec.rb +0 -63
  48. data/spec/lib/nrser/logger/use_spec.rb +0 -16
data/lib/nrser/logger.rb DELETED
@@ -1,457 +0,0 @@
1
- # Refinements
2
- # =======================================================================
3
-
4
- using NRSER
5
-
6
-
7
- # Definitions
8
- # =======================================================================
9
-
10
- module NRSER
11
-
12
- # @todo doc class
13
- class Logger
14
- # constants
15
- # =========
16
-
17
- # symbols for the level names
18
- LEVEL_SYMS = [
19
- :debug,
20
- :info,
21
- :warn,
22
- :error,
23
- :fatal,
24
- :unknown,
25
- ]
26
-
27
- LOGGING_METHODS = LEVEL_SYMS - [:unknown] + [:die]
28
-
29
- SEVERITY_COLORS = {
30
- 'DEBUG' => :bright_black,
31
- 'WARN' => :yellow,
32
- 'INFO' => :green,
33
- 'ERROR' => :red,
34
- 'FATAL' => :on_red,
35
- }
36
-
37
- # class variables
38
- # ===============
39
-
40
- # [Pastel, false] if pastel is present, this will be an instance of
41
- # `Pastel` otherwise, it will be false
42
- @@pastel = begin
43
- require 'pastel'
44
- Pastel.new
45
- rescue LoadError => e
46
- false
47
- end
48
-
49
- # [Hash<IO, ::Logger>] map of IO instances (files, STDOUT, STDERR, etc.)
50
- # to Ruby stdlib `Logger` instances that handle the actual writing to
51
- # that destination.
52
- @ruby_loggers = {}
53
-
54
- # class functions
55
- # ==============
56
-
57
- # @api util
58
- # *pure*
59
- #
60
- # format a debug message with optional key / values to print
61
- #
62
- # @param name [String] logger name.
63
- # @param level [String, Symbol, Fixnum] the level in string, symbol or
64
- # integer form.
65
- # @param msg [String] message to print.
66
- # @param dump [Hash] optional hash of keys and vaues to dump.
67
- def self.format name, level, msg, dump = {}
68
- data = {
69
- 'logger' => name,
70
- 'time' => Time.now,
71
- }
72
-
73
- data['msg'] = msg unless msg.empty?
74
- data['values'] = dump_value(dump) unless dump.empty?
75
-
76
- YAML.dump level_name(level) => data
77
- end
78
- # prev:
79
- # def self.format msg, dump = {}
80
- # unless dump.empty?
81
- # msg += "\n" + dump.map {|k, v| " #{ k }: #{ v.inspect }" }.join("\n")
82
- # end
83
- # msg
84
- # end
85
-
86
- def self.dump_value value
87
- case value
88
- when String, Fixnum, Float, TrueClass, FalseClass
89
- value
90
- when Array
91
- value.map {|v| dump_value v}
92
- when Hash
93
- Hash[value.map {|k, v| [k.to_s, dump_value(v)]}]
94
- else
95
- value.pretty_inspect
96
- end
97
- end
98
-
99
- # @api util
100
- #
101
- #
102
- def self.check_level level
103
- case level
104
- when Fixnum
105
- unless level >= 0 && level < LEVEL_SYMS.length
106
- raise ArgumentError.new "invalid integer level: #{ level.inspect }"
107
- end
108
- when Symbol
109
- unless LEVEL_SYMS.include? level
110
- raise ArgumentError.new "invalid level symbol: #{ level.inspect }"
111
- end
112
- when String
113
- unless LEVEL_SYMS.map {|_| _.to_s.upcase}.include? level
114
- raise ArgumentError.new "invalid level name: #{ level.inspect }"
115
- end
116
- else
117
- raise TypeError.new binding.erb <<-END
118
- level must be Fixnum, Symbol or String, not <%= level.inspect %>
119
- END
120
- end
121
- end # #check_level
122
-
123
- # @api util
124
- # *pure*
125
- #
126
- # get the integer value of a level (like ::Logger::DEBUG, etc.).
127
- #
128
- # @param level [Fixnum, Symbol, String] the integer level, method symbol,
129
- # or string name (all caps).
130
- #
131
- # @return [Fixnum] level integer (between 0 and 5 inclusive).
132
- #
133
- def self.level_int level
134
- check_level level
135
- case level
136
- when Fixnum
137
- level
138
- when Symbol
139
- LEVEL_SYMS.each_with_index {|sym, index|
140
- return index if level == sym
141
- }
142
- when String
143
- LEVEL_SYMS.each_with_index {|sym, index|
144
- return index if level == sym.to_s.upcase
145
- }
146
- end
147
- end
148
-
149
- # @api util
150
- # *pure*
151
- #
152
- # get the string "name" of a level ('DEBUG', 'INFO', etc.).
153
- #
154
- # @param level [Fixnum, Symbol, String] the integer level, method symbol,
155
- # or string name (all caps).
156
- #
157
- # @return ['DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL', 'UNKNOWN']
158
- #
159
- def self.level_name level
160
- check_level level
161
- case level
162
- when Fixnum
163
- LEVEL_SYMS[level].to_s.upcase
164
- when Symbol
165
- level.to_s.upcase
166
- when String
167
- level
168
- end
169
- end
170
-
171
- # @api util
172
- # *pure*
173
- #
174
- # get the symbol for a level as used in method sigs.
175
- #
176
- # @param level [Fixnum, Symbol, String] the integer level, method symbol,
177
- # or string name (all caps).
178
- #
179
- # @return [:debug, :info, :warn, :error, :fatal, :unknown]
180
- #
181
- def self.level_sym level
182
- check_level level
183
- case level
184
- when Fixnum
185
- LEVEL_SYMS[level]
186
- when Symbol
187
- level
188
- when String
189
- level.downcase.to_sym
190
- end
191
- end
192
-
193
- # @api util
194
- # *pure*
195
- #
196
- # creates methods closed around a `NRSER::Logger` instance to be attached
197
- # to objects to access the logger and do logging and 'installs' them on
198
- # the target.
199
- #
200
- # @param target [Object] object to install methods on.
201
- #
202
- # @param logger [NRSER::Logger] the logger to bind the methods to.
203
- #
204
- # @return nil
205
- #
206
- def self.install_methods! target, logger
207
- methods = {
208
- logger: {
209
- private: false,
210
- body: ->() { logger },
211
- },
212
- }
213
-
214
- LOGGING_METHODS.each do |sym|
215
- methods[sym] = {
216
- private: true,
217
- body: ->(*args, &block) { logger.send sym, *args, &block },
218
- }
219
- end
220
-
221
- if target.is_a? Class
222
- methods.each do |sym, stuff|
223
- target.define_singleton_method sym, &stuff[:body]
224
- target.send :define_method, sym, &stuff[:body]
225
- target.send :private, sym if stuff[:private]
226
- end
227
-
228
- elsif target.is_a? Module
229
- methods.each do |sym, stuff|
230
- target.define_singleton_method sym, &stuff[:body]
231
- target.private_class_method sym if stuff[:private]
232
- end
233
-
234
- else
235
- methods.each do |sym, stuff|
236
- target.send :define_method, sym, &stuff[:body]
237
- target.send :private, sym if stuff[:private]
238
- end
239
- end
240
-
241
- nil
242
- end
243
-
244
-
245
- # creates a new `NRSER::Logger` and 'installs' a logger on target, adding
246
- # singleton (class) and instance methods as appropriate
247
- #
248
- # @param target [Object] the object to install a new logger on, which can
249
- # be a Module, a Class, or just any plain-old instance that you want
250
- # to have it's own logger client.
251
- #
252
- # @param options [Hash] options used when creating the `NRSER::Logger`.
253
- # @see NRSER::Logger#initialize
254
- #
255
- # @return [NRSER::Logger] the new `NRSER::Logger` instance.
256
- #
257
- def self.install target, options = {}
258
- options[:on] ||= false
259
- options[:name] ||= if target.respond_to?(:name) && !target.name.nil?
260
- target.name
261
- else
262
- target.to_s
263
- end
264
-
265
- logger = self.new options
266
- install_methods! target, logger
267
-
268
- logger
269
- end # .install
270
-
271
-
272
- # singleton (class) and instance methods as appropriate
273
- #
274
- #
275
- # @param source [Object] source instance with a logger installed to use
276
- # for the target.
277
- #
278
- # @param target [Object] the object to use the source's logger.
279
- #
280
- # @return [NRSER::Logger] the new `NRSER::Logger` instance.
281
- #
282
- def self.use source, target
283
- install_methods! target, source.logger
284
- end # .use
285
-
286
- attr_reader :name, :dest, :level, :ruby_logger
287
-
288
- def initialize options = {}
289
- options = {
290
- dest: $stderr,
291
- level: :info,
292
- say_hi: true,
293
- on: true,
294
- }.merge options
295
-
296
- @name = options[:name]
297
- @on = options[:on]
298
- @level = self.class.level_int options[:level]
299
- self.dest = options[:dest]
300
-
301
- if @on && options[:say_hi]
302
- info <<-END.squish
303
- started to logging to #{ @dest } at level
304
- #{ self.class.level_name @level }...
305
- END
306
- end
307
- end
308
-
309
- def on?
310
- @on
311
- end
312
-
313
- def off?
314
- !on?
315
- end
316
-
317
- def on &block
318
- if block
319
- prev = @on
320
- @on = true
321
- block.call
322
- @on = prev
323
- else
324
- @on = true
325
- end
326
- end
327
-
328
- def off &block
329
- if block
330
- prev = @on
331
- @on = false
332
- block.call
333
- @on = prev
334
- else
335
- @on = false
336
- end
337
- end
338
-
339
- def level= level
340
- @level = self.class.level_int(level)
341
- @ruby_loggers.each do |dest, ruby_logger|
342
- ruby_logger.level = @level
343
- end
344
- @level
345
- end
346
-
347
- def with_level level, &block
348
- prev_level = self.level
349
- self.level = level
350
- block.call
351
- self.level = prev_level
352
- end
353
-
354
- def dest= dest
355
- @ruby_loggers = {}
356
- NRSER.each dest do |dest|
357
- @ruby_loggers[dest] = ::Logger.new(dest).tap do |ruby_logger|
358
- ruby_logger.level = @level
359
- ruby_logger.formatter = proc do |severity, datetime, progname, msg|
360
- # just pass through
361
- msg
362
- end
363
- end
364
- end
365
- @dest = dest
366
- end
367
-
368
- # logging api
369
- # ===========
370
-
371
- # @api logging
372
- def debug *args, &block
373
- send_log :debug, args, block
374
- end
375
-
376
- # @api logging
377
- def info *args, &block
378
- send_log :info, args, block
379
- end
380
-
381
- # @api logging
382
- def warn *args, &block
383
- send_log :warn, args, block
384
- end
385
-
386
- # @api logging
387
- def error *args, &block
388
- send_log :error, args, block
389
- end
390
-
391
- # @api logging
392
- def fatal *args, &block
393
- send_log :fatal, args, block
394
- end
395
-
396
- # @api logging
397
- def die *args, &block
398
- if @on
399
- send_log :fatal, args, block
400
- abort
401
- else
402
- abort self.class.format(
403
- @name,
404
- :fatal,
405
- *extract_msg_and_dump(args, block)
406
- )
407
- end
408
- end
409
-
410
- private
411
- def extract_msg_and_dump args, block
412
- msg = ''
413
- dump = {}
414
- case args.length
415
- when 0
416
- # if there is no block, just no-op
417
- # @todo is this the right way to go?
418
- if block
419
- result = block.call
420
- result = [result] unless result.is_a? Array
421
-
422
- return extract_msg_and_dump result, nil
423
- end
424
- when 1
425
- case args[0]
426
- when Hash
427
- dump = args[0]
428
- when String
429
- msg = args[0]
430
- else
431
- msg = args[0].to_s
432
- end
433
- when 2
434
- msg, dump = args
435
- else
436
- raise "must provide one or two arguments, not #{ args.length }"
437
- end
438
-
439
- [msg, dump]
440
- end
441
-
442
- def send_log level_sym, args, block
443
- return unless @on && @level <= self.class.level_int(level_sym)
444
-
445
- msg, dump = extract_msg_and_dump args, block
446
-
447
- @ruby_loggers.each do |dest, ruby_logger|
448
- ruby_logger.send(level_sym, @name) do
449
- self.class.format(@name, level_sym, msg, dump)
450
- end
451
- end
452
- end
453
-
454
- # end private
455
-
456
- end # Logger
457
- end # NRSER
@@ -1,15 +0,0 @@
1
- require 'spec_helper'
2
- require 'tempfile'
3
-
4
- describe 'NRSER::Logger.dest=' do
5
- it "writes to multiple destinations" do
6
- files = [Tempfile.new('f1'), Tempfile.new('f2')]
7
- logger = NRSER::Logger.new dest: files, say_hi: false
8
- logger.info "hey!"
9
- files.each do |file|
10
- file.rewind
11
- data = YAML.load file.read
12
- expect(data['INFO']['msg']).to eq "hey!"
13
- end
14
- end
15
- end
@@ -1,41 +0,0 @@
1
- require 'spec_helper'
2
-
3
- using NRSER
4
-
5
- describe 'NRSER::Logger#die' do
6
- # it "prints the log message when logger is off" do
7
- # src = <<-END.dedent
8
- # require 'nrser/logger'
9
- #
10
- # NRSER::Logger.install self
11
- #
12
- # die "die!", cause: "just 'cause"
13
- # END
14
- #
15
- # err = Cmds.err("bundle exec ruby"){ src }
16
- # data = YAML.load(err)['FATAL']
17
- #
18
- # expect(data['msg']).to eq 'die!'
19
- # expect(data['values']).to eq({'cause' => "just 'cause"})
20
- # end
21
- #
22
- # it(
23
- # "prints only to the log when it's on and the log writes to " +
24
- # "$stderr"
25
- # ) do
26
- # src = <<-END
27
- # require 'nrser/logger'
28
- #
29
- # NRSER::Logger.install self, on: true, say_hi: false
30
- #
31
- # die "die!", cause: "just 'cause"
32
- # END
33
- #
34
- # err = Cmds.err("bundle exec ruby"){ src }
35
- #
36
- # data = YAML.load(err)['FATAL']
37
- #
38
- # expect(data['msg']).to eq 'die!'
39
- # expect(data['values']).to eq({'cause' => "just 'cause"})
40
- # end
41
- end
@@ -1,98 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'NRSER::Logger.install' do
4
- it "installs on the global instance" do
5
- NRSER::Logger.install MAIN
6
-
7
- expect(logger).to be_a NRSER::Logger
8
- expect(logger.name).to eq 'main'
9
-
10
- expect_to_not_log { info "hey" }
11
-
12
- logger.on
13
-
14
- expect_to_log { info "hey" }
15
- expect_to_not_log { debug "hey" }
16
- end
17
-
18
- it "is accesible in modules" do
19
-
20
- mod = Module.new do
21
- def self.log
22
- info "hey"
23
- end
24
-
25
- def self.logger_name
26
- logger.name
27
- end
28
- end
29
-
30
- expect_to_log { mod.log }
31
- expect(mod.logger_name).to eq 'main'
32
- end
33
-
34
- it "is accessible in classes" do
35
-
36
- cls = Class.new do
37
- def self.log
38
- info "hey"
39
- end
40
-
41
- def log
42
- info "hey"
43
- end
44
- end
45
-
46
- expect_to_log { cls.log }
47
-
48
- c = cls.new
49
- expect_to_log { c.log }
50
- end
51
-
52
- it "installs in modules" do
53
- mod = Module.new do
54
- NRSER::Logger.install self
55
- end
56
-
57
- expect(mod.logger).to_not be MAIN.logger
58
- expect(mod.logger.name).to match /\#\<Module/
59
-
60
- expect_to_not_log { mod.send :info, "hey" }
61
-
62
- mod.logger.on
63
-
64
- expect_to_log { mod.send :info, "hey" }
65
- end
66
-
67
- it "installs in classes" do
68
- cls = Class.new do
69
- NRSER::Logger.install self
70
- end
71
-
72
- expect(cls.logger).to_not be MAIN.logger
73
- expect(cls.logger.name).to match /\#\<Class/
74
-
75
- c = cls.new
76
-
77
- expect(c.logger).to be cls.logger
78
- expect(c.logger.name).to match /\#\<Class/
79
-
80
- expect_to_not_log { cls.send :info, "hey" }
81
- expect_to_not_log { c.send :info, "hey" }
82
-
83
- cls.logger.on
84
-
85
- expect_to_log { cls.send :info, "hey" }
86
- expect_to_log { c.send :info, "hey" }
87
-
88
- expect_to_not_log { cls.send :debug, "hey" }
89
-
90
- # @todo hmmm... this switches the log level for the entire class, not
91
- # the instance...
92
- c.logger.level = :debug
93
-
94
- expect_to_log { cls.send :debug, "hey" }
95
- expect_to_log { c.send :debug, "hey" }
96
- end
97
-
98
- end
@@ -1,22 +0,0 @@
1
- require 'logger'
2
-
3
- require 'spec_helper'
4
-
5
- describe 'NRSER::Logger.level_int' do
6
- it "translates level ints, names and symbols" do
7
- LOG_LEVELS.each do |int, (sym, name)|
8
- expect( NRSER::Logger.level_int int ).to eq int
9
- expect( NRSER::Logger.level_int sym ).to eq int
10
- expect( NRSER::Logger.level_int name ).to eq int
11
- end
12
- end
13
-
14
- it "pukes on bad a args" do
15
- BAD_LOG_LEVELS.each do |arg|
16
- expect {
17
- NRSER::Logger.level_int arg
18
- }.to raise_error ArgumentError
19
- end
20
- end
21
-
22
- end
@@ -1,23 +0,0 @@
1
- require 'logger'
2
-
3
- require 'spec_helper'
4
-
5
- describe 'NRSER::Logger.level_name' do
6
-
7
- it "translates level names, integers and symbols" do
8
- LOG_LEVELS.each do |int, (sym, name)|
9
- expect( NRSER::Logger.level_name name ).to eq name
10
- expect( NRSER::Logger.level_name sym ).to eq name
11
- expect( NRSER::Logger.level_name int ).to eq name
12
- end
13
- end
14
-
15
- it "pukes on bad a args" do
16
- BAD_LOG_LEVELS.each do |arg|
17
- expect {
18
- NRSER::Logger.level_name arg
19
- }.to raise_error ArgumentError
20
- end
21
- end
22
-
23
- end
@@ -1,22 +0,0 @@
1
- require 'logger'
2
-
3
- require 'spec_helper'
4
-
5
- describe 'NRSER::Logger.level_sym' do
6
- it "translates level syms, names and symbols" do
7
- LOG_LEVELS.each do |int, (sym, name)|
8
- expect( NRSER::Logger.level_sym sym ).to eq sym
9
- expect( NRSER::Logger.level_sym int ).to eq sym
10
- expect( NRSER::Logger.level_sym name ).to eq sym
11
- end
12
- end
13
-
14
- it "pukes on bad a args" do
15
- BAD_LOG_LEVELS.each do |arg|
16
- expect {
17
- NRSER::Logger.level_sym arg
18
- }.to raise_error ArgumentError
19
- end
20
- end
21
-
22
- end