seh 0.0.1

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.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *~
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2@seh
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development, :test do
4
+ gem 'rspec'
5
+ end
6
+
7
+ # Specify your gem's dependencies in seh.gemspec
8
+ gemspec
@@ -0,0 +1,4 @@
1
+
2
+ * seh: structured event handler
3
+
4
+
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,20 @@
1
+ require "seh/version"
2
+ require "seh/event_type"
3
+ require "seh/event_target"
4
+ require "seh/event"
5
+
6
+ module Seh
7
+ class << self
8
+ def and( *types )
9
+ EventType::And.new *types
10
+ end
11
+
12
+ def or( *types )
13
+ EventType::Or.new *types
14
+ end
15
+
16
+ def not( type )
17
+ EventType::Not.new type
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,79 @@
1
+ require 'ostruct'
2
+
3
+ module Seh
4
+ module Private
5
+ class EventData
6
+ attr_accessor :types, :target, :time, :start, :finish
7
+
8
+ def initialize
9
+ @types = []
10
+ @target = nil
11
+ @time = Time.now
12
+
13
+ # staged handlers
14
+ @start = []
15
+ @finish = []
16
+ end
17
+ end
18
+
19
+ class EventStateReady
20
+ class << self
21
+ def type( data, t )
22
+ data.types << t
23
+ end
24
+ end
25
+ end
26
+
27
+ class EventStateInflight
28
+ end
29
+
30
+ class EventStateDone
31
+ end
32
+ end
33
+
34
+ class Event < OpenStruct
35
+ def initialize(target, &block)
36
+ super()
37
+ raise "Event expects a target" unless target
38
+ raise "Event expects the target to include EventTarget" unless target.class.include? EventTarget
39
+ @state = Private::EventStateReady
40
+ @data = Private::EventData.new
41
+ @data.target = target
42
+ instance_eval(&block) if block
43
+ end
44
+
45
+ def dispatch
46
+ raise "Event may only be dispatched once" unless @state == Private::EventStateReady
47
+ @state = Private::EventStateInflight
48
+ @data.target.each_bind { |bind| bind.block.call self if bind.event_type.match @data.types }
49
+ @data.start.each { |block| block.call self }
50
+ @data.finish.each { |block| block.call self }
51
+ @state = Private::EventStateDone
52
+ end
53
+
54
+ def target
55
+ @data.target
56
+ end
57
+
58
+ def type( event_type )
59
+ @state.type @data, event_type
60
+ end
61
+
62
+ def match_type( event_type )
63
+ event_type = EventType.new event_type unless event_type.is_a? EventType
64
+ event_type.match @data.types
65
+ end
66
+
67
+ def time
68
+ @data.time.dup
69
+ end
70
+
71
+ def start(&block)
72
+ @data.start << block if block_given?
73
+ end
74
+
75
+ def finish(&block)
76
+ @data.finish << block if block_given?
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,35 @@
1
+ module Seh
2
+ module EventTarget
3
+ def bind( event_type=nil, &block )
4
+ raise "EventTarget::bind expects a block" unless block_given?
5
+ @_binds ||= []
6
+ bind = Private::EventBind.new( event_type, &block )
7
+ @_binds << bind
8
+ ->{ @_binds.delete bind }
9
+ end
10
+
11
+ def bind_once( event_type=nil, &block )
12
+ return unless block_given?
13
+ disconnect = self.bind(event_type) { |event| disconnect.call; block.call event }
14
+ disconnect
15
+ end
16
+
17
+ def each_bind
18
+ @_binds ||= []
19
+ @_binds.dup.each { |b| yield b }
20
+ nil
21
+ end
22
+ end
23
+
24
+ module Private
25
+ class EventBind
26
+ attr_reader :event_type, :block
27
+
28
+ def initialize( event_type, &block )
29
+ event_type = EventType.new event_type unless event_type.is_a? EventType
30
+ @event_type = event_type
31
+ @block = block
32
+ end
33
+ end # EventBind
34
+ end # Private
35
+ end
@@ -0,0 +1,58 @@
1
+ module Seh
2
+ class EventType
3
+ attr_reader :type
4
+
5
+ def initialize( type )
6
+ @type = type
7
+ end
8
+
9
+ def match(types)
10
+ types = [types] unless types.respond_to? :each
11
+ _match types
12
+ end
13
+
14
+ private
15
+ def _match(types)
16
+ return true if not self.type or types.include? self.type
17
+ false
18
+ end
19
+
20
+ class And < EventType
21
+ def initialize(*types)
22
+ @types = []
23
+ types.each { |t| t = EventType.new t unless t.kind_of? EventType ; @types << t }
24
+ end
25
+
26
+ private
27
+ def _match(types)
28
+ @types.each { |t| return false unless t.match types }
29
+ true
30
+ end
31
+ end # And
32
+
33
+ class Or < EventType
34
+ def initialize(*types)
35
+ @types = []
36
+ types.each { |t| t = EventType.new t unless t.kind_of? EventType ; @types << t }
37
+ end
38
+
39
+ private
40
+ def _match(types)
41
+ @types.each { |t| return true if t.match types }
42
+ false
43
+ end
44
+ end # Or
45
+
46
+ class Not < EventType
47
+ def initialize(type)
48
+ type = EventType.new type unless type.kind_of? EventType
49
+ @type = type
50
+ end
51
+
52
+ private
53
+ def _match(types)
54
+ ! @type.match types
55
+ end
56
+ end # Not
57
+ end # EventType
58
+ end
@@ -0,0 +1,3 @@
1
+ module Seh
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "seh/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "seh"
7
+ s.version = Seh::VERSION
8
+ s.authors = ["Ryan Berckmans"]
9
+ s.email = ["ryan.berckmans@gmail.com"]
10
+ s.homepage = "https://github.com/ryanberckmans/seh"
11
+ s.summary = "event handling similar to w3c dom events; pre-alpha wip"
12
+ s.description = ""
13
+
14
+ s.rubyforge_project = "seh"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,4 @@
1
+
2
+ # stub
3
+
4
+
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Berckmans
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-06-15 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: ''
15
+ email:
16
+ - ryan.berckmans@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rvmrc
23
+ - Gemfile
24
+ - README.org
25
+ - Rakefile
26
+ - lib/seh.rb
27
+ - lib/seh/event.rb
28
+ - lib/seh/event_target.rb
29
+ - lib/seh/event_type.rb
30
+ - lib/seh/version.rb
31
+ - seh.gemspec
32
+ - spec/spec_helper.rb
33
+ homepage: https://github.com/ryanberckmans/seh
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project: seh
53
+ rubygems_version: 1.8.5
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: event handling similar to w3c dom events; pre-alpha wip
57
+ test_files: []