inferno 0.0.2

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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ Y2I3OGVjMDNiZWRkODgyMTAzYmQ1YjcxNTBkMGVjZjk4ZDkwNThmZg==
5
+ data.tar.gz: !binary |-
6
+ Njg0NzNmMDY1OTg0ZjY5YjEwM2MwZDk5YmY4N2JlZTlkYjY0M2M1NQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZjIwMWZmZGQ5MjA3MDlhNDhjNTY4YmYwZWQ4MDM4NzgyOGViZDJkZDk1NDM4
10
+ YjdiNzk0NGEwY2NkNGFjZTliMzRmMDA4NjA3OWM3MDkzMGNlMGMyYmVhZjZm
11
+ NjA4NDI5MWFiZjEzODM3MDM5OGVmYTA3NzEwYTcxMGYxYTdlOTk=
12
+ data.tar.gz: !binary |-
13
+ MTgyZTY5OTQxMjc2ZDBkOTFjMTQwMjQ1OTA2ZTBjY2Q0MmQ3Y2U1MTFiZGFl
14
+ NDQxOGRkNjc4ZmJlODk0NThlYTE3YjU3NDc2YWQ1YmQ2YjU5ZjkyM2FlY2Iz
15
+ YjY1OTQxYTg4OTQwMWNkMTgwNzBmOTRiMTllYjliMmQ0Y2M2MDE=
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in inferno.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Arkadiusz Buras
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ # Inferno
2
+
3
+ Gem gives object the ability to bind and trigger custom named events. Events do not have to be declared before they are bound, and may take passed arguments.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'inferno'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install inferno
18
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'inferno/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "inferno"
8
+ spec.version = Inferno::VERSION
9
+ spec.authors = ["Arkadiusz Buras"]
10
+ spec.email = ["macbury@gmail.com"]
11
+ spec.summary = "Gem gives object the ability to bind and trigger custom named events. Events do not have to be declared before they are bound, and may take passed arguments. "
12
+ spec.homepage = "http://macbury.pl"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.5"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_dependency "eventmachine"
23
+ end
@@ -0,0 +1,109 @@
1
+ require "inferno/version"
2
+ require "eventmachine"
3
+ module Inferno
4
+ class Event
5
+ def initialize
6
+ @events = {}
7
+ @fibers = []
8
+ end
9
+
10
+ def fibers
11
+ @fibers
12
+ end
13
+
14
+ def events
15
+ @events.keys
16
+ end
17
+
18
+ # Just like on, but causes the bound callback to only fire once before being removed.
19
+ # Handy for saying "the next time that X happens, do this".
20
+ # @param [String] [event name]
21
+ # @param [Object] [on what object run callback]
22
+ # @param [Proc] [callback]
23
+ def once(event, context, &callback)
24
+ on(event, context, &callback)
25
+ on(event, self) { off(event) }
26
+ end
27
+
28
+ # Bind a callback function to an object. The callback will be invoked whenever the event is fired.
29
+ # @param [String] [event name]
30
+ # @param [Object] [on what object run callback]
31
+ # @param [Proc] [callback with proc]
32
+ def on(event, context, &callback)
33
+ @events[event] ||= {}
34
+ @events[event][context] = callback
35
+ end
36
+
37
+ def count(event)
38
+ @events[event] ? @events[event].size : 0
39
+ end
40
+
41
+ # Remove a previously-bound callback function from an object.
42
+ # If no context is specified, all of the versions of the callback with different contexts will be removed.
43
+ # If no event is specified, callbacks for all events will be removed.
44
+ # @param [String] [event name]
45
+ # @param [Object] [on what object run callback]
46
+ def off(event,context=nil)
47
+ if @events[event]
48
+ if context
49
+ @events[event].delete(context)
50
+ else
51
+ @events[event].clear
52
+ end
53
+ @events.delete(event) if @events[event].empty?
54
+ end
55
+ end
56
+
57
+ # Trigger callbacks for the given event, or space-delimited list of events. Subsequent arguments to trigger will be passed along to the event callbacks.
58
+ # @param [String] [event name]
59
+ # @param [Hash] [payload to send]
60
+ def trigger(event, payload={})
61
+ broadcast(false, "triggered.event", { event: event, payload: payload })
62
+ broadcast(true, event, payload)
63
+ end
64
+
65
+ private
66
+
67
+ def pool_fiber
68
+ @fibers << Fiber.new { |block| loop { block = fiber_loop(block) } } if @fibers.empty?
69
+ @fibers.shift
70
+ end
71
+
72
+ def in_fiber(&block)
73
+ pool_fiber.resume(block)
74
+ end
75
+
76
+ def fiber_loop(block)
77
+ block.call
78
+ @fibers.unshift Fiber.current
79
+ Fiber.yield
80
+ end
81
+
82
+ # Run block in fiber and next reactor tick
83
+ # @param [Proc] [Block to run in next tick if eventmachine reactor is running]
84
+ def schedule(&block)
85
+ fiber = pool_fiber
86
+ if defined?(EM) && EM.reactor_running?
87
+ EM.next_tick { fiber.resume(block) }
88
+ else
89
+ fiber.resume(block)
90
+ end
91
+ end
92
+
93
+ def broadcast(in_fiber, event, payload={})
94
+ list = @events[event] || []
95
+
96
+ list.each do |context, block|
97
+ if in_fiber
98
+ schedule { run_in_context(context, payload, &block) }
99
+ else
100
+ run_in_context(context, payload, &block)
101
+ end
102
+ end
103
+ end
104
+
105
+ def run_in_context(context, payload, &block)
106
+ context.instance_exec(payload, &block)
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,3 @@
1
+ module Inferno
2
+ VERSION = "0.0.2"
3
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inferno
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Arkadiusz Buras
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ~>
17
+ - !ruby/object:Gem::Version
18
+ version: '1.5'
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: '1.5'
24
+ name: bundler
25
+ type: :development
26
+ prerelease: false
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: !binary |-
33
+ MA==
34
+ version_requirements: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: !binary |-
39
+ MA==
40
+ name: rake
41
+ type: :development
42
+ prerelease: false
43
+ - !ruby/object:Gem::Dependency
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: !binary |-
49
+ MA==
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: !binary |-
55
+ MA==
56
+ name: eventmachine
57
+ type: :runtime
58
+ prerelease: false
59
+ description:
60
+ email:
61
+ - macbury@gmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - inferno.gemspec
72
+ - lib/inferno.rb
73
+ - lib/inferno/version.rb
74
+ homepage: http://macbury.pl
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: !binary |-
87
+ MA==
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: !binary |-
93
+ MA==
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.0.6
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Gem gives object the ability to bind and trigger custom named events. Events
100
+ do not have to be declared before they are bound, and may take passed arguments.
101
+ test_files: []