eventer 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.en.md ADDED
@@ -0,0 +1,35 @@
1
+ # Eventer
2
+
3
+ Eventer is simple engine to control custom class events.
4
+
5
+ ## Usage
6
+
7
+ ### Creation, and setup a handler
8
+
9
+ Make a class, and set the allowable events for it. Then create a class instance, and set handler to process an event as a block using 'on_...' method of the class instance, where instead ... put the name of the event. For an each of events, thou canst setup an unlimited number of handlers.
10
+
11
+ class Test
12
+ events :event
13
+ end
14
+
15
+ t = Test.new
16
+
17
+ t.on_event do |args|
18
+ "Matched"
19
+ end
20
+
21
+ ### Triggering the handlers
22
+
23
+ In order to the event triggers, call 'event' method, that enumerates and calls all settled handlers. The 'event' method will return an Array, with a Hash values. The Hash will contain the pair as seen as the Proc pointer, and a result value.
24
+
25
+ t.event :event, args... # -> [{<#Proc...> => "Matched"}, ...]
26
+
27
+ Also, there is the procedure allowing to output the simple result value as an Array with values from each handler.
28
+
29
+ t.event_rs :event, args... # -> ["Matched", ...]
30
+
31
+ # Copyright
32
+
33
+ Copyright (c) 2011 Malo Skrylevo
34
+ See LICENSE for details.
35
+
data/README.md CHANGED
@@ -1,15 +1,13 @@
1
1
  # Eventer
2
2
 
3
- Eventer есть простой движокъ для управления событями класса.
3
+ Eventer есть простой движокъ для управленія событіями класса.
4
4
 
5
- Eventer is simple engine to control custom class events.
5
+ ## Использованіе
6
6
 
7
- ## Использованіе (Usage)
7
+ ### Созданіе и установка обработчика
8
8
 
9
- ### Используя методъ экземпляра класса (Using the class instance method)
10
- Создай класс, и задай допустимыя для него событія. Затѣмъ создай экземпляръ класса и задай обработчикъ событія въ блокѣ съ помощью метода 'on_...' экземпляра класса, гдѣ вмѣсто ... задай имя событія. А потомъ, по необходимости, дёрни событіе методомъ 'event' экземпляра класса. А плодомъ выполненія сего метода будетъ словарь (Hash) съ ключами какъ экземплярами класса Proc отражающими вызванные обработчики и со значеніями какъ плодами работы сихъ обработчиковъ:
9
+ Создай классъ, и задай допустимыя для него событія. Затѣмъ создай екземпляръ класса и опредѣли обработчикъ событія въ блокѣ съ помощью метода 'on_...' екземпляра класса, гдѣ вмѣсто ... установи имя событія. Для каждаго предопредѣленнаго событія можно закладывать неограниченное количество обработчиковъ.
11
10
 
12
- Make a class, and set the allowable events for it. Then create a class instance, and set handler to process an event as a block using 'on_...' method of the class instance, where instead ... put the name of the event. And then, if needed, trigger event with the 'event' method of the class instance. The result is a Hash with keys as the handler block instance, and values as the handler's output:
13
11
 
14
12
  class Test
15
13
  events :event
@@ -19,13 +17,21 @@ Make a class, and set the allowable events for it. Then create a class instance,
19
17
 
20
18
  t.on_event do |args|
21
19
  "Matched"
22
- end # -> {<#Proc...> => "Matched"}
20
+ end
21
+
22
+ ### Задѣствованіе обработчика
23
+
24
+ Чтобы событіе сработало, вызови методъ event, задѣйствующій всѣ установленныя обработчики. Методъ же сей вернётъ наборъ съ словарями для каждаго из обработчиковъ. Словарь при семъ будетъ содержать обѣ ключъ-значеніе, выраженныя какъ указатель на обработчикъ и плодъ его выполненія.
25
+
26
+ t.event :event, args... # -> [{<#Proc...> => "Matched"}, ...]
27
+
28
+ Есть также процедура съ выводомъ болѣе простаго результата въ видѣ набора значеній, полученныхъ изъ оныхъ обработчиковъ.
29
+
30
+ t.event_rs :event, args... # -> ["Matched", ...]
23
31
 
24
- # Права (Copyright)
32
+ # Права
25
33
 
26
34
  Авторскія и исключительныя права (а) 2011 Малъ Скрылевъ
27
35
  Зри LICENSE за подробностями.
28
36
 
29
- Copyright (c) 2011 Malo Skrylevo
30
- See LICENSE for details.
31
37
 
data/lib/eventer.rb CHANGED
@@ -1,44 +1,64 @@
1
1
  #!/usr/bin/ruby
2
2
 
3
3
  module Eventer
4
- class EventerError < StandardError
4
+ class UnknownEventError < StandardError
5
+ def initialize(*args)
6
+ args[0] = "Unregistered event :#{args[0]}" if args[0]
7
+ super(*args)
8
+ end
5
9
  end
6
10
 
7
- def method_missing(sym, *args, &block)
8
- if sym.to_s =~ /^on_(.*)/
9
- (event, prc) = [ $1.to_sym, block ]
10
- raise EventerError.new "Unregistered event :#{event}" unless self.class.events.include? event
11
- @__events__ = {} unless @__events__
12
- @__events__[event] = [] unless @__events__[event]
13
- @__events__[event] << prc unless @__events__[event].include? prc
14
- else; super; end
11
+ private
12
+
13
+ def add_event(event, &prc)
14
+ event = event.to_sym
15
+ @__events__ = {} unless @__events__
16
+ @__events__[event] = [] unless @__events__[event]
17
+ @__events__[event] << prc unless @__events__[event].include? prc
18
+ true
15
19
  end
16
20
 
21
+ public
22
+
17
23
  def event(event, *args)
18
24
  res = {}
19
25
 
20
- if @__events__ and @__events__[event]
21
- @__events__[event].each do |event_h|
22
- res[event_h] = event_h.call(args)
26
+ if @__events__
27
+ if @__events__[event]
28
+ @__events__[event].each do |event_h|
29
+ res[event_h] = event_h.call(*args)
30
+ end
31
+ else
32
+ []
23
33
  end
24
34
  else
25
- raise EventerError.new "Unregistered event :#{event}"
35
+ raise UnknownEventError.new event
26
36
  end
27
37
 
28
38
  res
29
39
  end
30
40
 
41
+ def purge_events(*events)
42
+ (events.empty? ? @__events__ : events).each do |e|
43
+ @__events__[e].clear if @__events__[e]
44
+ end if @__events__
45
+ end
46
+
47
+ def event_rs(event, *args)
48
+ event(event, *args).map do |x| x[1..-1] end.flatten
49
+ end
50
+
31
51
  end
32
52
 
33
53
 
34
54
  class Class
35
55
 
56
+ public
36
57
  def events(*args)
37
- return @__events__ if args.empty?
38
- return if @__events__
39
-
40
- @__events__ = args.map do |event| event.to_sym end.compact
41
58
  include Eventer
59
+ args.map do |e| e.to_sym end.compact.each do |e|
60
+ self.class_eval "def on_#{e}(&block); add_event(:#{e}, &block); end"
61
+ end
42
62
  end
43
63
 
44
64
  end
@@ -1,3 +1,3 @@
1
1
  module Eventer
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/spec/eventer_spec.rb CHANGED
@@ -3,60 +3,60 @@
3
3
  require File.expand_path('../spec_helper', __FILE__)
4
4
 
5
5
  describe 'Eventer' do
6
- it "Good event match" do
7
- class Test
8
- events :event
9
- end
6
+ it "Good event match" do
7
+ class Test
8
+ events :event
9
+ end
10
10
 
11
- t = Test.new
11
+ t = Test.new
12
12
 
13
- t.on_event do |args|
14
- "Matched"
15
- end
13
+ t.on_event do |args|
14
+ "Matched"
15
+ end
16
16
 
17
- o = (t.event :event).to_a.flatten.map do |x| x.class == String ? x : nil end.compact
17
+ o = t.event_rs :event
18
18
 
19
- raise "Event result isn't matched" if o != [ "Matched" ]
19
+ raise "Event result isn't matched" if o != [ "Matched" ]
20
+ end
21
+
22
+ it "Unknown event triggered" do
23
+ class Test
24
+ events :event
20
25
  end
21
26
 
22
- it "Unknown event triggered" do
23
- class Test
24
- events :event
25
- end
27
+ t = Test.new
26
28
 
27
- t = Test.new
29
+ t.on_event do |args|
30
+ "Matched"
31
+ end
28
32
 
29
- t.on_event do |args|
30
- "Matched"
31
- end
33
+ o = begin
34
+ t.event_rs :unknown
35
+ rescue Eventer::UnknownEventError
36
+ true
37
+ end
32
38
 
33
- o = begin
34
- (t.event :unknown).to_a.flatten.map do |x| x.class == String ? x : nil end.compact
35
- rescue Eventer::EventerError
36
- true
37
- end
39
+ raise "Event result isn't matched" if (not o) or (o != true)
40
+ end
38
41
 
39
- raise "Event result isn't matched" if (not o) or (o != true)
42
+ it "Try to register an unknown event" do
43
+ class Test
44
+ events :event
40
45
  end
41
46
 
42
- it "Try to register an unknown event" do
43
- class Test
44
- events :event
45
- end
47
+ t = Test.new
46
48
 
47
- t = Test.new
48
-
49
- o = begin
50
- t.on_unknown do |args|
51
- "Matched"
52
- end
53
- false
54
- rescue Eventer::EventerError
55
- true
49
+ o = begin
50
+ t.on_unknown do |args|
51
+ "Matched"
56
52
  end
57
-
58
- raise "Event result isn't matched" if (not o) or (o != true)
53
+ false
54
+ rescue NoMethodError
55
+ true
59
56
  end
57
+
58
+ raise "Event result isn't matched" if (not o) or (o != true)
59
+ end
60
60
  end
61
61
 
62
62
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eventer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-04-14 00:00:00.000000000Z
12
+ date: 2011-05-16 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
- requirement: &69651220 !ruby/object:Gem::Requirement
16
+ requirement: &69597080 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.0.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *69651220
24
+ version_requirements: *69597080
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &69650990 !ruby/object:Gem::Requirement
27
+ requirement: &69596850 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 2.0.1
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *69650990
35
+ version_requirements: *69596850
36
36
  description: eventer is the simpest event throwing engine
37
37
  email:
38
38
  - 3aHyga@gmail.com
@@ -44,6 +44,7 @@ files:
44
44
  - Gemfile
45
45
  - Gemfile.lock
46
46
  - LICENSE
47
+ - README.en.md
47
48
  - README.md
48
49
  - Rakefile
49
50
  - eventer.gemspec