ruby-hooks 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: faf3e81331151394f1e31790f945f67194a4477d
4
- data.tar.gz: 76c02d07f7f8bbe97b04fb29b2f03bab744dd827
3
+ metadata.gz: 3d1ab49d7471dbf83250ef6097e8633e42cf5029
4
+ data.tar.gz: 031177c8eaf440f8f907ab6aa659df19945f277c
5
5
  SHA512:
6
- metadata.gz: 42d819cf30ef2957b783dfb9fb1a7b29e5c0c66ac3cfabe7bc7810f506d613f56c00e8da885b27db437f73635ed77292a461b4656470f336612e371df23072c0
7
- data.tar.gz: 06ec442741c9e8d95221724f2af93dccc9b5b56e4a9579ff35054db4be822ce1c0c6258d0ad64c506582576d782cd79edff49222081b8432206a5206fe5ecd08
6
+ metadata.gz: f43eaed97d10494d1d899e50dbb5903eaf6aa95508cd10a23201010af5e527f8248ddeeb13a0bb286ee64e5854f644a03cf34b9df39ab6e4b548a2b41180f4a3
7
+ data.tar.gz: fdcd36619942ba0cd4c35485c9dc1e4ba023e29518dc03b1b7aced17b8eae2a450732c073e0b2cbe660724988aad6fc765972639a22a749f97e2efec6cbeb6e0
@@ -13,16 +13,29 @@ class Ruby::Hooks::Hook
13
13
  include ::Ruby::Hooks::Extensible
14
14
 
15
15
  # automatically extends Hook instance with given modules
16
+ #
16
17
  # @option options [Array] :extend array of modules to extend hook instance
17
18
  # @option options [Module] :extend module to extend hook instance
18
19
  def initialize(options = {})
19
20
  @options = options
21
+ @observer_peers = {}
20
22
  add_plugins( :extend, options[:extends])
21
23
  end
22
24
 
23
25
  # allow writign code independent of the internals of Observable
26
+ #
27
+ # @return [Array] array of observers defined for the hook
24
28
  def observers
25
29
  @observer_peers
26
30
  end
27
31
 
32
+ # convienience method to set changed state and notify observers in one call
33
+ #
34
+ # @param args [Array] arguments to pass to +notify_observers+
35
+ # @return [nil|false] forwards result of +notify_observers+
36
+ def changed_and_notify(*args)
37
+ changed
38
+ notify_observers(*args)
39
+ end
40
+
28
41
  end
@@ -8,7 +8,7 @@ module Ruby
8
8
  # Helpers for multiple publish/subscribe hooks
9
9
  module Hooks
10
10
  # Rubygems version of Ruby::Hooks
11
- VERSION = '1.0.0'
11
+ VERSION = "1.1.0"
12
12
  end
13
13
  end
14
14
 
@@ -4,8 +4,8 @@ Copyright 2014 Michal Papis <mpapis@gmail.com>
4
4
  See the file LICENSE for copying permission.
5
5
  =end
6
6
 
7
- require 'test_helper'
8
- require 'ruby/hooks'
7
+ require "test_helper"
8
+ require "ruby/hooks"
9
9
 
10
10
  class Test1
11
11
  extend Ruby::Hooks::InstanceHooks
@@ -4,8 +4,8 @@ Copyright 2014 Michal Papis <mpapis@gmail.com>
4
4
  See the file LICENSE for copying permission.
5
5
  =end
6
6
 
7
- require 'test_helper'
8
- require 'ruby/hooks'
7
+ require "test_helper"
8
+ require "ruby/hooks"
9
9
 
10
10
  module FindObserver
11
11
  def find_observer(*arg)
@@ -4,8 +4,8 @@ Copyright 2014 Michal Papis <mpapis@gmail.com>
4
4
  See the file LICENSE for copying permission.
5
5
  =end
6
6
 
7
- require 'test_helper'
8
- require 'ruby/hooks/extensible'
7
+ require "test_helper"
8
+ require "ruby/hooks/extensible"
9
9
 
10
10
  class ExtensibleTestClass
11
11
  include Ruby::Hooks::Extensible
@@ -4,14 +4,20 @@ Copyright 2014 Michal Papis <mpapis@gmail.com>
4
4
  See the file LICENSE for copying permission.
5
5
  =end
6
6
 
7
- require 'test_helper'
8
- require 'ruby/hooks/hook'
7
+ require "test_helper"
8
+ require "ruby/hooks/hook"
9
9
 
10
10
  class HookTestClass
11
11
  module Extra
12
12
  def method_2
13
13
  end
14
14
  end
15
+ class Update
16
+ attr_reader :value
17
+ def update(value)
18
+ @value = value
19
+ end
20
+ end
15
21
  end
16
22
 
17
23
  describe Ruby::Hooks::Hook do
@@ -19,8 +25,40 @@ describe Ruby::Hooks::Hook do
19
25
  Ruby::Hooks::Hook.allocate
20
26
  end
21
27
 
22
- it "extends self" do
28
+ it "is based on Observable" do
23
29
  subject.must_be_kind_of(Observable)
30
+ end
31
+
32
+ it "initializes without params" do
33
+ subject.send :initialize
34
+ subject.methods.map(&:to_sym).wont_include :method_2
35
+ end
36
+
37
+ describe "observer interaction" do
38
+
39
+ let(:observer) do
40
+ HookTestClass::Update.new
41
+ end
42
+
43
+ it "gives access to observers" do
44
+ subject.send :initialize
45
+ subject.methods.map(&:to_sym).must_include :observers
46
+ subject.observers.must_be_empty
47
+ subject.add_observer(observer)
48
+ subject.observers.must_equal({observer => :update})
49
+ end
50
+
51
+ it "allows fast execution" do
52
+ subject.send :initialize
53
+ subject.methods.map(&:to_sym).must_include :changed_and_notify
54
+ subject.add_observer(observer)
55
+ subject.changed_and_notify(7)
56
+ observer.value.must_equal(7)
57
+ end
58
+
59
+ end # context "observer interaction"
60
+
61
+ it "extends self" do
24
62
  subject.methods.map(&:to_sym).wont_include :method_2
25
63
  subject.send :initialize, :extends => HookTestClass::Extra
26
64
  subject.methods.map(&:to_sym).must_include :method_2
@@ -4,8 +4,8 @@ Copyright 2014 Michal Papis <mpapis@gmail.com>
4
4
  See the file LICENSE for copying permission.
5
5
  =end
6
6
 
7
- require 'test_helper'
8
- require 'ruby/hooks/instance_hooks'
7
+ require "test_helper"
8
+ require "ruby/hooks/instance_hooks"
9
9
 
10
10
  class InstanceHooksTestClass
11
11
  module Extra
@@ -4,7 +4,7 @@ Copyright 2014 Michal Papis <mpapis@gmail.com>
4
4
  See the file LICENSE for copying permission.
5
5
  =end
6
6
 
7
- require 'rubygems'
7
+ require "rubygems"
8
8
 
9
9
  if
10
10
  RUBY_VERSION == "2.0.0" && # check Gemfile
@@ -22,12 +22,12 @@ then
22
22
  add_filter "/test/"
23
23
  end
24
24
 
25
- Coveralls.noisy = true unless ENV['CI']
25
+ Coveralls.noisy = true unless ENV["CI"]
26
26
  end
27
27
 
28
28
  # Autoload all lib/**/*.rb files so simplecov does not misses anything
29
29
  Dir[File.expand_path("../../lib/**/*.rb", __FILE__)].each{|f| require f }
30
30
 
31
- require 'minitest/autorun' unless $0=="-e" # skip in guard
32
- require 'minitest/unit'
31
+ require "minitest/autorun" unless $0=="-e" # skip in guard
32
+ require "minitest/unit"
33
33
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-hooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michal Papis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-05 00:00:00.000000000 Z
11
+ date: 2014-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -82,8 +82,8 @@ specification_version: 4
82
82
  summary: Helpers for multiple publish/subscribe hooks
83
83
  test_files:
84
84
  - test/ruby/hooks/extensible_test.rb
85
- - test/ruby/hooks/hook_test.rb
86
85
  - test/ruby/hooks/instance_hooks_test.rb
86
+ - test/ruby/hooks/hook_test.rb
87
87
  - test/test_helper.rb
88
88
  - test/example2_test.rb
89
89
  - test/example1_test.rb