kungfuig 0.3.1 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ac62b08dead08d021c21acbb9c5fe2bc673d928
4
- data.tar.gz: 47b0818b81f566d87304a257cb6f5032bcb07eb4
3
+ metadata.gz: 69f461e7748885c43f77aa5ea2d1635b825f4cd2
4
+ data.tar.gz: 6bebc1fbf07394c0af30c7c44ea1cefff3d0a75a
5
5
  SHA512:
6
- metadata.gz: a5816742eac7ae87672cadd8cc0fa89bca9dc88fe51feadd6ac02775c0e21b2a4fb53370a70207601d1b4c9b691ee40f3c9d79fa0bf0642f95e2d67f34428dcf
7
- data.tar.gz: f6a24156db9d8f5e666d79cbeb397205f96030803149063a8ccd77b6436c1ff1053869ceddf261872c3b0c1c13ffe6383beae1f82831dfd797e363cd77de11bf
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
- ### Plugin to be called on method execution
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
- plugin :report do |result|
47
+ aspect :report do |result|
48
48
  puts "MyApp#report returned #{result}"
49
49
  end
50
50
  end
@@ -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 {} # aka skip
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 => ae
82
+ rescue ArgumentError
79
83
  fail ArgumentError.new "#{__callee__} expects valid YAML configuration file. [#{hos}] contains invalid syntax."
80
- rescue Psych::SyntaxError => pse
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
- def plugin meth
113
- fail ArgumentError.new "Plugin must have a codeblock" unless block_given?
114
- fail NoMethodError.new "Plugin must be attached to existing method" unless instance_methods.include? meth.to_sym
115
-
116
- ((@plugins ||= {})[meth.to_sym] ||= []) << Proc.new
117
- plugins = @plugins
118
- class_eval do
119
- unless instance_methods(true).include?(:"∃#{meth}")
120
- alias_method :"∃#{meth}", meth.to_sym
121
- define_method meth.to_sym do |*args|
122
- send(:"∃#{meth}", *args).tap do |result|
123
- plugins[meth.to_sym].each do |p|
124
- p.call result
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
- end
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
@@ -1,3 +1,3 @@
1
1
  module Kungfuig
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.1"
3
3
  end
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.3.1
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-21 00:00:00.000000000 Z
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: