kungfuig 0.3.1 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/kungfuig.rb +39 -19
- data/lib/kungfuig/aspector.rb +40 -0
- data/lib/kungfuig/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69f461e7748885c43f77aa5ea2d1635b825f4cd2
|
4
|
+
data.tar.gz: 6bebc1fbf07394c0af30c7c44ea1cefff3d0a75a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2976c6c64585dd06ae78443f2f0b480cb0dae0b0880d2cd5361efceee268de50a108c6c3562b0aef86da001d04f47d58048bedf585d1f7f77039e601c7bb9370
|
7
|
+
data.tar.gz: 67e7a375acdee7ec04b24794c4a6520949d03d7443a9056d28ab27e8d22a869927eea7dc4dd9188198b8626c7d363b73431972017982722a226d61a1b5b1f9ec
|
data/README.md
CHANGED
@@ -32,7 +32,7 @@ MyApp.kungfuig do
|
|
32
32
|
end
|
33
33
|
```
|
34
34
|
|
35
|
-
###
|
35
|
+
### Aspect to be called on method execution
|
36
36
|
|
37
37
|
```ruby
|
38
38
|
class MyApp
|
@@ -44,7 +44,7 @@ class MyApp
|
|
44
44
|
end
|
45
45
|
|
46
46
|
MyApp.kungfuig do
|
47
|
-
|
47
|
+
aspect :report do |result|
|
48
48
|
puts "MyApp#report returned #{result}"
|
49
49
|
end
|
50
50
|
end
|
data/lib/kungfuig.rb
CHANGED
@@ -3,6 +3,7 @@ require 'yaml'
|
|
3
3
|
require 'hashie'
|
4
4
|
|
5
5
|
module Kungfuig
|
6
|
+
ASPECT_PREFIX = '♻_'.freeze
|
6
7
|
MX = Mutex.new
|
7
8
|
|
8
9
|
module InstanceMethods
|
@@ -63,27 +64,34 @@ module Kungfuig
|
|
63
64
|
!option(*keys).nil?
|
64
65
|
end
|
65
66
|
|
67
|
+
# rubocop:disable Metrics/AbcSize
|
68
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
69
|
+
# rubocop:disable Metrics/MethodLength
|
66
70
|
# @param hos [Hash|String] the new values taken from hash,
|
67
71
|
# mash or string (when string, should be either valid YAML file name or
|
68
72
|
# string with valid YAML)
|
69
73
|
def merge_hash_or_string! hos
|
70
74
|
options.deep_merge! case hos
|
71
|
-
when NilClass then
|
72
|
-
when Hash then hos
|
75
|
+
when NilClass then Hashie::Mash.new # aka skip
|
76
|
+
when Hash then Hashie::Mash.new(hos)
|
73
77
|
when String
|
74
78
|
begin
|
75
79
|
File.exist?(hos) ? Hashie::Mash.load(hos) : Hashie::Mash.new(YAML.load(hos)).tap do |opts|
|
76
80
|
fail ArgumentError.new "#{__callee__} expects valid YAML configuration file or YAML string." unless opts.is_a?(Hash)
|
77
81
|
end
|
78
|
-
rescue ArgumentError
|
82
|
+
rescue ArgumentError
|
79
83
|
fail ArgumentError.new "#{__callee__} expects valid YAML configuration file. [#{hos}] contains invalid syntax."
|
80
|
-
rescue Psych::SyntaxError
|
84
|
+
rescue Psych::SyntaxError
|
81
85
|
fail ArgumentError.new "#{__callee__} expects valid YAML configuration string. Got:\n#{hos}"
|
82
86
|
end
|
87
|
+
when ->(h) { h.respond_to?(:to_hash) } then Hashie::Mash.new(h.to_hash)
|
83
88
|
else
|
84
89
|
fail ArgumentError.new "#{__callee__} accepts either String or Hash as parameter."
|
85
90
|
end
|
86
91
|
end
|
92
|
+
# rubocop:enable Metrics/MethodLength
|
93
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
94
|
+
# rubocop:enable Metrics/AbcSize
|
87
95
|
private :merge_hash_or_string!
|
88
96
|
end
|
89
97
|
|
@@ -109,25 +117,37 @@ module Kungfuig
|
|
109
117
|
instance_eval(&block)
|
110
118
|
end
|
111
119
|
|
112
|
-
|
113
|
-
|
114
|
-
fail
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
120
|
+
# rubocop:disable Metrics/MethodLength
|
121
|
+
def aspect meth, after = true
|
122
|
+
fail ArgumentError.new "Aspect must have a codeblock" unless block_given?
|
123
|
+
fail NoMethodError.new "Aspect must be attached to existing method" unless instance_methods.include? meth.to_sym
|
124
|
+
|
125
|
+
aspects(meth)[after ? :after : :before] << Proc.new
|
126
|
+
|
127
|
+
unless instance_methods(true).include?(:"#{ASPECT_PREFIX}#{meth}")
|
128
|
+
class_eval <<-CODE
|
129
|
+
alias_method :#{ASPECT_PREFIX}#{meth}, :#{meth}
|
130
|
+
def #{meth}(*args, &cb)
|
131
|
+
ps = self.class.aspects(:#{meth})
|
132
|
+
ps[:before].each do |p|
|
133
|
+
p.call(*args) # TODO: make prependers able to change args!!!
|
134
|
+
end
|
135
|
+
send(:#{ASPECT_PREFIX}#{meth}, *args, &cb).tap do |result|
|
136
|
+
ps[:after].each do |p|
|
137
|
+
p.call result, *args
|
125
138
|
end
|
126
139
|
end
|
127
140
|
end
|
128
|
-
|
129
|
-
|
141
|
+
CODE
|
130
142
|
end
|
143
|
+
|
144
|
+
meth.to_sym
|
145
|
+
end
|
146
|
+
# rubocop:enable Metrics/MethodLength
|
147
|
+
|
148
|
+
def aspects meth = nil
|
149
|
+
@aspects ||= {}
|
150
|
+
meth ? @aspects[meth.to_sym] ||= {after: [], before: []} : @aspects
|
131
151
|
end
|
132
152
|
alias_method :set, :option!
|
133
153
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require_relative '../kungfuig'
|
2
|
+
|
3
|
+
module Kungfuig
|
4
|
+
# Generic helper for massive attaching aspects
|
5
|
+
module Aspector
|
6
|
+
# Helper methods
|
7
|
+
class H
|
8
|
+
def value_to_method_list klazz, values_inc, values_exc
|
9
|
+
[values_inc, values_exc].map do |v|
|
10
|
+
v = [*v].map(&:to_sym)
|
11
|
+
case
|
12
|
+
when v.empty? then []
|
13
|
+
when v.include?(:'*') then klazz.instance_methods(false)
|
14
|
+
else klazz.instance_methods(false) & v
|
15
|
+
end
|
16
|
+
end.reduce(&:-)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def attach(klazz, before: nil, after: nil, exclude: nil)
|
21
|
+
raise ArgumentError, "Trying to attach nothing to #{klazz}. I need a block!" unless block_given?
|
22
|
+
|
23
|
+
klazz.send(:include, Kungfuig::Aspector) unless klazz.ancestors.include? Kungfuig::Aspector
|
24
|
+
cb = Proc.new
|
25
|
+
|
26
|
+
H.new.value_to_method_list(klazz, before, exclude).each do |m|
|
27
|
+
klazz.aspect(m, false, &cb)
|
28
|
+
end unless before.nil?
|
29
|
+
|
30
|
+
H.new.value_to_method_list(klazz, after, exclude).each do |m|
|
31
|
+
klazz.aspect(m, true, &cb)
|
32
|
+
end unless after.nil?
|
33
|
+
|
34
|
+
klazz.aspects
|
35
|
+
end
|
36
|
+
module_function :attach
|
37
|
+
|
38
|
+
private_constant :H
|
39
|
+
end
|
40
|
+
end
|
data/lib/kungfuig/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kungfuig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kantox LTD
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|
@@ -125,6 +125,7 @@ files:
|
|
125
125
|
- Rakefile
|
126
126
|
- kungfuig.gemspec
|
127
127
|
- lib/kungfuig.rb
|
128
|
+
- lib/kungfuig/aspector.rb
|
128
129
|
- lib/kungfuig/version.rb
|
129
130
|
homepage: http://kantox.com
|
130
131
|
licenses:
|