sappho-basics 0.0.7 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,8 +11,6 @@ module Sappho
11
11
 
12
12
  class AutoFlushLog
13
13
 
14
- include Singleton
15
-
16
14
  LOG_LEVELS = {
17
15
  'debug' => Logger::DEBUG,
18
16
  'info' => Logger::INFO,
@@ -24,23 +22,11 @@ module Sappho
24
22
  'message' => proc { |severity, datetime, progname, message| "#{message}\n" },
25
23
  'test' => proc { |severity, datetime, progname, message| "#{severity} #{message}\n" }
26
24
  }
27
- @@file = nil
28
-
29
- def AutoFlushLog.file file
30
- @@file = file
31
- end
32
25
 
33
- def initialize
26
+ def initialize file, level, detail
34
27
  @mutex = Mutex.new
35
- unless @@file
36
- filename = ENV['application.log.filename']
37
- mode = File.exists?(filename) ? 'a' : 'w' if filename
38
- @@file = filename ? File.open(filename, mode) : $stdout
39
- end
40
- @log = Logger.new @@file
41
- level = ENV['application.log.level']
28
+ @log = Logger.new file
42
29
  @log.level = LOG_LEVELS.has_key?(level) ? LOG_LEVELS[level] : Logger::INFO
43
- detail = ENV['application.log.detail']
44
30
  @log.formatter = LOG_DETAIL[detail] if LOG_DETAIL.has_key?(detail)
45
31
  end
46
32
 
@@ -87,6 +73,27 @@ module Sappho
87
73
 
88
74
  end
89
75
 
76
+ class ApplicationAutoFlushLog < AutoFlushLog
77
+
78
+ include Singleton
79
+
80
+ @@file = nil
81
+
82
+ def AutoFlushLog.file file
83
+ @@file = file
84
+ end
85
+
86
+ def initialize
87
+ unless @@file
88
+ filename = ENV['application.log.filename']
89
+ mode = File.exists?(filename) ? 'a' : 'w' if filename
90
+ @@file = filename ? File.open(filename, mode) : $stdout
91
+ end
92
+ super @@file, ENV['application.log.level'], ENV['application.log.detail']
93
+ end
94
+
95
+ end
96
+
90
97
  module LogUtilities
91
98
 
92
99
  def hexString bytes
@@ -10,10 +10,8 @@ module Sappho
10
10
 
11
11
  class ModuleRegister
12
12
 
13
- include Singleton
14
-
15
- def initialize
16
- @modules = { :log => AutoFlushLog.instance }
13
+ def initialize modules = {}
14
+ @modules = modules
17
15
  end
18
16
 
19
17
  def set name, mod
@@ -34,14 +32,28 @@ module Sappho
34
32
 
35
33
  def each
36
34
  @modules.each do |name, mod|
37
- begin
38
- yield mod
39
- rescue
40
- # do nothing here - modules must report if they need to
35
+ if mod.instance_of? ModuleRegister
36
+ mod.each { |mod| yield mod }
37
+ else
38
+ begin
39
+ yield mod
40
+ rescue
41
+ # do nothing here - modules must report if they need to
42
+ end
41
43
  end
42
44
  end
43
45
  end
44
46
 
45
47
  end
46
48
 
49
+ class ApplicationModuleRegister < ModuleRegister
50
+
51
+ include Singleton
52
+
53
+ def initialize
54
+ super :log => ApplicationAutoFlushLog.instance
55
+ end
56
+
57
+ end
58
+
47
59
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  module Sappho
7
7
  NAME = 'sappho-basics'
8
- VERSION = '0.0.7'
8
+ VERSION = '0.1.0'
9
9
  AUTHORS = ['Andrew Heald']
10
10
  EMAILS = ['andrew@heald.co.uk']
11
11
  HOMEPAGE = 'https://github.com/sappho/sappho-basics/wiki'
@@ -13,13 +13,13 @@ class AutoFlushLogTest < Test::Unit::TestCase
13
13
 
14
14
  def test_logging
15
15
  file = StringIO.new
16
- Sappho::AutoFlushLog.file file
16
+ Sappho::ApplicationAutoFlushLog.file file
17
17
  assert_raises NoMethodError do
18
- Sappho::AutoFlushLog.new
18
+ Sappho::ApplicationAutoFlushLog.new
19
19
  end
20
20
  ENV['application.log.level'] = 'debug'
21
21
  ENV['application.log.detail'] = 'test'
22
- log = Sappho::AutoFlushLog.instance
22
+ log = Sappho::ApplicationAutoFlushLog.instance
23
23
  assert log.debug?
24
24
  log.debug 'Test 1'
25
25
  log.info 'Test 2'
@@ -10,38 +10,45 @@ class ModuleRegisterTest < Test::Unit::TestCase
10
10
 
11
11
  def test_module_register
12
12
  assert_raises NoMethodError do
13
- Sappho::ModuleRegister.new
13
+ Sappho::ApplicationModuleRegister.new
14
14
  end
15
- modules = Sappho::ModuleRegister.instance
15
+ modules = Sappho::ApplicationModuleRegister.instance
16
16
  assert_nil modules.get(:missing)
17
17
  assert !modules.set?(:missing)
18
18
  assert modules.set?(:log)
19
- assert_instance_of Sappho::AutoFlushLog, modules.get(:log)
19
+ assert_instance_of Sappho::ApplicationAutoFlushLog, modules.get(:log)
20
20
  test1 = TestModule.new
21
21
  test2 = TestModule.new
22
+ test3 = TestModule.new
23
+ test4 = TestModule.new
24
+ test5 = Sappho::ModuleRegister.new
25
+ test5.set :test3, test3
26
+ test5.set :test4, test4
22
27
  modules.set :test1, test1
23
28
  modules.set :test2, test2
24
29
  modules.set :test1, test1
30
+ modules.set :test5, test5
25
31
  assert_equal test1, modules.get(:test1)
26
32
  assert_equal test2, modules.get(:test2)
33
+ assert_equal test5, modules.get(:test5)
27
34
  assert modules.set?(:test1)
28
35
  assert modules.set?(:test2)
36
+ assert !modules.set?(:test4)
29
37
  assert test1.alive
30
38
  assert test2.alive
39
+ assert test3.alive
40
+ assert test4.alive
31
41
  modules.shutdown
32
42
  assert !test1.alive
33
43
  assert !test2.alive
34
- assert_instance_of Sappho::AutoFlushLog, modules.get(:log)
44
+ assert !test3.alive
45
+ assert !test4.alive
46
+ assert_instance_of Sappho::ApplicationAutoFlushLog, modules.get(:log)
35
47
  assert_equal test1, modules.get(:test1)
36
- assert_equal test2, modules.get(:test2)
37
- test1.alive = true
38
- test2.alive = true
48
+ test4.alive = true
49
+ modules.set :notmod, 42
39
50
  modules.shutdown
40
- assert !test1.alive
41
- assert !test2.alive
42
- assert_instance_of Sappho::AutoFlushLog, modules.get(:log)
43
- assert_equal test1, modules.get(:test1)
44
- assert_equal test2, modules.get(:test2)
51
+ assert !test4.alive
45
52
  end
46
53
 
47
54
  class TestModule
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sappho-basics
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 7
10
- version: 0.0.7
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Heald
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-03-15 00:00:00 Z
18
+ date: 2012-03-16 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rake