ruby-hooks 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ODYxMWI5M2UwZTAzMDQxNzVhYThkMWVhMGIzNzU2ZDdkMjAxMzkxNQ==
5
- data.tar.gz: !binary |-
6
- MWVjYTA2ZDI4ODY2YWVmNDY2NGNjNDQ5MjJkMDZjN2I3NjgzYjE3OQ==
2
+ SHA1:
3
+ metadata.gz: faf3e81331151394f1e31790f945f67194a4477d
4
+ data.tar.gz: 76c02d07f7f8bbe97b04fb29b2f03bab744dd827
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- YjY4NWYzOTc3NDc5N2Q0N2RjNTdkNmFmZDQyYTMwYTFkZTZlMmM2MTljNzYw
10
- ZmVjNzljMjg3NGY3YjFjYjZjNTZlOTFmOGE1NTAyMzVlOWNmNWVkYzIyY2Zi
11
- M2YwMjY2MTRhZTI1Y2Q2Y2FlOGViMGMzMDJmZmI2NjVjMWFlZGM=
12
- data.tar.gz: !binary |-
13
- YWI1NmQzYzJmZTk5N2IxNWIyODQ5NWYzZmJiY2IxMmI1YzdlNTgzMDI4MWQ3
14
- ODdlMjFjNDE0YTBiOGMzODE3NTE1ZjllMWJjMWFjNzY2MTc1NGQ3OTcwNzFi
15
- NDQwNTUxNzIxMjY5MmEwODdiNGI5M2MwNDIyNGQwNDE3NzJkNzI=
6
+ metadata.gz: 42d819cf30ef2957b783dfb9fb1a7b29e5c0c66ac3cfabe7bc7810f506d613f56c00e8da885b27db437f73635ed77292a461b4656470f336612e371df23072c0
7
+ data.tar.gz: 06ec442741c9e8d95221724f2af93dccc9b5b56e4a9579ff35054db4be822ce1c0c6258d0ad64c506582576d782cd79edff49222081b8432206a5206fe5ecd08
data/lib/ruby/hooks.rb ADDED
@@ -0,0 +1,8 @@
1
+ =begin
2
+ Copyright 2014 Michal Papis <mpapis@gmail.com>
3
+
4
+ See the file LICENSE for copying permission.
5
+ =end
6
+
7
+ require "ruby/hooks/version"
8
+ require "ruby/hooks/instance_hooks"
@@ -0,0 +1,23 @@
1
+ =begin
2
+ Copyright 2014 Michal Papis <mpapis@gmail.com>
3
+
4
+ See the file LICENSE for copying permission.
5
+ =end
6
+
7
+ # Allow extending object with plugins(modules)
8
+ module Ruby::Hooks::Extensible
9
+
10
+ # handle single parameters as well as arrays of params
11
+ # @param method [Symbol] name of the method to call
12
+ # @param plugins [Array] call method for every element of array
13
+ # @param plugins [Object] call method for the object
14
+ # @returns :nodoc: - unperdictable
15
+ def add_plugins(method, plugins = nil)
16
+ case plugins
17
+ when Array then plugins.each { |plugin| send(method, plugin) }
18
+ when nil then true
19
+ else send(method, plugins)
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,28 @@
1
+ =begin
2
+ Copyright 2014 Michal Papis <mpapis@gmail.com>
3
+
4
+ See the file LICENSE for copying permission.
5
+ =end
6
+
7
+ require "observer"
8
+ require "ruby/hooks/extensible"
9
+
10
+ # Wrapper for Observable module
11
+ class Ruby::Hooks::Hook
12
+ include Observable
13
+ include ::Ruby::Hooks::Extensible
14
+
15
+ # automatically extends Hook instance with given modules
16
+ # @option options [Array] :extend array of modules to extend hook instance
17
+ # @option options [Module] :extend module to extend hook instance
18
+ def initialize(options = {})
19
+ @options = options
20
+ add_plugins( :extend, options[:extends])
21
+ end
22
+
23
+ # allow writign code independent of the internals of Observable
24
+ def observers
25
+ @observer_peers
26
+ end
27
+
28
+ end
@@ -0,0 +1,30 @@
1
+ =begin
2
+ Copyright 2014 Michal Papis <mpapis@gmail.com>
3
+
4
+ See the file LICENSE for copying permission.
5
+ =end
6
+
7
+ require "ruby/hooks/hook"
8
+
9
+ # Helper to add multiple instance hooks
10
+ module Ruby::Hooks::InstanceHooks
11
+
12
+ # define instance hook method, it gives easy acces to Hook and
13
+ # it's methods
14
+ #
15
+ # @param name [Symbol] name of the hook handling method to define
16
+ # @param options [Hash] optional settigns for hook
17
+ # @option options [Module] :extends - the module to extend hook instance
18
+ # @return [Method or Symbol] exactly what method definition would return
19
+ def define_hook(name, options = {})
20
+ define_method(name) do
21
+ if hook = instance_variable_get(:"@#{name}")
22
+ then return hook
23
+ end
24
+ hook = Ruby::Hooks::Hook.new(options)
25
+ instance_variable_set(:"@#{name}", hook)
26
+ hook
27
+ end
28
+ end
29
+
30
+ end
@@ -1,6 +1,14 @@
1
+ =begin
2
+ Copyright 2014 Michal Papis <mpapis@gmail.com>
3
+
4
+ See the file LICENSE for copying permission.
5
+ =end
6
+
1
7
  module Ruby
2
- class Hooks
3
- VERSION = '0.0.1'
8
+ # Helpers for multiple publish/subscribe hooks
9
+ module Hooks
10
+ # Rubygems version of Ruby::Hooks
11
+ VERSION = '1.0.0'
4
12
  end
5
13
  end
6
14
 
@@ -0,0 +1,48 @@
1
+ =begin
2
+ Copyright 2014 Michal Papis <mpapis@gmail.com>
3
+
4
+ See the file LICENSE for copying permission.
5
+ =end
6
+
7
+ require 'test_helper'
8
+ require 'ruby/hooks'
9
+
10
+ class Test1
11
+ extend Ruby::Hooks::InstanceHooks
12
+ define_hook(:my_event_one)
13
+ end
14
+
15
+ class Observer1
16
+ attr_accessor :result
17
+ def update(x)
18
+ @result = x
19
+ end
20
+ end
21
+
22
+ class Observer2
23
+ attr_accessor :result
24
+ def log(x)
25
+ @result = x
26
+ end
27
+ end
28
+
29
+ describe Ruby::Hooks::Extensible do
30
+ subject do
31
+ Test1.new
32
+ end
33
+
34
+ before do
35
+ @object1 = Observer1.new
36
+ @object2 = Observer2.new
37
+ end
38
+
39
+ it "uses extended method to find single observer" do
40
+ subject.my_event_one.add_observer(@object1)
41
+ subject.my_event_one.add_observer(@object2, :log)
42
+ subject.my_event_one.changed
43
+ subject.my_event_one.notify_observers("msg")
44
+ @object1.result.must_equal("msg")
45
+ @object2.result.must_equal("msg")
46
+ end
47
+
48
+ end
@@ -0,0 +1,63 @@
1
+ =begin
2
+ Copyright 2014 Michal Papis <mpapis@gmail.com>
3
+
4
+ See the file LICENSE for copying permission.
5
+ =end
6
+
7
+ require 'test_helper'
8
+ require 'ruby/hooks'
9
+
10
+ module FindObserver
11
+ def find_observer(*arg)
12
+ result = nil
13
+ if changed?
14
+ if observers
15
+ result =
16
+ observers.find do |k, v|
17
+ k.send v, *arg
18
+ end
19
+ end
20
+ changed(false)
21
+ end
22
+ result and result.first
23
+ end
24
+ end
25
+
26
+ class Test2
27
+ extend Ruby::Hooks::InstanceHooks
28
+ define_hook(:my_event_two, :extends => FindObserver)
29
+ end
30
+
31
+ class Observer3
32
+ def test_me(x)
33
+ x == 3
34
+ end
35
+ end
36
+
37
+ class Observer4
38
+ def test_me(x)
39
+ x == 4
40
+ end
41
+ end
42
+
43
+ describe Ruby::Hooks::Extensible do
44
+ subject do
45
+ Test2.new
46
+ end
47
+
48
+ it "uses extended method to find single observer" do
49
+ subject.my_event_two.add_observer(Observer3.new, :test_me)
50
+ subject.my_event_two.add_observer(Observer4.new, :test_me)
51
+ subject.my_event_two.methods.map(&:to_sym).must_include :find_observer
52
+ subject.my_event_two.changed
53
+ subject.my_event_two.changed?.must_equal true
54
+ subject.my_event_two.find_observer(1).must_be_nil
55
+ subject.my_event_two.changed?.must_equal false
56
+ subject.my_event_two.changed
57
+ subject.my_event_two.find_observer(3).must_be_instance_of(Observer3)
58
+ subject.my_event_two.changed
59
+ subject.my_event_two.find_observer(4).must_be_instance_of(Observer4)
60
+ subject.my_event_two.find_observer(4).must_be_nil
61
+ end
62
+
63
+ end
@@ -0,0 +1,58 @@
1
+ =begin
2
+ Copyright 2014 Michal Papis <mpapis@gmail.com>
3
+
4
+ See the file LICENSE for copying permission.
5
+ =end
6
+
7
+ require 'test_helper'
8
+ require 'ruby/hooks/extensible'
9
+
10
+ class ExtensibleTestClass
11
+ include Ruby::Hooks::Extensible
12
+ attr_reader :calls
13
+ def initialize
14
+ @calls = []
15
+ end
16
+ def count(name)
17
+ @calls << name
18
+ end
19
+ module Extra
20
+ def method_1
21
+ end
22
+ end
23
+ module Test1
24
+ end
25
+ module Test2
26
+ end
27
+ module Test3
28
+ end
29
+ end
30
+
31
+ describe Ruby::Hooks::Extensible do
32
+ subject do
33
+ ExtensibleTestClass.new
34
+ end
35
+
36
+ it "extends self" do
37
+ subject.methods.map(&:to_sym).wont_include :method_1
38
+ subject.add_plugins :extend, ExtensibleTestClass::Extra
39
+ subject.methods.map(&:to_sym).must_include :method_1
40
+ subject.calls.must_be_empty
41
+ end
42
+
43
+ it "calls single element" do
44
+ subject.add_plugins :count, ExtensibleTestClass::Test1
45
+ subject.calls.must_equal [ExtensibleTestClass::Test1]
46
+ end
47
+
48
+ it "calls each element" do
49
+ subject.add_plugins :count, [ExtensibleTestClass::Test1, ExtensibleTestClass::Test2, ExtensibleTestClass::Test3]
50
+ subject.calls.must_equal [ExtensibleTestClass::Test1, ExtensibleTestClass::Test2, ExtensibleTestClass::Test3]
51
+ end
52
+
53
+ it "calls nothing" do
54
+ subject.add_plugins :count
55
+ subject.calls.must_equal []
56
+ end
57
+
58
+ end
@@ -0,0 +1,29 @@
1
+ =begin
2
+ Copyright 2014 Michal Papis <mpapis@gmail.com>
3
+
4
+ See the file LICENSE for copying permission.
5
+ =end
6
+
7
+ require 'test_helper'
8
+ require 'ruby/hooks/hook'
9
+
10
+ class HookTestClass
11
+ module Extra
12
+ def method_2
13
+ end
14
+ end
15
+ end
16
+
17
+ describe Ruby::Hooks::Hook do
18
+ subject do
19
+ Ruby::Hooks::Hook.allocate
20
+ end
21
+
22
+ it "extends self" do
23
+ subject.must_be_kind_of(Observable)
24
+ subject.methods.map(&:to_sym).wont_include :method_2
25
+ subject.send :initialize, :extends => HookTestClass::Extra
26
+ subject.methods.map(&:to_sym).must_include :method_2
27
+ end
28
+
29
+ end
@@ -0,0 +1,35 @@
1
+ =begin
2
+ Copyright 2014 Michal Papis <mpapis@gmail.com>
3
+
4
+ See the file LICENSE for copying permission.
5
+ =end
6
+
7
+ require 'test_helper'
8
+ require 'ruby/hooks/instance_hooks'
9
+
10
+ class InstanceHooksTestClass
11
+ module Extra
12
+ def method3
13
+ end
14
+ end
15
+ extend Ruby::Hooks::InstanceHooks
16
+ define_hook(:test1)
17
+ define_hook(:test2, :extends => Extra)
18
+ end
19
+
20
+ describe Ruby::Hooks::Extensible do
21
+ subject do
22
+ InstanceHooksTestClass.new
23
+ end
24
+
25
+ it "defines and extends hooks" do
26
+ subject.methods.map(&:to_sym).must_include :test1
27
+ subject.methods.map(&:to_sym).must_include :test2
28
+ subject.test2.methods.map(&:to_sym).must_include :method3
29
+ end
30
+
31
+ it "reuses existing hook" do
32
+ subject.test2.must_equal subject.test2
33
+ end
34
+
35
+ end
@@ -0,0 +1,33 @@
1
+ =begin
2
+ Copyright 2014 Michal Papis <mpapis@gmail.com>
3
+
4
+ See the file LICENSE for copying permission.
5
+ =end
6
+
7
+ require 'rubygems'
8
+
9
+ if
10
+ RUBY_VERSION == "2.0.0" && # check Gemfile
11
+ $0 != "-e" # do not do that in guard
12
+ then
13
+ require "coveralls"
14
+ require "simplecov"
15
+
16
+ SimpleCov.start do
17
+ formatter SimpleCov::Formatter::MultiFormatter[
18
+ SimpleCov::Formatter::HTMLFormatter,
19
+ Coveralls::SimpleCov::Formatter,
20
+ ]
21
+ command_name "Unit Tests"
22
+ add_filter "/test/"
23
+ end
24
+
25
+ Coveralls.noisy = true unless ENV['CI']
26
+ end
27
+
28
+ # Autoload all lib/**/*.rb files so simplecov does not misses anything
29
+ Dir[File.expand_path("../../lib/**/*.rb", __FILE__)].each{|f| require f }
30
+
31
+ require 'minitest/autorun' unless $0=="-e" # skip in guard
32
+ require 'minitest/unit'
33
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-hooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michal Papis
@@ -14,14 +14,28 @@ dependencies:
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
25
39
  - !ruby/object:Gem::Version
26
40
  version: '0'
27
41
  description: Helpers for multiple publish/subscribe hooks
@@ -31,7 +45,17 @@ executables: []
31
45
  extensions: []
32
46
  extra_rdoc_files: []
33
47
  files:
48
+ - lib/ruby/hooks.rb
49
+ - lib/ruby/hooks/extensible.rb
50
+ - lib/ruby/hooks/hook.rb
51
+ - lib/ruby/hooks/instance_hooks.rb
34
52
  - lib/ruby/hooks/version.rb
53
+ - test/example1_test.rb
54
+ - test/example2_test.rb
55
+ - test/ruby/hooks/extensible_test.rb
56
+ - test/ruby/hooks/hook_test.rb
57
+ - test/ruby/hooks/instance_hooks_test.rb
58
+ - test/test_helper.rb
35
59
  homepage: https://github.com/remote-exec/ruby-hooks
36
60
  licenses:
37
61
  - MIT
@@ -42,18 +66,25 @@ require_paths:
42
66
  - lib
43
67
  required_ruby_version: !ruby/object:Gem::Requirement
44
68
  requirements:
45
- - - ! '>='
69
+ - - '>='
46
70
  - !ruby/object:Gem::Version
47
71
  version: '0'
48
72
  required_rubygems_version: !ruby/object:Gem::Requirement
49
73
  requirements:
50
- - - ! '>='
74
+ - - '>='
51
75
  - !ruby/object:Gem::Version
52
76
  version: '0'
53
77
  requirements: []
54
78
  rubyforge_project:
55
- rubygems_version: 2.4.1
79
+ rubygems_version: 2.4.2
56
80
  signing_key:
57
81
  specification_version: 4
58
82
  summary: Helpers for multiple publish/subscribe hooks
59
- test_files: []
83
+ test_files:
84
+ - test/ruby/hooks/extensible_test.rb
85
+ - test/ruby/hooks/hook_test.rb
86
+ - test/ruby/hooks/instance_hooks_test.rb
87
+ - test/test_helper.rb
88
+ - test/example2_test.rb
89
+ - test/example1_test.rb
90
+ has_rdoc: