event_hooks 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.
- data/.gitignore +17 -0
- data/.rvmrc +48 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/event_hooks.gemspec +21 -0
- data/lib/event_hooks/errors.rb +3 -0
- data/lib/event_hooks/event_hooks.rb +22 -0
- data/lib/event_hooks/version.rb +3 -0
- data/lib/event_hooks.rb +3 -0
- data/spec/event_hooks/event_hooks_spec.rb +68 -0
- data/spec/spec_helper.rb +5 -0
- metadata +76 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 1.9.2" > .rvmrc
|
9
|
+
environment_id="ruby-1.9.3-p194@clip"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.14.3 (stable)" # 1.10.1 seams as a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
[[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
|
27
|
+
\. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
|
28
|
+
else
|
29
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
30
|
+
rvm --create "$environment_id" || {
|
31
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
32
|
+
return 1
|
33
|
+
}
|
34
|
+
fi
|
35
|
+
|
36
|
+
# If you use bundler, this might be useful to you:
|
37
|
+
if [[ -s Gemfile ]] && {
|
38
|
+
! builtin command -v bundle >/dev/null ||
|
39
|
+
builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
|
40
|
+
}
|
41
|
+
then
|
42
|
+
printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
43
|
+
gem install bundler
|
44
|
+
fi
|
45
|
+
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
|
46
|
+
# then
|
47
|
+
# bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
|
48
|
+
# fi
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 pcrwebdesign
|
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,29 @@
|
|
1
|
+
# EventHooks
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'event_hooks'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install event_hooks
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/event_hooks.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'event_hooks/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "event_hooks"
|
8
|
+
gem.version = EventHooks::VERSION
|
9
|
+
gem.authors = ["Pablo Calderon"]
|
10
|
+
gem.email = ["pablo@pcr-webdesign.com"]
|
11
|
+
gem.description = %q{Add hooks before or after events}
|
12
|
+
gem.summary = %q{HookEvents allows you to add pre and post-conditions to events}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.add_development_dependency "rspec", "~> 2.12.0"
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($/)
|
18
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module EventHooks
|
2
|
+
def self.included(base) #:nodoc:
|
3
|
+
#base.extend EventHooks::ClassMethods
|
4
|
+
super
|
5
|
+
end
|
6
|
+
|
7
|
+
# prepend the hook to the event
|
8
|
+
# event won't run unless hook returns a true-ish value
|
9
|
+
def hook_before(event, hook)
|
10
|
+
raise EventHooks::DoubleHook.new if instance_methods.include?("#{event}_without_before_hook".to_sym)
|
11
|
+
|
12
|
+
alias_method "#{event}_without_before_hook".to_sym, event
|
13
|
+
|
14
|
+
define_method "#{event}_with_before_hook".to_sym do |*args|
|
15
|
+
send(hook) && send("#{event}_without_before_hook".to_sym, *args)
|
16
|
+
end
|
17
|
+
|
18
|
+
alias_method event, "#{event}_with_before_hook".to_sym
|
19
|
+
end
|
20
|
+
|
21
|
+
Class.send(:include, EventHooks)
|
22
|
+
end
|
data/lib/event_hooks.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class StateMachine
|
4
|
+
attr_accessor :ready, :submitted
|
5
|
+
|
6
|
+
def initialize(ready=true)
|
7
|
+
@ready = ready
|
8
|
+
@submitted = false
|
9
|
+
end
|
10
|
+
|
11
|
+
def submit
|
12
|
+
puts "SUBMIT NOW"
|
13
|
+
@submitted = true
|
14
|
+
end
|
15
|
+
|
16
|
+
def submission_preconditions
|
17
|
+
@ready
|
18
|
+
end
|
19
|
+
|
20
|
+
hook_before :submit, :submission_preconditions
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "any class" do
|
24
|
+
class Sub; end
|
25
|
+
|
26
|
+
it "responds to hook_before" do
|
27
|
+
Sub.should respond_to(:hook_before)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'hook_before' do
|
32
|
+
let(:sm) { StateMachine.new }
|
33
|
+
|
34
|
+
it "calls the hook before sending the event" do
|
35
|
+
sm.should_receive(:submission_preconditions)
|
36
|
+
sm.submit
|
37
|
+
end
|
38
|
+
|
39
|
+
context "when precondition is met (hook returns true)" do
|
40
|
+
it "allows the event to occur" do
|
41
|
+
sm.should_receive(:submit_without_before_hook).and_call_original
|
42
|
+
sm.submit
|
43
|
+
sm.submitted.should be_true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "when precondition is not met (hook returns false)" do
|
48
|
+
let(:sm) { StateMachine.new(false) }
|
49
|
+
|
50
|
+
it "stops the event from happening" do
|
51
|
+
sm.should_receive(:submit_without_before_hook).never
|
52
|
+
sm.submit
|
53
|
+
sm.submitted.should be_false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "when you try to add another before hook" do
|
58
|
+
class StateMachine
|
59
|
+
def another_hook
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it "raises a DoubleHook exception" do
|
64
|
+
expect { StateMachine.send :hook_before, :submit, :another_hook }.
|
65
|
+
to raise_exception(EventHooks::DoubleHook)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: event_hooks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Pablo Calderon
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.12.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.12.0
|
30
|
+
description: Add hooks before or after events
|
31
|
+
email:
|
32
|
+
- pablo@pcr-webdesign.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- .rvmrc
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- event_hooks.gemspec
|
44
|
+
- lib/event_hooks.rb
|
45
|
+
- lib/event_hooks/errors.rb
|
46
|
+
- lib/event_hooks/event_hooks.rb
|
47
|
+
- lib/event_hooks/version.rb
|
48
|
+
- spec/event_hooks/event_hooks_spec.rb
|
49
|
+
- spec/spec_helper.rb
|
50
|
+
homepage: ''
|
51
|
+
licenses: []
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.8.24
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: HookEvents allows you to add pre and post-conditions to events
|
74
|
+
test_files:
|
75
|
+
- spec/event_hooks/event_hooks_spec.rb
|
76
|
+
- spec/spec_helper.rb
|