call_me_ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +76 -0
  3. data/lib/call_me_ruby.rb +47 -0
  4. metadata +131 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2450b371428529143af4c2cdf9d0fa8058b101d8
4
+ data.tar.gz: 0760e245a0193e08e837718988390231bada6c99
5
+ SHA512:
6
+ metadata.gz: 95bf4940ac97c77cb529fa6faaa46f948b6a2694e651873c732a653f9c96c47a7d0f052f7ed01eea015e44e5791a6bee08e28730b68a8549ce433805147eb2a5
7
+ data.tar.gz: c44856fd6ae64c43697b499f098d8d427b4d0cccc97f13fc353ca57e6e07b0dc0115dfbccb29078db96c385b52f83d4554ca9ac71a1e7b18d2a6ab421b9e0e39
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # Call Me Ruby! [![Build Status](https://travis-ci.org/nathankleyn/call_me_ruby.svg?branch=master)](https://travis-ci.org/nathankleyn/call_me_ruby) [![Coverage Status](https://coveralls.io/repos/nathankleyn/call_me_ruby/badge.png?branch=master)](https://coveralls.io/r/nathankleyn/call_me_ruby?branch=master)
2
+
3
+ Simple, declarative, ActiveModel style callbacks (aka hooks or events) in Ruby!
4
+
5
+ ## Installing
6
+
7
+ You can install this gem via RubyGems:
8
+
9
+ ```sh
10
+ gem install call_me_ruby
11
+ ```
12
+
13
+ ## Using
14
+
15
+ Include the mixin in your class and you're away!
16
+
17
+ Here's how to subscribe to events:
18
+
19
+ ```ruby
20
+ require 'call_me_ruby'
21
+
22
+ class MyAmazingClass
23
+ include CallMeRuby
24
+
25
+ # When you do subscribe at the class level like this, it's shared between all instances of this class...
26
+ subscribe :some_event, :will_succeed
27
+
28
+ def initialize
29
+ # But if you do subscribe at the instance level like this, it'll only add the callback to this one instance.
30
+ subscribe :some_event, :will_fail
31
+ end
32
+
33
+ def will_succeed
34
+ puts 'foo'
35
+ # If a callback returns a "truthy" value, than it's considered to have succeeded. However...
36
+ true
37
+ end
38
+
39
+ def bar
40
+ # If a callback returns a "falsy" value, then no more callbacks will be called and publish will return false. If
41
+ # all callbacks succeed, then publish will return true. Use this to make things fail fast as you need.
42
+ false
43
+ end
44
+ end
45
+ ```
46
+
47
+ And now you can trigger them:
48
+
49
+ ```ruby
50
+ my_amazing_class = MyAmazingClass.new
51
+ my_amazing_class.publish(:some_event)
52
+ ```
53
+
54
+ ## License
55
+
56
+ The MIT License (MIT)
57
+
58
+ Copyright (c) 2015 Nathan Kleyn
59
+
60
+ Permission is hereby granted, free of charge, to any person obtaining a copy
61
+ of this software and associated documentation files (the "Software"), to deal
62
+ in the Software without restriction, including without limitation the rights
63
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
64
+ copies of the Software, and to permit persons to whom the Software is
65
+ furnished to do so, subject to the following conditions:
66
+
67
+ The above copyright notice and this permission notice shall be included in
68
+ all copies or substantial portions of the Software.
69
+
70
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
71
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
72
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
73
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
74
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
75
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
76
+ THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ # A mixin to implement publish/subscribe style callbacks in the class that
2
+ # includes this.
3
+ module CallMeRuby
4
+ # The self.included idiom. This is described in great detail in a
5
+ # fantastic blog post here:
6
+ #
7
+ # http://www.railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/
8
+ #
9
+ # Basically, this idiom allows us to add both instance *and* class methods
10
+ # to the class that is mixing this module into itself without forcing them
11
+ # to call extend and include for this mixin. You'll see this idiom everywhere
12
+ # in the Ruby/Rails world, so we use it too.
13
+ def self.included(cls)
14
+ cls.extend(ClassMethods)
15
+ end
16
+
17
+ # Common methods inherited by all classes
18
+ module ClassMethods
19
+ def class_callbacks
20
+ @class_callbacks ||= Hash.new { |h, k| h[k] = [] }
21
+ end
22
+
23
+ def subscribe(*names, sym)
24
+ names.each do |name|
25
+ class_callbacks[name] << sym
26
+ end
27
+ end
28
+ end
29
+
30
+ def callbacks
31
+ @callbacks ||= Hash.new { |h, k| h[k] = [] }
32
+ end
33
+
34
+ def subscribe(*names, sym)
35
+ names.each do |name|
36
+ callbacks[name] << sym
37
+ end
38
+ end
39
+
40
+ def publish(name, *args)
41
+ class_callback_methods = self.class.class_callbacks[name]
42
+ callback_methods = callbacks[name]
43
+
44
+ (class_callback_methods + callback_methods).each { |method| return false unless send(method, *args) }
45
+ true
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: call_me_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Kleyn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: coveralls
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: filewatcher
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.28'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.28'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.2'
97
+ description: See https://github.com/nathankleyn/call_me_ruby for more information!
98
+ email:
99
+ - nathan@nathankleyn.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - README.md
105
+ - lib/call_me_ruby.rb
106
+ homepage: https://github.com/nathankleyn/call_me_ruby
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.4.5
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Simple, declarative, ActiveModel style callbacks (aka hooks or events) in
130
+ Ruby!
131
+ test_files: []