event_object 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7078b01a38aaeae3a94e66a5fa514788bd2a0719
4
+ data.tar.gz: e372d4c456670a72b566bf5e360b8917637bc0cd
5
+ SHA512:
6
+ metadata.gz: ba6bee4551c6502e8335f161f688de7a1804b13dbbc8bb779f56f98386e5146219c67865236aca2b7756a659d673b5a810d1bfb91d1ac7578ebc55d6e4181cbf
7
+ data.tar.gz: 5a0b4328ffeb3d9cc3526d7c4a32f737dd1bf17a57bb020b6cfa9dc8a39549c24f1a856168ef327f59f21af66a4f7ce447ebed50870a87d1c44a6eac30f29863
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in event-object.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 che
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,42 @@
1
+ # Event::Object
2
+
3
+ EventObject - event-driven programming for ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'event-object'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install event-object
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ using EventObject
25
+ class EventEmitter
26
+ events :success, :error
27
+ end
28
+
29
+ emitter = EventEmitter.new
30
+ emitter.on :success do
31
+ [200, {}, "OK"]
32
+ end
33
+ p emitter.success!
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it ( https://github.com/[my-github-username]/event-object/fork )
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'event_object/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "event_object"
8
+ spec.version = EventObject::VERSION
9
+ spec.authors = ["che"]
10
+ spec.email = ["chelovekov@gmail.com"]
11
+ spec.summary = %q{EventObject - do ruby object to be event-driven }
12
+ #spec.description = %q{TODO: Write a longer description. Optional.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.required_ruby_version = '>= 2.0.0'
17
+ spec.required_rubygems_version = '>= 2.3.0'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0")
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0.0"
27
+ end
@@ -0,0 +1,16 @@
1
+ module EventObject
2
+ class Array
3
+ # array like event
4
+ def fire(*a, &b)
5
+ each { |h| h.call(*a, &b) }
6
+ end
7
+
8
+ def fire_for(t, *a)
9
+ each { |h| t.instance_exec(*a, &h) }
10
+ end
11
+
12
+ def off(proc=nil)
13
+ (proc) ? delete(proc) : clear
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module Event
2
+ refine Class
3
+ def def(method, &b)
4
+ define_method method, &b
5
+ end
6
+
7
+ def events(*names)
8
+ names.each { |name| self.def(name) { var(name, Array) } }
9
+ names.each { |name| self.def("#{name}!") { |*a| var(name, Array).fire(*a) } }
10
+ end
11
+
12
+ def var(name, init=nil)
13
+ key = "@#{name}"
14
+ class_variable_defined?(key) ? class_variable_get(key) : class_variable_set(key, init.new)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,38 @@
1
+ module Event
2
+ refine Object
3
+ def def(method, &b)
4
+ define_singleton_method method, &b
5
+ end
6
+
7
+ def var(name, init=nil)
8
+ instance_variable_get("@#{name}") || instance_variable_set("@#{name}", init.new)
9
+ end
10
+
11
+ def on(name, &b)
12
+ var(name, Array).tap { |e| e << b }; b
13
+ end
14
+
15
+ #
16
+ # obj.off(:data)
17
+ # obj.off(:data, proc)
18
+ # obj.off(target, :data, proc) => target.off(:data, proc)
19
+ def off(*a)
20
+ case a.size
21
+ when 1 then var(a[0], Array).clear
22
+ when 2 then var(a[0], Array).tap { |e| e.delete a[1] }
23
+ when 3 then a[0].off(a[1], a[2])
24
+ end
25
+ self
26
+ end
27
+
28
+ def once(name, &b)
29
+ (ev = nil).tap { |ev|
30
+ var(name, Array).tap { |e| e << (ev = ->() { b.call; e.delete(ev) }) }
31
+ }
32
+ end
33
+
34
+ def listen(object, name, meth)
35
+ object.on :name, &method(meth)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,4 @@
1
+ require 'event_object/array'
2
+ require 'event_object/class'
3
+ require 'event_object/object'
4
+ require 'event_object/nil_class'
@@ -0,0 +1,16 @@
1
+ module EventObject
2
+ refine Array do
3
+ # array like event
4
+ def fire(*a, &b)
5
+ collect { |h| h.call(*a, &b) }
6
+ end
7
+
8
+ def fire_for(t, *a)
9
+ collect { |h| t.instance_exec(*a, &h) }
10
+ end
11
+
12
+ def off(proc=nil)
13
+ (proc) ? delete(proc) : clear
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module EventObject
2
+ refine Class do
3
+ def def(met, &b)
4
+ define_method met, &b
5
+ end
6
+
7
+ def events(*ev)
8
+ ev.each { |name| self.def(name) { var(name, Array) } }
9
+ ev.each { |name| self.def("#{name}!") { |*a| var(name, Array).fire(*a) } }
10
+ end
11
+
12
+ def var(name, init)
13
+ key = "@#{name}"
14
+ class_variable_defined?(key) ? class_variable_get(key) : class_variable_set(key, init.new)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ module EventObject
2
+ refine NilClass do
3
+ def fire(*a)
4
+ []
5
+ end
6
+
7
+ def fire_for(t, *a)
8
+ []
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,38 @@
1
+ module EventObject
2
+ refine Object do
3
+ def def(method, &b)
4
+ define_singleton_method method, &b
5
+ end
6
+
7
+ def var(name, init)
8
+ instance_variable_get("@#{name}") || instance_variable_set("@#{name}", init.new)
9
+ end
10
+
11
+ def on(name, &b)
12
+ var(name, Array).tap { |e| e << b }; b
13
+ end
14
+
15
+ #
16
+ # obj.off(:data)
17
+ # obj.off(:data, proc)
18
+ # obj.off(target, :data, proc) => target.off(:data, proc)
19
+ def off(*a)
20
+ case a.size
21
+ when 1 then var(a[0], Array).clear
22
+ when 2 then var(a[0], Array).tap { |e| e.delete a[1] }
23
+ when 3 then a[0].off(a[1], a[2])
24
+ end
25
+ self
26
+ end
27
+
28
+ def once(name, &b)
29
+ (ev = nil).tap { |ev|
30
+ var(name, Array).tap { |e| e << (ev = ->() { b.call; e.delete(ev) }) }
31
+ }
32
+ end
33
+
34
+ def listen(object, name, meth)
35
+ object.on :name, &method(meth)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module EventObject
2
+ VERSION = "0.9.0"
3
+ end
@@ -0,0 +1,43 @@
1
+ require "spec_helper"
2
+ describe EventObject do
3
+ using EventObject
4
+
5
+ it "should create array of events" do
6
+ events = []
7
+ proc = lambda { :result }
8
+ events.push proc
9
+ events_with_arguments = []
10
+ events_with_arguments.push lambda { |num| num+1 }
11
+
12
+ expect(events.fire.last).to be(:result)
13
+ expect(events.fire_for(self).last).to be(:result)
14
+ expect(events_with_arguments.fire(5).last).to be(6)
15
+
16
+ events.off(proc)
17
+ expect(events).to be_empty
18
+ end
19
+
20
+ it "should NilClass has fire" do
21
+ expect(nil.fire).to eq([])
22
+ expect(nil.fire_for(self)).to eq([])
23
+ end
24
+
25
+ it "should create events and fire them" do
26
+ class EventEmitter
27
+ events :success, :error
28
+ end
29
+
30
+ emitter = EventEmitter.new
31
+ emitter.on :success do
32
+ 18
33
+ end
34
+ emitter.on :success do
35
+ 42
36
+ end
37
+
38
+ expect(emitter.success!).to eq([18,42])
39
+ expect(emitter.success.fire).to eq([18,42])
40
+ expect(emitter.error).to be_empty
41
+ expect(emitter.error.fire).to be_empty
42
+ end
43
+ end
@@ -0,0 +1,8 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require File.expand_path("./lib/event_object")
5
+
6
+ RSpec.configure do |config|
7
+ config.color = true
8
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: event_object
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - che
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ description:
56
+ email:
57
+ - chelovekov@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - event_object.gemspec
68
+ - lib/event/array.rb
69
+ - lib/event/class.rb
70
+ - lib/event/object.rb
71
+ - lib/event_object.rb
72
+ - lib/event_object/array.rb
73
+ - lib/event_object/class.rb
74
+ - lib/event_object/nil_class.rb
75
+ - lib/event_object/object.rb
76
+ - lib/event_object/version.rb
77
+ - spec/event_object_spec.rb
78
+ - spec/spec_helper.rb
79
+ homepage: ''
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 2.0.0
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 2.3.0
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.4.1
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: EventObject - do ruby object to be event-driven
103
+ test_files:
104
+ - spec/event_object_spec.rb
105
+ - spec/spec_helper.rb