goz 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,200 @@
1
+ # encoding: utf-8
2
+
3
+ require 'goz/test_case'
4
+
5
+
6
+ module Goz # :nodoc:
7
+ class Group # :nodoc
8
+
9
+ #
10
+ # = Goz::Group::TestCase - Goz group test case.
11
+ #
12
+ # == Author
13
+ #
14
+ # blair christensen. <mailto:blair.christensen@gmail.com>
15
+ #
16
+ # == Homepage
17
+ #
18
+ # https://github.com/blairc/goz/
19
+ #
20
+ class TestCase < Goz::TestCase
21
+
22
+ def setup
23
+ super
24
+ @klass ||= self.class
25
+ @skip = @klass == Goz::Group::TestCase
26
+ end
27
+
28
+ def skip?
29
+ @skip
30
+ end
31
+
32
+
33
+ def test_default_initialization
34
+ return if skip?
35
+
36
+ blockable = false
37
+ group = @klass.new do |g|
38
+ assert_kind_of @klass, g
39
+ assert_nil g.display_name
40
+ assert_nil g.identifier
41
+ assert_nil g.klass
42
+ assert_nil g.name
43
+ assert_nil g.created_at
44
+ assert_nil g.modified_at
45
+ assert_nil g.synchronized_at
46
+ blockable = true
47
+ end
48
+ assert blockable, 'works as block'
49
+ assert_kind_of Goz::Group, group
50
+ assert_kind_of @klass, group unless @klass == Goz::Group
51
+ assert_kind_of @klass, @klass.new
52
+ assert_nil group.display_name
53
+ assert_nil group.identifier
54
+ assert_nil group.klass
55
+ assert_nil group.name
56
+ assert_nil group.created_at
57
+ assert_nil group.modified_at
58
+ assert_nil group.synchronized_at
59
+ end
60
+
61
+ def test_find_by_name_for_group_that_does_not_exist
62
+ return if skip?
63
+
64
+ assert_nil @klass.find_by_name @groups[:a][:name]
65
+ end
66
+
67
+ def test_find_by_name_for_group_that_does_exist
68
+ return if skip?
69
+ return if @klass == Goz::Group
70
+ fail("not implemented")
71
+ end
72
+
73
+ def test_create_with_invalid_parameters
74
+ return if skip?
75
+
76
+ assert_raise(RuntimeError, 'invalid display_name') { @klass.create(nil) }
77
+ assert_raise(RuntimeError, 'invalid display_name') {
78
+ @klass.create( :identifier => @groups[:a][:identifier],
79
+ :klass => @klass.name,
80
+ :name => @groups[:a][:name]
81
+ )
82
+ }
83
+ assert_raise(RuntimeError, 'invalid display_name') {
84
+ @klass.create( :display_name => '',
85
+ :identifier => @groups[:a][:identifier],
86
+ :klass => @klass.name,
87
+ :name => @groups[:a][:name]
88
+ )
89
+ }
90
+ assert_raise(RuntimeError, 'invalid identifier') {
91
+ @klass.create( :display_name => @groups[:a][:display_name],
92
+ :klass => @klass.name,
93
+ :name => @groups[:a][:name]
94
+ )
95
+ }
96
+ assert_raise(RuntimeError, 'invalid identifier') {
97
+ @klass.create( :display_name => @groups[:a][:display_name],
98
+ :identifier => '',
99
+ :klass => @klass.name,
100
+ :name => @groups[:a][:name]
101
+ )
102
+ }
103
+ assert_raise(RuntimeError, 'invalid klass') {
104
+ @klass.create( :display_name => @groups[:a][:display_name],
105
+ :identifier => @groups[:a][:identifier],
106
+ :name => @groups[:a][:name]
107
+ )
108
+ }
109
+ assert_raise(RuntimeError, 'invalid klass') {
110
+ @klass.create( :display_name => @groups[:a][:display_name],
111
+ :identifier => @groups[:a][:identifier],
112
+ :klass => '',
113
+ :name => @groups[:a][:name]
114
+ )
115
+ }
116
+ assert_raise(RuntimeError, 'invalid name') {
117
+ @klass.create( :display_name => @groups[:a][:display_name],
118
+ :identifier => @groups[:a][:identifier],
119
+ :klass => @klass.name
120
+ )
121
+ }
122
+ assert_raise(RuntimeError, 'invalid name') {
123
+ @klass.create( :display_name => @groups[:a][:display_name],
124
+ :identifier => @groups[:a][:identifier],
125
+ :klass => @klass.name,
126
+ :name => ''
127
+ )
128
+ }
129
+ end
130
+
131
+ def test_create
132
+ return if skip?
133
+
134
+ t = Time.now
135
+ g = @klass.create @groups[:a].merge( :klass => @klass.name )
136
+ assert_not_nil g
137
+ assert_kind_of @klass, g
138
+ assert_equal @groups[:a][:display_name], g.display_name
139
+ assert_equal @groups[:a][:identifier], g.identifier
140
+ assert_equal @klass.name, g.klass
141
+ assert_equal @groups[:a][:name], g.name
142
+ assert_not_nil g.created_at
143
+ assert g.created_at >= t
144
+ end
145
+
146
+ def test_update
147
+ return if skip?
148
+
149
+ g = Goz::Group.create @groups[:a].merge( :klass => @klass.name )
150
+ g.name = g.name.reverse
151
+ g.save
152
+
153
+ assert_not_nil g.modified_at
154
+ assert_kind_of Time, g.modified_at
155
+ assert g.modified_at > g.created_at
156
+
157
+ assert_nil g.synchronized_at
158
+ end
159
+
160
+ def test_sync_with_provider
161
+ return if skip?
162
+ return if @klass == Goz::Group
163
+ fail("not implemented")
164
+ end
165
+
166
+ def test_sync_with_provider_no_admin_or_member_changes
167
+ return if skip?
168
+ return if @klass == Goz::Group
169
+ fail("not implemented")
170
+ end
171
+
172
+ def test_sync_with_provider_add_admin
173
+ return if skip?
174
+ return if @klass == Goz::Group
175
+ fail("not implemented")
176
+ end
177
+
178
+ def test_sync_with_provider_remove_admin
179
+ return if skip?
180
+ return if @klass == Goz::Group
181
+ fail("not implemented")
182
+ end
183
+
184
+ def test_sync_with_provider_add_member
185
+ return if skip?
186
+ return if @klass == Goz::Group
187
+ fail("not implemented")
188
+ end
189
+
190
+ def test_sync_with_provider_remove_member
191
+ return if skip?
192
+ return if @klass == Goz::Group
193
+ fail("not implemented")
194
+ end
195
+
196
+ end
197
+
198
+ end
199
+ end
200
+
@@ -2,8 +2,10 @@
2
2
 
3
3
  require 'goz/database'
4
4
  require 'goz/event'
5
+ require 'goz/logger'
5
6
  require 'goz/group_service'
6
7
 
8
+
7
9
  module Goz # :nodoc:
8
10
 
9
11
  #
@@ -19,20 +21,13 @@ module Goz # :nodoc:
19
21
  #
20
22
  class GroupService < Sequel::Model
21
23
 
24
+ TAG = self.name
25
+
22
26
  #
23
27
  # Synchronize group with external service.
24
28
  #
25
29
  def sync(force = false)
26
- if force || synchronized_at.nil? || synchronized_at < ( Time.now - ( 1 * 60 * 60 ) ) # XXX
27
- # XXX Synchronize group from remote source (if necessary)
28
- # XXX Synchronize admins
29
- # XXX Synchronize members
30
- self.synchronized_at = Time.now
31
- self.save
32
- Goz::Event.group_service_sync( Group[ self.group_id ], Service[ self.service_id ] )
33
- return true
34
- end
35
- false
30
+ raise RuntimeError, 'XXX'
36
31
  end
37
32
 
38
33
  end
data/lib/goz/logger.rb CHANGED
@@ -9,23 +9,7 @@ module Goz # :nodoc:
9
9
  #
10
10
  # == Usage
11
11
  #
12
- # require 'goz/logger'
13
- #
14
- # logger = Goz::Logger.new('MyApp') do |logger|
15
- # logger.debug('block initialization with default logger')
16
- # end
17
- #
18
- # logger = Goz::Logger.new('MyApp')
19
- # logger.error('initialization with default logger')
20
- #
21
- # logger = Goz::Logger.instance( 'MyApp', ::Logger.new($stdout) ) do |logger|
22
- # logger.fatal('instance initialization with custom logger')
23
- # end
24
- #
25
- # logger = Goz::Logger.instance( 'MyApp', ::Logger.new($stdout) )
26
- # logger.info('instance initialization with custom logger')
27
- #
28
- # logger.warn('warn example to cover all log severity levels')
12
+ # TODO
29
13
  #
30
14
  # == Author
31
15
  #
@@ -38,104 +22,89 @@ module Goz # :nodoc:
38
22
  class Logger
39
23
 
40
24
  #
41
- # Default logfile to use.
25
+ # Default log device to use.
42
26
  #
43
- DEFAULT_LOGFILE = $stdout
44
-
45
- #
46
- # Default logger to use.
47
- #
48
- DEFAULT_LOGGER = ::Logger.new(DEFAULT_LOGFILE)
49
-
27
+ DEFAULT_LOGDEV = $stdout
50
28
 
51
- @@loggers = {}
52
-
53
-
54
- #
55
- # Create (if necessary) and return Goz::Logger instance.
56
- #
57
- # Params:
58
- # +tag+:: Tag log messages with this +String+.
59
- # +logger+:: (optional) Any +::Logger+ compatible class. Defaults to +DEFAULT_LOGGER+.
60
- #
61
- def self.instance( tag, logger = DEFAULT_LOGGER )
62
- key = [ tag, logger ]
63
- unless @@loggers.key?(key)
64
- @@loggers[key] = self.new(tag, logger)
65
- end
66
- yield @@loggers[key] if block_given?
67
- @@loggers[key]
68
- end
29
+ @@logger = nil
69
30
 
70
- #
71
- # Initialize Goz::Logger
72
- #
73
- # Params:
74
- # +tag+:: Tag log messages with this +String+.
75
- # +logger+:: (optional) Any +::Logger+ compatible class. Defaults to +DEFAULT_LOGGER+.
76
- #
77
- def initialize( tag, logger = DEFAULT_LOGGER )
78
- @logger = logger
79
- @tag = tag
80
- # TODO Make configurable?
81
- @logger.formatter = proc do |severity, datetime, progname, msg|
82
- "#{datetime} #{severity} #{tag}: #{msg}\n"
83
- end
84
- yield self if block_given?
85
- self
86
- end
87
31
 
88
32
  #
89
33
  # Send +debug+ log message.
90
34
  #
91
35
  # Params:
36
+ # +sender+:: +String+ sender of log message.
92
37
  # +message+:: +String+ message to send to logger.
93
38
  #
94
- def debug(message)
95
- @logger.debug message
39
+ def self.debug(sender, message)
40
+ self.instance.debug "#{sender} - #{message}"
96
41
  end
97
42
 
98
43
  #
99
44
  # Send +error+ log message.
100
45
  #
101
46
  # Params:
47
+ # +sender+:: +String+ sender of log message.
102
48
  # +message+:: +String+ message to send to logger.
103
49
  #
104
- def error(message)
105
- @logger.error message
50
+ def self.error(sender, message)
51
+ self.instance.error "#{sender} - #{message}"
106
52
  end
107
53
 
108
54
  #
109
55
  # Send +fatal+ log message.
110
56
  #
111
57
  # Params:
58
+ # +sender+:: +String+ sender of log message.
112
59
  # +message+:: +String+ message to send to logger.
113
60
  #
114
- def fatal(message)
115
- @logger.fatal message
61
+ def self.fatal(sender, message)
62
+ self.instance.fatal "#{sender} - #{message}"
116
63
  end
117
64
 
118
65
  #
119
66
  # Send info log message.
120
67
  #
121
68
  # Params:
69
+ # +sender+:: +String+ sender of log message.
122
70
  # +message+:: +String+ message to send to logger.
123
71
  #
124
- def info(message)
125
- @logger.info message
72
+ def self.info(sender, message)
73
+ self.instance.info "#{sender} - #{message}"
126
74
  end
127
75
 
128
76
  #
129
77
  # Send warn log message.
130
78
  #
131
79
  # Params:
80
+ # +sender+:: +String+ sender of log message.
132
81
  # +message+:: +String+ message to send to logger.
133
82
  #
134
- def warn(message)
135
- @logger.warn message
83
+ def self.warn(sender, message)
84
+ self.instance.warn "#{sender} - #{message}"
85
+ end
86
+
87
+
88
+ private
89
+
90
+ #
91
+ # Initialize logger.
92
+ #
93
+ # Params:
94
+ # +tag+:: Tag log messages with this +String+.
95
+ #
96
+ def self.instance( tag = 'goz', logdev = DEFAULT_LOGDEV )
97
+ if @@logger.nil?
98
+ @@logger = ::Logger.new logdev
99
+ @@logger.formatter = proc do |severity, datetime, progname, msg|
100
+ "#{datetime} #{severity} #{tag}: #{msg}\n"
101
+ end
102
+ end
103
+ yield @@logger if block_given?
104
+ @@logger
136
105
  end
137
106
 
138
- end # class Logger
107
+ end
139
108
 
140
- end # module Goz
109
+ end
141
110
 
data/lib/goz/service.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'active_support/inflector'
3
4
  require 'goz/database'
4
5
  require 'goz/event'
6
+ require 'goz/group_service'
7
+ require 'goz/logger'
5
8
 
6
9
 
7
10
  module Goz # :nodoc:
@@ -31,18 +34,11 @@ module Goz # :nodoc:
31
34
  :right_key => :group_id,
32
35
  :join_table => :group_services,
33
36
  :class => :'Goz::Group',
34
- :after_add => proc { |s, g| Goz::Event.group_service_add(g, s) },
35
- :after_remove => proc { |s, g| Goz::Event.group_service_remove(g, s) }
37
+ :after_add => proc { |s, g| Goz::Group.after_add_service g, s },
38
+ :after_remove => proc { |s, g| Goz::Event.after_remove_service g, s }
36
39
 
37
- #
38
- # Initialize new (non-persistent) Goz::Event object
39
- #
40
- def initialize(*params)
41
- super
42
- yield self if block_given?
43
- self
44
- end
45
40
 
41
+ TAG = self.name
46
42
 
47
43
  #
48
44
  # Compare equality.
@@ -59,41 +55,65 @@ module Goz # :nodoc:
59
55
  end
60
56
  end
61
57
 
62
- #
63
- # Create Goz::Service or raise +RuntimeError+
64
- #
65
- def self.create( params = {} )
66
- params = {} unless params.kind_of?(Hash)
67
- [ :name, :klass ].each do |k|
68
- raise( "invalid #{k.to_s}" ) if ( params[k].nil? || params[k].empty? )
58
+ # XXX
59
+ def configuration( configuration = {} )
60
+ klass_instance.configuration configuration
61
+ end
62
+
63
+ def klass_instance
64
+ unless @k
65
+ require self.klass.underscore
66
+ @k = self.klass.constantize
69
67
  end
70
- super
68
+ return @k
69
+ end
70
+
71
+ # XXX
72
+ def create(g)
73
+ Goz::Logger.debug TAG, "#create( group.name=#{g.name} )"
74
+ klass_instance.create g
75
+ end
76
+
77
+ def destroy(g)
78
+ Goz::Logger.debug TAG, "#destroy( group.name=#{g.name} )"
79
+ klass_instance.destroy g
80
+ end
81
+
82
+ def to_s
83
+ "name=#{ self.name } klass=#{ self.klass }"
71
84
  end
72
85
 
73
86
  #
74
87
  # Find Goz::Service by name or return +nil+
75
88
  #
76
89
  def self.find_by_name(name)
77
- self.find( :name => name )
90
+ Goz::Logger.debug TAG, "find_by_name( name=#{name} )"
91
+ self.find :name => name
78
92
  end
79
93
 
80
94
  #
81
95
  # Synchronize service with external data sources (if relevant).
82
96
  #
83
- def sync(force = false)
84
- if force || synchronized_at.nil? || synchronized_at < ( Time.now - ( 1 * 60 * 60 ) ) # XXX
85
- # XXX Synchronize service from remote source (if necessary)
86
- # XXX Synchronize groups
87
- # XXX Synchronize group admins
88
- # XXX Synchronize group members
89
- self.synchronized_at = Time.now
90
- self.save
91
- Goz::Event.service_sync(self)
97
+ def sync_group(g) # XXX
98
+ Goz::Logger.debug TAG, "#sync_group( group.name=#{g.name} )"
99
+ if klass_instance.sync_group g, self
100
+ self.synchronized_at = Time.now # TODO GroupService
101
+ self.save # TODO GroupService
102
+ Goz::Event.service_sync(self) # TODO GroupService
92
103
  return true
93
104
  end
94
105
  false
95
106
  end
96
107
 
108
+ def sync(force = false)
109
+ Goz::Logger.debug TAG, "#sync( force=#{force} )"
110
+ self.groups.each { |g| self.klass_instance.sync_group g, self }
111
+ self.synchronized_at = Time.now
112
+ self.save
113
+ Goz::Event.service_sync self
114
+ return true
115
+ end
116
+
97
117
  #
98
118
  # Perform model valiations.
99
119
  #
@@ -131,7 +151,7 @@ module Goz # :nodoc:
131
151
  self.modified_at = Time.now
132
152
  end
133
153
 
134
- end # class Service
154
+ end
135
155
 
136
- end # module Goz
156
+ end
137
157