ianwhite-hark 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b71c62889085f5896f266c976ae49d855d550917
4
+ data.tar.gz: 9f69d753ec889c9cf354b1586c68313604cbeaf3
5
+ SHA512:
6
+ metadata.gz: 7c690463f3539c9b835b3ed34b09cdcaa64d642f37a51eb63f441812274888b7ee93f8f1ce40b6afa1de957b3f9056de68b7cd2860b6181d10898eca851f7425
7
+ data.tar.gz: f79b3613750fc960271a57091f966ea02b6b17a35f6c9d19087b35715d3b6e4ac154efc53a1390adbe6b5a6a5692fa69970d98402da2c90466c2ec0a09ff3c41
data/.gitignore ADDED
@@ -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 hark.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec, all_on_start: true do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ian White
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.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Hark
2
+
3
+ Create an ad-hoc listener object with hark.
4
+
5
+ The idea behind hark is that the objects that receive listeners shouldn't need to perform any ceremony on
6
+ them, just treat them as objects that respond to messages. It's up to the caller to provide these lsitener objects,
7
+ and to decide how they behave (re: lax), perhaps smushing together listeners (re: add). If required, these ad-hoc
8
+ listeners can easily be refactored into classes in their own right, as the recievers don't need to know anything about
9
+ hark.
10
+
11
+ listener = hark success: ->{ "succeeded" }, failure: ->{ "failed" }
12
+ listener.success # => ["succeeded"]
13
+ listener.failure # => ["failed"]
14
+ listener.unknown # raises NoMethodError
15
+
16
+ Listeners return an array of return values, but using return values is discouraged (tell don't ask)
17
+
18
+ To create a listener that silently swallows unknown messages, send it the #lax method
19
+
20
+ listener = hark(success: ->{ "succeeded" }).lax
21
+ listener.success # => ["succeeded"]
22
+ listener.unknown # => []
23
+
24
+ To make a lax listener strict again, send it the #strict method
25
+
26
+ listener = listener.strict
27
+
28
+ To smush together listeners, use #hark
29
+
30
+ listener = listener.hark other_listener
31
+ listener = listener.hark emailer, logger
32
+ listener = hark(emailer, logger, twitter_notifier)
33
+
34
+ To add new messages to a listener, use #hark
35
+
36
+ listener = listener.hark(:success) { "extra success" }
37
+ listener.success # => ["success", "extra success"]
38
+
39
+ To decorate an object (of any sort) so that it becomes a hark listener (and therefore can be smushed etc)
40
+
41
+ listener = object.to_hark
42
+
43
+ The listener is immutable, #strict, #lax, and #hark all return new listeners
44
+
45
+ ## Installation
46
+
47
+ Add this line to your application's Gemfile:
48
+
49
+ gem 'ianwhite-hark'
50
+
51
+ And then execute:
52
+
53
+ $ bundle
54
+
55
+ Or install it yourself as:
56
+
57
+ $ gem install ianwhite-hark
58
+
59
+ ## Usage
60
+
61
+ TODO: Write usage instructions here
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/hark.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hark/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ianwhite-hark"
8
+ spec.version = Hark::VERSION
9
+ spec.authors = ["Ian White"]
10
+ spec.email = ["ian.w.white@gmail.com"]
11
+ spec.description = %q{Create ad-hoc listener objects with impunity}
12
+ spec.summary = %q{Hark is a gem that enables writing code in a 'hexagonal architecture' style}
13
+ spec.homepage = "http://github.com/ianwhite/hark"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec-given"
24
+ spec.add_development_dependency "guard-rspec"
25
+ end
@@ -0,0 +1,47 @@
1
+ module Hark
2
+ # AdHoc is a tiny class to facilitate creating an ad-hoc object that from either a hash or proc.
3
+ #
4
+ # Eg. from a hash:
5
+ #
6
+ # handler = AdHoc.new(success: (o)-> { o.great_success }, failure: (o)-> { o.failed } )
7
+ #
8
+ # Eg. from a 'response' style block:
9
+ #
10
+ # handler = AdHoc.new do |on|
11
+ # on.success {|o| o.great_success }
12
+ # on.failure {|o| o.failed }
13
+ # end
14
+ #
15
+ # Eg. adding methods after creation
16
+ #
17
+ # obj = AdHoc.new
18
+ # obj.add_method!(:foo) { "bar" }
19
+ #
20
+ # All blocks keep their original binding. This makes AdHoc suitable for creating
21
+ # ad-hoc responses from controller type objects.
22
+ #
23
+ class AdHoc
24
+ def self.new hash = {}, &proc
25
+ super().tap do |ad_hoc|
26
+ AddMethodsFromProc.new(proc, ad_hoc) if block_given?
27
+ hash.each {|method, body| ad_hoc.add_method!(method, &body) }
28
+ end
29
+ end
30
+
31
+ def add_method!(method, &body)
32
+ singleton_class.send(:define_method, method) {|*args, &block| body.call(*args, &block) }
33
+ end
34
+
35
+ class AddMethodsFromProc
36
+ def initialize proc, ad_hoc
37
+ @ad_hoc = ad_hoc
38
+ proc.call(self)
39
+ end
40
+
41
+ def method_missing method, *, &body
42
+ @ad_hoc.add_method!(method, &body)
43
+ end
44
+ end
45
+ end
46
+ end
47
+
@@ -0,0 +1,9 @@
1
+ module Kernel
2
+ def hark *args, &block
3
+ Hark.from *args, &block
4
+ end
5
+
6
+ def to_hark *args, &block
7
+ Hark.from self, *args, &block
8
+ end
9
+ end
@@ -0,0 +1,53 @@
1
+ module Hark
2
+ class Dispatcher
3
+ # from(:success) do
4
+ # "success"
5
+ # end
6
+ #
7
+ # from(success: ->{ "success" })
8
+ #
9
+ # from do |on|
10
+ # on.success { "success" }
11
+ # end
12
+ #
13
+ def self.from(*args, &block)
14
+ if block
15
+ if args.last.is_a?(Symbol)
16
+ args << {args.pop => block}
17
+ elsif args.empty?
18
+ args << block
19
+ end
20
+ end
21
+
22
+ new args.map{|o| to_handler(o) }.flatten.freeze
23
+ end
24
+
25
+ def self.to_handler object
26
+ case object
27
+ when Listener then object.dispatcher.handlers
28
+ when Dispatcher then object.handlers
29
+ when Hash then AdHoc.new(object)
30
+ when Proc then AdHoc.new(&object)
31
+ when Array then object.map{|o| to_handler(o) }
32
+ else object
33
+ end
34
+ end
35
+
36
+ attr_reader :handlers
37
+
38
+ def initialize handlers
39
+ @handlers = handlers
40
+ freeze
41
+ end
42
+
43
+ def handles? method
44
+ handlers.any? {|handler| handler.respond_to?(method) }
45
+ end
46
+
47
+ def handle method, *args, &block
48
+ handlers.each_with_object([]) do |handler, results|
49
+ results << handler.send(method, *args, &block) if handler.respond_to?(method)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,53 @@
1
+ module Hark
2
+ # A Listener holds a dispatcher, which it dispatches messages to
3
+ #
4
+ # A listener is by default a 'strict' listener, it will raise NoMethodError if
5
+ # it is sent a message it doesn't know how to handle.
6
+ #
7
+ # A listener can be turned into a 'lax' listener, by sending it the #lax message.
8
+ # A lax listener will silently swallow any unknown messages.
9
+ class Listener
10
+ def self.new *args, &block
11
+ self == Listener ? StrictListener.new(*args, &block) : super(*args, &block)
12
+ end
13
+
14
+ attr_reader :dispatcher
15
+
16
+ def initialize(*args, &block)
17
+ @dispatcher = Dispatcher.from(*args, &block)
18
+ freeze
19
+ end
20
+
21
+ def strict
22
+ StrictListener.new dispatcher
23
+ end
24
+
25
+ def lax
26
+ LaxListener.new dispatcher
27
+ end
28
+
29
+ def hark *args, &block
30
+ self.class.new dispatcher, *args, &block
31
+ end
32
+ end
33
+
34
+ class StrictListener < Listener
35
+ def respond_to_missing? method, *args
36
+ dispatcher.handles?(method) || super
37
+ end
38
+
39
+ def method_missing *args, &block
40
+ (results = dispatcher.handle(*args, &block)).any? ? results : super
41
+ end
42
+ end
43
+
44
+ class LaxListener < Listener
45
+ def respond_to_missing? *args
46
+ true
47
+ end
48
+
49
+ def method_missing *args, &block
50
+ dispatcher.handle(*args, &block)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module Hark
2
+ VERSION = "0.0.1"
3
+ end
data/lib/hark.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "hark/version"
2
+ require "hark/ad_hoc"
3
+ require "hark/dispatcher"
4
+ require "hark/listener"
5
+ require "hark/core_ext"
6
+
7
+ module Hark
8
+ def self.from(*args, &block)
9
+ Listener.new(*args, &block)
10
+ end
11
+ end
data/spec/hark_spec.rb ADDED
@@ -0,0 +1,115 @@
1
+ require 'spec_helper'
2
+ require 'hark'
3
+
4
+ describe Hark do
5
+ Given(:transcript) { [] }
6
+
7
+ class PlainListener < Struct.new(:transcript)
8
+ def success(value)
9
+ transcript.push [:succeeded, value]
10
+ end
11
+
12
+ def failure(value)
13
+ transcript.push [:failed, value]
14
+ end
15
+ end
16
+
17
+ shared_examples_for "a success/failure listener" do
18
+ describe "success" do
19
+ When { listener.success(42) }
20
+ Then { transcript == [[:succeeded, 42]] }
21
+ end
22
+
23
+ describe "failure" do
24
+ When { listener.failure(54) }
25
+ Then { transcript == [[:failed, 54]] }
26
+ end
27
+ end
28
+
29
+ shared_examples_for "a strict listener" do
30
+ Then { ! strict_listener.respond_to?(:unknown) }
31
+ And { (strict_listener.unknown rescue $!).is_a?(NoMethodError) }
32
+ end
33
+
34
+ shared_examples_for "a lax listener" do
35
+ Then { lax_listener.respond_to?(:unknown) }
36
+ And { lax_listener.unknown == [] }
37
+ end
38
+
39
+ shared_examples_for "a success/failure hark listener" do
40
+ it_should_behave_like "a success/failure listener"
41
+ it_should_behave_like "a strict listener" do
42
+ Given(:strict_listener) { listener }
43
+ end
44
+
45
+ context "when made lax" do
46
+ Given(:lax_listener) { listener.lax }
47
+ it_should_behave_like "a lax listener"
48
+
49
+ context "and made strict again" do
50
+ Given(:strict_listener) { lax_listener.strict }
51
+ it_should_behave_like "a strict listener"
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "A plain (non hark) listener object" do
57
+ Given(:listener) { PlainListener.new(transcript) }
58
+
59
+ it_should_behave_like "a success/failure listener"
60
+ it_should_behave_like "a strict listener" do
61
+ Given(:strict_listener) { listener }
62
+ end
63
+ end
64
+
65
+ describe "hark with respond_to style block" do
66
+ Given(:listener) do
67
+ hark do |on|
68
+ on.success {|v| transcript.push [:succeeded, v] }
69
+ on.failure {|v| transcript.push [:failed, v] }
70
+ end
71
+ end
72
+
73
+ it_should_behave_like "a success/failure hark listener"
74
+ end
75
+
76
+ describe "hark with callables" do
77
+ Given(:listener) do
78
+ hark success: ->(v){ transcript.push [:succeeded, v] }, failure: ->(v){ transcript.push [:failed, v] }
79
+ end
80
+
81
+ it_should_behave_like "a success/failure hark listener"
82
+ end
83
+
84
+ describe "hark built up in steps" do
85
+ Given(:listener) do
86
+ l = hark
87
+ l = l.hark(:success) {|v| transcript.push [:succeeded, v] }
88
+ l = l.hark(:failure) {|v| transcript.push [:failed, v] }
89
+ end
90
+
91
+ it_should_behave_like "a success/failure hark listener"
92
+ end
93
+
94
+ describe "object.to_hark" do
95
+ Given(:listener) { PlainListener.new(transcript).to_hark }
96
+
97
+ it_should_behave_like "a success/failure hark listener"
98
+ end
99
+
100
+ describe "combine two listeners together" do
101
+ Given(:logger) { hark(:signup_user) {|user| transcript << "User #{user} signed up" } }
102
+ Given(:emailer) { hark(:signup_user) {|user| transcript << "Emailed #{user}" } }
103
+
104
+ Given(:listener) { logger.hark(emailer) }
105
+
106
+ When { listener.signup_user("Fred") }
107
+
108
+ Then { transcript == ["User Fred signed up", "Emailed Fred"] }
109
+ end
110
+
111
+ describe "lax/strict is preserved on #hark" do
112
+ Then { hark.lax.hark.is_a? Hark::LaxListener }
113
+ Then { hark.strict.hark.is_a? Hark::StrictListener }
114
+ end
115
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift File.dirname('../lib')
2
+
3
+ require 'rspec/given'
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ianwhite-hark
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ian White
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-24 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-given
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Create ad-hoc listener objects with impunity
70
+ email:
71
+ - ian.w.white@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - Guardfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - hark.gemspec
83
+ - lib/hark.rb
84
+ - lib/hark/ad_hoc.rb
85
+ - lib/hark/core_ext.rb
86
+ - lib/hark/dispatcher.rb
87
+ - lib/hark/listener.rb
88
+ - lib/hark/version.rb
89
+ - spec/hark_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: http://github.com/ianwhite/hark
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.0.6
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Hark is a gem that enables writing code in a 'hexagonal architecture' style
115
+ test_files:
116
+ - spec/hark_spec.rb
117
+ - spec/spec_helper.rb