injectedlogger 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ed2823f82e7cba6cf36cf6a49fc25a9653ce00b
4
- data.tar.gz: 260c054c7c2d305ab0704f957ce48abbf3cf004b
3
+ metadata.gz: 6c1110f07cfbbe6a10e93eed0e971351cb165f4b
4
+ data.tar.gz: f8c373e8b28fc600b561e52c8ebf676a7b3c3471
5
5
  SHA512:
6
- metadata.gz: b2ca8524827fbff3ff2f53dec3c9a192e236aee3e8de1c4c18ade2f75d0c3728ce5e2f053f431dd8b5a2d4556c73e21cf82d500004b69e3f1d39cf5dc797af0a
7
- data.tar.gz: 208518022caff1c31ffa758717fcf08e38c21d0bb7a6a81c7f92807af260489b660f30c07096d906f31f574d43aba5469fbfc6121dcab75afea2d7200b14f928
6
+ metadata.gz: 8d7d74723b918d031678ab8ff90194e41b8bddcd72393037877aed3318802b5b2d64aae7ae27cd68259a71af30430c29072283b879f7c1cd98eb417c0dc10985
7
+ data.tar.gz: 7d9734789ff5eae1f754bdb43466dedbc6ff092beed42ac0571531eccd995f7bfe284fec33f8cc7d21013e1df13ab4b72bd7be8a4ab1d4b7b717b2d6888d8eb7
data/README.md CHANGED
@@ -7,7 +7,7 @@ It will try to support as many methods and levels as the underlying object suppo
7
7
  ## Usage
8
8
 
9
9
  ```ruby
10
- logger = InjectedLogger::Logger.use somelogger
10
+ logger = InjectedLogger::Logger.inject somelogger
11
11
  raise 'No info :(' unless logger.level_info[:supported].include? :info
12
12
  logger.info 'You now have a logger!'
13
13
  ```
@@ -16,7 +16,7 @@ or you can set-up a default injection for your logger in case no one else sets i
16
16
 
17
17
  ```ruby
18
18
  module MyLogger
19
- InjectedLogger.inject required: [:debug, :info] do
19
+ InjectedLogger.declare required: [:debug, :info] do
20
20
  require 'logger'
21
21
  { logger: Logger.new(STDERR), prefix: '[mylogger]' }
22
22
  end
@@ -8,7 +8,7 @@ module InjectedLogger
8
8
  # module MyLogger
9
9
  # InjectedLogger.inject do
10
10
  # require 'logger'
11
- # # logger is required, the rest are other use() params
11
+ # # logger is required, the rest are other inject() params
12
12
  # { logger: Logger.new(STDERR), prefix: '[mylogger]', ... }
13
13
  # end
14
14
  # end
@@ -26,18 +26,20 @@ module InjectedLogger
26
26
  # logger method is called, so that it does not 'require' anything if it is not
27
27
  # needed. :)
28
28
 
29
- def self.inject(on: nil, required: [], method_name: :logger, &blk)
30
- on = blk.binding.eval 'self' unless on
31
- on = on.singleton_class unless on.is_a? Module
29
+ def self.declare(on: nil, required: [], method_name: :logger, &blk)
30
+ if on.nil?
31
+ raise InjectedLogger::DefaultInjectionBlockMissing if blk.nil?
32
+ on = blk.binding.eval 'self'
33
+ else
34
+ on = on.singleton_class unless on.is_a? Module
35
+ end
32
36
  on.send :define_method, method_name do
33
- unless InjectedLogger::Logger.in_use?
34
- args = blk.call
35
- logger = args.delete :logger
36
- unless required.empty?
37
- args[:levels] ||= []
38
- args[:levels].push(required).flatten!.uniq!
39
- end
40
- InjectedLogger::Logger.use(logger, args)
37
+ # avoid recursion if someone calls logger in the block
38
+ on.send :remove_method, method_name
39
+ unless InjectedLogger::Logger.injected?
40
+ args = blk ? blk.call : nil
41
+ args = InjectedLogger.default_logger if args.nil? or args == :default
42
+ InjectedLogger.inject_logger args, required
41
43
  end
42
44
  required.uniq!
43
45
  required -= InjectedLogger::Logger.level_info[:supported]
@@ -48,4 +50,20 @@ module InjectedLogger
48
50
  InjectedLogger::Logger
49
51
  end
50
52
  end
53
+
54
+ private
55
+
56
+ def self.default_logger
57
+ require 'logger'
58
+ { logger: ::Logger.new(STDERR) }
59
+ end
60
+
61
+ def self.inject_logger(args, required)
62
+ logger = args.delete :logger
63
+ unless required.empty?
64
+ args[:levels] ||= []
65
+ args[:levels].push(required).flatten!.uniq!
66
+ end
67
+ InjectedLogger::Logger.inject(logger, args)
68
+ end
51
69
  end
@@ -1,6 +1,7 @@
1
1
  module InjectedLogger
2
2
  Error = Class.new StandardError
3
3
  InUse = Class.new Error
4
+ DefaultInjectionBlockMissing = Class.new Error
4
5
  UnsupportedLevels = Class.new Error do
5
6
  def initalize(levels)
6
7
  super("logger does not support required levels #{levels.join ', '}")
@@ -9,18 +9,18 @@ module InjectedLogger
9
9
  class << self
10
10
  attr_reader :prefix, :levels, :level_info, :fallback
11
11
 
12
- def in_use?
12
+ def injected?
13
13
  not logger.nil?
14
14
  end
15
15
 
16
- def use(logger_obj, levels: LOGLEVELS, fallback: UNKNOWN, prefix: nil)
16
+ def inject(logger_obj, levels: LOGLEVELS, fallback: UNKNOWN, prefix: nil)
17
17
  if logger and logger != logger_obj
18
18
  raise InUse, "#{self} was already using logger #{logger}"
19
19
  end
20
- use! logger_obj, levels: levels, fallback: fallback
20
+ inject! logger_obj, levels: levels, fallback: fallback
21
21
  end
22
22
 
23
- def use!(logger_obj, levels: LOGLEVELS, fallback: UNKNOWN, prefix: nil)
23
+ def inject!(logger_obj, levels: LOGLEVELS, fallback: UNKNOWN, prefix: nil)
24
24
  self.logger = logger_obj
25
25
  set_prefix prefix
26
26
  set_levels levels
@@ -1,3 +1,3 @@
1
1
  module InjectedLogger
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
data/spec/logger_spec.rb CHANGED
@@ -66,7 +66,7 @@ module InjectedLogger
66
66
  let(:logger_object) { Helpers::RubyLikeLogger.new [] }
67
67
 
68
68
  before do
69
- InjectedLogger::Logger.use! logger_object
69
+ InjectedLogger::Logger.inject! logger_object
70
70
  #l = InjectedLogger::Logger
71
71
  #STDERR.puts "Native: #{l.level_info[:native]} Non-native: #{l.level_info[:nonnative]} Fallback: #{l.level_info[:fallback]} Info: #{l.level_info[:info]}"
72
72
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: injectedlogger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alejandro Martinez Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-07 00:00:00.000000000 Z
11
+ date: 2014-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler