garfio 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0998e556317f91f784ca15cb6cb924b1a5c2c701
4
+ data.tar.gz: e3bfc9f37e78bb6a03da96dd999c9d99c56d9cfa
5
+ SHA512:
6
+ metadata.gz: 12506e7eddecf8fae636bdbf59fef3c0d35c41ce7befe86d036388cbc80f4f35fd706ad4497d8e02a2135b81178ef1e1423809dae5b49e30dbab8bd43d2ac787
7
+ data.tar.gz: d543c6c2ee16022340e6f30a9411550e9f13c567ac8e03c97880da0c77a3d17548f5aec9b2d72d91eb317be2539da0cfb9381e3bd234b83fb1ade3d028251c46
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Julio Lopez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,84 @@
1
+ Garfio
2
+ ====
3
+
4
+ Garfio helps you to build hooks in your ruby objects, is minimalist and completely framework-agnostic
5
+
6
+ ## Introduction
7
+
8
+ With few lines of code, one method compilation and no method missing Garfio with an easy way lets you to launch callbacks before and after of the methods of your objects.
9
+
10
+ ## Installation
11
+
12
+ Installing Garfio is as simple as running:
13
+
14
+ ```
15
+ $ gem install garfio
16
+ ```
17
+
18
+ Include Garfio in your Gemfile with gem 'garfio' or require it with require 'garfio'.
19
+
20
+ Usage
21
+ -----
22
+
23
+ Once you have extend Garfio, you can define your before and after callbacks in the `set_hook` method, your callback recognize the method sending his name as symbol
24
+
25
+ ```ruby
26
+ class User
27
+ extend Garfio
28
+
29
+ def send_greeting
30
+ puts "preparing the welcome message"
31
+ end
32
+
33
+ def register_user
34
+ puts "registering user"
35
+ end
36
+
37
+ def send_mailer
38
+ puts "sending to your email"
39
+ end
40
+
41
+ set_hook :register_user do
42
+ before :send_greeting
43
+ after :send_mailer
44
+ end
45
+ end
46
+
47
+ User.new.register_user
48
+ #=> preparing the welcome message
49
+ #=> registering user
50
+ #=> sending to your email
51
+ ```
52
+
53
+ Is not neccesary indicate both callbacks, you can work with just before or after
54
+
55
+ ```ruby
56
+ set_hook :register_user do
57
+ before :send_greeting
58
+ end
59
+ ```
60
+
61
+ Also you can send blocks to your callbacks
62
+ ```ruby
63
+ set_hook :register_user do
64
+ before { puts "hello user!" }
65
+ after { puts "Ok, bye" }
66
+ end
67
+
68
+ User.new.register_user
69
+ #=> hello user!
70
+ #=> registering user
71
+ #=> Ok, bye
72
+ ```
73
+
74
+ And you can take advantage of them to do more complex stuff
75
+
76
+ ```ruby
77
+ set_hook :register_user do
78
+ after do
79
+ update_status_with("activate")
80
+ send_mailer
81
+ notify_friends
82
+ end
83
+ end
84
+ ```
@@ -0,0 +1,20 @@
1
+ require "./lib/garfio"
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "garfio"
5
+ s.version = Garfio::VERSION
6
+ s.summary = "Garfio helps you to build hooks in your ruby objects"
7
+ s.description = "With few lines of code, one method compilation and no method missing Garfio with an easy way lets you to launch callbacks before and after of the methods of your objects."
8
+ s.authors = ["Julio Lopez"]
9
+ s.email = ["ljuliom@gmail.com"]
10
+ s.homepage = "http://github.com/TheBlasfem/garfio"
11
+ s.files = Dir[
12
+ "LICENSE",
13
+ "README.md",
14
+ "lib/**/*.rb",
15
+ "*.gemspec",
16
+ "test/**/*.rb"
17
+ ]
18
+ s.license = "MIT"
19
+ s.add_development_dependency "cutest", "1.1.3"
20
+ end
@@ -0,0 +1,47 @@
1
+ # Copyright (c) 2015 Julio Lopez
2
+
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+ module Garfio
21
+ VERSION = "1.0.0"
22
+
23
+ class GarfioHooks
24
+ attr_reader :hook_before, :hook_after
25
+ def initialize(&block)
26
+ instance_eval &block
27
+ end
28
+
29
+ private
30
+ [:before, :after].each do |name|
31
+ define_method name do |method_name = nil, &block|
32
+ instance_variable_set("@hook_#{name}", method_name || block)
33
+ end
34
+ end
35
+ end
36
+
37
+ def set_hook(original_method, _v = "", &block)
38
+ gar = GarfioHooks.new &block
39
+ send :alias_method, "old_#{original_method}", original_method
40
+ send :define_method, original_method do |*args|
41
+ _v.is_a?(Proc) ? instance_eval(&_v) : send(_v) if _v = gar.hook_before
42
+ return_value = send "old_#{original_method}", *args
43
+ _v.is_a?(Proc) ? instance_eval(&_v) : send(_v) if _v = gar.hook_after
44
+ return_value
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,152 @@
1
+ require File.expand_path("../lib/garfio", File.dirname(__FILE__))
2
+
3
+ class User
4
+ extend Garfio
5
+
6
+ def initialize
7
+ @@sum = 0
8
+ end
9
+
10
+ def send_greeting
11
+ @@sum += 1
12
+ end
13
+
14
+ def register_user(n = 2)
15
+ @@sum += n
16
+ end
17
+
18
+ def send_mail
19
+ @@sum += 3
20
+ end
21
+
22
+ def some_method(n = 4)
23
+ @@sum += n
24
+ end
25
+
26
+ def some_other_method(n = 5)
27
+ @@sum += n
28
+ end
29
+
30
+ def get_sum
31
+ @@sum
32
+ end
33
+ end
34
+
35
+ scope do
36
+ test "hook before" do
37
+ u = Class.new(User) do
38
+ set_hook :register_user do
39
+ before :send_greeting
40
+ end
41
+ end
42
+ u_instance = u.new
43
+ u_instance.register_user
44
+
45
+ assert_equal 3, u_instance.get_sum
46
+ end
47
+
48
+ test "hook after" do
49
+ u = Class.new(User) do
50
+ set_hook :register_user do
51
+ after :send_mail
52
+ end
53
+ end
54
+ u_instance = u.new
55
+ u_instance.register_user
56
+
57
+ assert_equal 5, u_instance.get_sum
58
+ end
59
+
60
+ test "hook before and after" do
61
+ u = Class.new(User) do
62
+ set_hook :register_user do
63
+ before :send_greeting
64
+ after :send_mail
65
+ end
66
+ end
67
+ u_instance = u.new
68
+ u_instance.register_user
69
+
70
+ assert_equal 6, u_instance.get_sum
71
+ end
72
+
73
+ test "respecting the sending variable" do
74
+ u = Class.new(User) do
75
+ set_hook :register_user do
76
+ before :send_greeting
77
+ after :send_mail
78
+ end
79
+ end
80
+ u_instance = u.new
81
+ u_instance.register_user(10)
82
+
83
+ assert_equal 14, u_instance.get_sum
84
+ end
85
+
86
+ test "sending a block in before" do
87
+ u = Class.new(User) do
88
+ set_hook :register_user do
89
+ before { some_method }
90
+ end
91
+ end
92
+ u_instance = u.new
93
+ u_instance.register_user
94
+
95
+ assert_equal 6, u_instance.get_sum
96
+ end
97
+
98
+ test "sending a complex block in before" do
99
+ u = Class.new(User) do
100
+ set_hook :register_user do
101
+ before do
102
+ some_method 5
103
+ some_other_method 8
104
+ end
105
+ end
106
+ end
107
+ u_instance = u.new
108
+ u_instance.register_user
109
+
110
+ assert_equal 15, u_instance.get_sum
111
+ end
112
+
113
+ test "sending a block in after" do
114
+ u = Class.new(User) do
115
+ set_hook :register_user do
116
+ after { some_method }
117
+ end
118
+ end
119
+ u_instance = u.new
120
+ u_instance.register_user
121
+
122
+ assert_equal 6, u_instance.get_sum
123
+ end
124
+
125
+ test "sending a complex block in after" do
126
+ u = Class.new(User) do
127
+ set_hook :register_user do
128
+ after do
129
+ some_method 5
130
+ some_other_method 8
131
+ end
132
+ end
133
+ end
134
+ u_instance = u.new
135
+ u_instance.register_user
136
+
137
+ assert_equal 15, u_instance.get_sum
138
+ end
139
+
140
+ test "hook method return a value" do
141
+ u = Class.new(User) do
142
+ set_hook :register_user do
143
+ after do
144
+ some_method 5
145
+ some_other_method 8
146
+ end
147
+ end
148
+ end
149
+ u_instance = u.new
150
+ assert_equal 2, u_instance.register_user
151
+ end
152
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: garfio
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Julio Lopez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cutest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.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.1.3
27
+ description: With few lines of code, one method compilation and no method missing
28
+ Garfio with an easy way lets you to launch callbacks before and after of the methods
29
+ of your objects.
30
+ email:
31
+ - ljuliom@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - LICENSE
37
+ - README.md
38
+ - garfio.gemspec
39
+ - lib/garfio.rb
40
+ - test/test_garfio.rb
41
+ homepage: http://github.com/TheBlasfem/garfio
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.2.2
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Garfio helps you to build hooks in your ruby objects
65
+ test_files: []