messagefactory 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -40,7 +40,7 @@ MessageFactory is currently tested on:
40
40
 
41
41
  === Result:
42
42
 
43
- #<OpenStruct direction=:incoming, received=Fri Feb 05 19:58:42 UTC 2010, raw=":mod_spox!~mod_spox@host JOIN :#m", type=:join, source="mod_spox!~mod_spox@host", channel="#m">
43
+ #<MessageFactory::Message direction=:incoming, received=Fri Feb 05 19:58:42 UTC 2010, raw=":mod_spox!~mod_spox@host JOIN :#m", type=:join, source="mod_spox!~mod_spox@host", channel="#m">
44
44
 
45
45
  == Last remarks
46
46
 
data/Rakefile CHANGED
@@ -13,7 +13,7 @@ require 'spec/rake/spectask'
13
13
 
14
14
  spec = Gem::Specification.new do |s|
15
15
  s.name = 'messagefactory'
16
- s.version = '0.0.2'
16
+ s.version = '0.0.3'
17
17
  s.has_rdoc = true
18
18
  s.extra_rdoc_files = ['README.rdoc', 'LICENSE']
19
19
  s.summary = 'Objectifies content from an IRC server'
@@ -1,8 +1,10 @@
1
+ require 'messagefactory/Handler'
2
+
1
3
  module MessageFactory
2
4
  class Factory
3
5
  # Create a new Factory
4
6
  def initialize
5
- @handers = {}
7
+ @handlers = {}
6
8
  end
7
9
 
8
10
  # s:: string from server
@@ -11,14 +13,15 @@ module MessageFactory
11
13
  s = s.dup
12
14
  t = nil
13
15
  begin
14
- if(s.slice(0,1).chr == ':')
16
+ if(s.slice(0,1) == ':')
15
17
  s.slice!(0..s.index(' '))
16
18
  t = s.slice!(0..s.index(' ')-1)
17
19
  else
18
20
  t = s.slice(0..s.index(' ')-1)
19
21
  end
20
22
  t.strip!
21
- rescue
23
+ rescue => e
24
+ puts e
22
25
  raise 'Failed to determine message type'
23
26
  end
24
27
  t.to_sym
@@ -31,10 +34,15 @@ module MessageFactory
31
34
  s = nil
32
35
  mtype = type(string)
33
36
  if(@handlers[mtype])
34
- s = @handlers.process(string)
37
+ s = @handlers[mtype].process(string)
35
38
  else
36
39
  if(do_require)
37
- require "messagefactory/handlers/#{mtype}"
40
+ begin
41
+ require "messagefactory/handlers/#{mtype}"
42
+ rescue LoadError
43
+ raise NoMethodError.new("No handler found to process string: #{string}")
44
+ end
45
+ load_handlers
38
46
  s = process(string, false)
39
47
  else
40
48
  raise NoMethodError.new("No handler found to process string: #{string}")
@@ -43,5 +51,24 @@ module MessageFactory
43
51
  s
44
52
  end
45
53
 
54
+ private
55
+
56
+ def load_handlers
57
+ loaded = @handlers.values.map{|x|x.class}
58
+ MessageFactory::Handlers.constants.each do |cons|
59
+ klas = MessageFactory::Handlers.const_get(cons)
60
+ if(klas.is_a?(Class) && klas < MessageFactory::Handlers::Handler && !loaded.include?(klas))
61
+ handler = klas.new
62
+ if(handler.types_process.is_a?(Array))
63
+ handler.types_process.each do |t|
64
+ @handlers[t] = handler
65
+ end
66
+ else
67
+ @handlers[handler.types_process] = handler
68
+ end
69
+ end
70
+ end
71
+ end
72
+
46
73
  end
47
74
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: messagefactory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - spox
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-04-18 00:00:00 -07:00
12
+ date: 2010-04-19 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15