after_do-loader 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: fc03471141f4bf6a67f011746a49f8bc5c938f81
4
+ data.tar.gz: 7934725beb90a64261ae6ec595c8384a230014d1
5
+ SHA512:
6
+ metadata.gz: 70edc0bd393849c1cbe7e2a03f235c5f24aad4808334ed14d87fd4f890c16f4f3543b7e1efabce1763dbe1284c25b2526862d7d6530875637f5ad03a1f656a25
7
+ data.tar.gz: 442c50496680bdf0c58f5f339e11f8535ce5cd16c9c88a188957e61031d946ca2189d2df5f9d0ed02680f52d4906b8d012da12393a3355e4328d32be56abb5dc
data/.git_hooks.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ pre_commits:
3
+ - GitHooks::PreCommit::TrailingWhitespace
4
+ - GitHooks::PreCommit::PreventDebugger
5
+ - GitHooks::PreCommit::PreventMaster
6
+ - GitHooks::PreCommit::Rubocop
7
+ - GitHooks::PreCommit::Rspec
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ Documentation:
2
+ Enabled: false
3
+
4
+ AllCops:
5
+ # Include gemspec and Rakefile
6
+ Include:
7
+ - '**/*.gemspec'
8
+ - '**/Rakefile'
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in after_do-loader.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 renan-ranelli
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,94 @@
1
+ # AfterDo::Loader
2
+
3
+ `after_do-loader` is a gem that allows you to apply `after_do`'s callbacks
4
+ dynamically using a `config` file. With this, you do need to add anything to
5
+ your source code, much like aspect oriented programming does.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'after_do-loader'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install after_do-loader
22
+
23
+ ## Usage
24
+
25
+ In order to use this gem, you need to add a `aspects.yml` file to your
26
+ application. This file must define the classes that will implement the callbacks
27
+ used by `after_do`'s methods.
28
+
29
+ Example:
30
+
31
+ Suppose I want to log every method call to `AdvisedClass#initialize`, before and
32
+ after it's invocation. What will be logged is defined in the
33
+ `LoggingAspect#log_start` and `LoggingAspect#log_finish` methods.
34
+
35
+ The `aspects.yml` file must look like this:
36
+
37
+ ```yaml
38
+ aspects:
39
+ - name: Logging
40
+ klass: LoggingAspect
41
+ description: This aspects provides basic method logging
42
+ advices:
43
+ - names: [log_start, log_finish]
44
+ targets:
45
+ - klass: AdvisedClass
46
+ target_methods:
47
+ - initialize
48
+ - merge
49
+ ```
50
+
51
+ And the `LoggingAspect` class will be something like this:
52
+
53
+ ```ruby
54
+ class LoggingAspect
55
+ def initialize(target_class)
56
+ @target_class = target_class
57
+ end
58
+
59
+ def log_start(target_method)
60
+ target_class.before target_method do |*args, advised_class_instance|
61
+ init_time = Time.now
62
+ arg_text = args.map(&:inspect).join(', ')
63
+ method = "#{target_class}##{target_method}"
64
+
65
+ msg = "Started: Method=#{method} Time=#{init_time} Args='#{arg_text}'"
66
+ Rails.logger.info(msg)
67
+ end
68
+ end
69
+
70
+ def log_finish(target_method)
71
+ target_class.after target_method do |*args, advised_class_instance|
72
+ init_time = Time.now
73
+ arg_text = args.map(&:inspect).join(', ')
74
+ method = "#{target_class}##{target_method}"
75
+
76
+ msg = "Finished: Method=#{method} Time=#{init_time} Args='#{arg_text}'"
77
+
78
+ Rails.logger.info(msg)
79
+ end
80
+ end
81
+
82
+ private
83
+
84
+ attr_reader :target_class
85
+ end
86
+ ```
87
+
88
+ ## Contributing
89
+
90
+ 1. Fork it ( https://github.com/rranelli/after_do-loader/fork )
91
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
92
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
93
+ 4. Push to the branch (`git push origin my-new-feature`)
94
+ 5. Create a new Pull Request
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'after_do/loader/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'after_do-loader'
7
+ spec.version = AfterDo::Loader::VERSION
8
+ spec.authors = ['Renan Ranelli', 'Rafael Almeida']
9
+ spec.email = ['renanranelli@gmail.com', 'rubygems@rafaelalmeida.net']
10
+ spec.summary = 'Apply after_do methods dynamically based on a config file.'
11
+ spec.description = 'Apply after_do methods dynamically based on a config file.'
12
+ spec.homepage = ''
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(/^(test|spec|features)\//)
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_dependency 'after_do', '~> 0.3'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.7'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+ spec.add_development_dependency 'recursive-open-struct'
25
+ spec.add_development_dependency 'git-hooks', '~> 0.2'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+
28
+ spec.add_development_dependency 'pry'
29
+ spec.add_development_dependency 'pry-doc'
30
+ spec.add_development_dependency 'method_source'
31
+ end
@@ -0,0 +1,42 @@
1
+ module AfterDo
2
+ module Loader
3
+ class AspectLoader
4
+ def initialize(config)
5
+ @config = config
6
+ end
7
+
8
+ def load
9
+ config.aspects.each do |aspect|
10
+ apply_advices(aspect.klass, aspect.advices)
11
+ end
12
+ rescue => e
13
+ raise e, "Could not apply advices. Check you .yml file\
14
+ for ill formatting. \n" + e.message
15
+ end
16
+
17
+ protected
18
+
19
+ attr_reader :config
20
+
21
+ def apply_advices(aspect, advices)
22
+ advices.each do |advice|
23
+ advice.targets.each do |target|
24
+ target.klass.extend(AfterDo)
25
+
26
+ methods = target.target_methods
27
+
28
+ apply_aspect_to_target(aspect, advice, target.klass, methods)
29
+ end
30
+ end
31
+ end
32
+
33
+ def apply_aspect_to_target(aspect, advice, target, methods)
34
+ methods.each do |method|
35
+ advice.names.each do |advice_name|
36
+ aspect.new(target).send(advice_name, method)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,37 @@
1
+ require 'delegate'
2
+
3
+ module AfterDo
4
+ module Loader
5
+ class Config < SimpleDelegator
6
+ def initialize(config_path)
7
+ @config_path = config_path
8
+ super(parse_config)
9
+ end
10
+
11
+ protected
12
+
13
+ attr_reader :config_path
14
+
15
+ def parse_config
16
+ config_hash = YAML.load_file(config_path)
17
+ config = constantize_classes!(config_hash)
18
+
19
+ RecursiveOpenStruct.new(config, recurse_over_arrays: true)
20
+ end
21
+
22
+ def constantize_classes!(config_hash)
23
+ config_hash['aspects'].each do |aspect|
24
+ aspect['klass'] = AfterDo.const_get(aspect['klass'])
25
+
26
+ aspect['advices'].each do |advice|
27
+ advice['targets'].each do |target|
28
+ target['klass'] = AfterDo.const_get(target['klass'])
29
+ end
30
+ end
31
+ end
32
+
33
+ config_hash
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module AfterDo
2
+ module Loader
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ require 'yaml'
2
+ require 'recursive_open_struct'
3
+
4
+ require_relative 'loader/version'
5
+
6
+ require_relative 'loader/aspect_loader'
7
+ require_relative 'loader/config'
8
+
9
+ module AfterDo
10
+ module Loader
11
+ end
12
+ end
@@ -0,0 +1,44 @@
1
+ module AfterDo::Loader
2
+ class AdvisedClass; end
3
+ class CoolAspect; end
4
+
5
+ describe AspectLoader do
6
+ subject(:loader) { described_class.new(config) }
7
+
8
+ let(:config) do
9
+ Config.new(fixture_path('aspect_config.yml'))
10
+ end
11
+
12
+ let(:aspect) { CoolAspect.new }
13
+ let(:aspect_class) { CoolAspect }
14
+ let(:target_class) { AdvisedClass }
15
+
16
+ describe '#load' do
17
+ subject(:load) { loader.load }
18
+
19
+ before do
20
+ allow(aspect_class).to receive(:new).and_return(aspect)
21
+ allow(aspect).to receive(:add_callback)
22
+ end
23
+
24
+ it 'extends the target class with the AfterDo library' do
25
+ expect(target_class).to receive(:extend).with(AfterDo)
26
+
27
+ load
28
+ end
29
+
30
+ it 'creates an Aspect object of the correct class' do
31
+ expect(aspect_class).to receive(:new).with(target_class)
32
+
33
+ load
34
+ end
35
+
36
+ it 'applies the right advice to the target class' do
37
+ expect(aspect).to receive(:add_callback).with('initialize').once
38
+ expect(aspect).to receive(:add_callback).with('merge').once
39
+
40
+ load
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,36 @@
1
+ module AfterDo::Loader
2
+ class AdvisedClass; end
3
+ class CoolAspect; end
4
+
5
+ describe Config do
6
+ subject(:config) { Config.new(fixture_path('aspect_config.yml')) }
7
+
8
+ let(:config_hash) do
9
+ {
10
+ 'aspects' => [
11
+ {
12
+ 'name' => 'CoolAspect',
13
+ 'klass' => AfterDo::Loader::CoolAspect,
14
+ 'description' => 'This aspect adds after_do callbacks to its target classes',
15
+ 'advices' => [
16
+ {
17
+ 'names' => %w(add_callback),
18
+ 'targets' => [
19
+ {
20
+ 'klass' => AfterDo::Loader::AdvisedClass,
21
+ 'target_methods' => %w(initialize merge)
22
+ }
23
+ ]
24
+ }
25
+ ]
26
+ }
27
+ ]
28
+ }
29
+ end
30
+ let(:config_os) do
31
+ RecursiveOpenStruct.new(config_hash, recurse_over_arrays: true)
32
+ end
33
+
34
+ it { expect(config.aspects).to eq(config_os.aspects) }
35
+ end
36
+ end
@@ -0,0 +1,11 @@
1
+ aspects:
2
+ - name: CoolAspect
3
+ klass: AfterDo::Loader::CoolAspect
4
+ description: This aspect adds after_do callbacks to its target classes
5
+ advices:
6
+ - names: [add_callback]
7
+ targets:
8
+ - klass: AfterDo::Loader::AdvisedClass
9
+ target_methods:
10
+ - initialize
11
+ - merge
@@ -0,0 +1,9 @@
1
+ require 'git-hooks'
2
+ GitHooks.validate_hooks!
3
+
4
+ require_relative '../lib/after_do/loader'
5
+
6
+ def fixture_path(filename)
7
+ return '' if filename == ''
8
+ File.join(File.absolute_path(File.dirname(__FILE__)), 'fixtures', filename)
9
+ end
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: after_do-loader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Renan Ranelli
8
+ - Rafael Almeida
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-09-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: after_do
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '0.3'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '0.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.7'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.7'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: recursive-open-struct
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: git-hooks
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '0.2'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '0.2'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rake
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '10.0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '10.0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: pry
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: pry-doc
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: method_source
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ description: Apply after_do methods dynamically based on a config file.
141
+ email:
142
+ - renanranelli@gmail.com
143
+ - rubygems@rafaelalmeida.net
144
+ executables: []
145
+ extensions: []
146
+ extra_rdoc_files: []
147
+ files:
148
+ - ".git_hooks.yml"
149
+ - ".gitignore"
150
+ - ".rspec"
151
+ - ".rubocop.yml"
152
+ - Gemfile
153
+ - LICENSE.txt
154
+ - README.md
155
+ - after_do-loader.gemspec
156
+ - lib/after_do/loader.rb
157
+ - lib/after_do/loader/aspect_loader.rb
158
+ - lib/after_do/loader/config.rb
159
+ - lib/after_do/loader/version.rb
160
+ - spec/after_do/loader/aspect_loader_spec.rb
161
+ - spec/after_do/loader/config_spec.rb
162
+ - spec/fixtures/aspect_config.yml
163
+ - spec/spec_helper.rb
164
+ homepage: ''
165
+ licenses:
166
+ - MIT
167
+ metadata: {}
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ requirements: []
183
+ rubyforge_project:
184
+ rubygems_version: 2.2.2
185
+ signing_key:
186
+ specification_version: 4
187
+ summary: Apply after_do methods dynamically based on a config file.
188
+ test_files:
189
+ - spec/after_do/loader/aspect_loader_spec.rb
190
+ - spec/after_do/loader/config_spec.rb
191
+ - spec/fixtures/aspect_config.yml
192
+ - spec/spec_helper.rb