kungfuig 0.5.1 → 0.5.3

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: 581fb0eb44fd71f631bfab19958c88944fc9029c
4
- data.tar.gz: 7828afdfb7f56ec38e31786212610ba6d95dc596
3
+ metadata.gz: fdc42cd90b49647a952a90a49d8c29a4c952822f
4
+ data.tar.gz: c1b33a8b31c3b86dcfeef91685ced514df47e576
5
5
  SHA512:
6
- metadata.gz: 594896fa2caf55cb544aee7e9ce91ed61f10157d130b2a12386dbbe91ebf12e0afe97ff79c4110a0aa82be9d2069a621249d6c647a9375ee18e9451ea6724627
7
- data.tar.gz: f2c511de943a406a379dee7a8c755bdcb7749e6cc826bdd7379e40290147b383f0d39677c3ee4310d2e818566d861d7fb2578b4472d5972f34cdfc9c1aa89e77
6
+ metadata.gz: 5973581c6471eedd579745e5f8025f40a58b3c8ab5d558986bc04fc5a75268d97a303b5eb93c5f3463abe5fd610a2a6b81340f97cdde6894f1792d722874d49b
7
+ data.tar.gz: a5f53d80c9ca338c4e25fcb6b73b0d729994dcc72699c3377b0e564376428d4fac02a7abd7474524c964685caa1e68d90b9a0932ccdc8e779ea6d109f761373d
data/README.md CHANGED
@@ -14,20 +14,20 @@ class MyApp
14
14
  end
15
15
 
16
16
  # Load configuration file
17
- MyApp.config('config/myapp.yml')
17
+ MyApp.kungfuig('config/myapp.yml')
18
18
 
19
19
  # Append options explicitly
20
- MyApp.config do |options|
20
+ MyApp.kungfuig do |options|
21
21
  options.value = 42
22
22
  end
23
23
 
24
24
  # load options from JSON file and execute block on it
25
- MyApp.config('config/myapp.json') do |options|
25
+ MyApp.kungfuig('config/myapp.json') do |options|
26
26
  options.other_value = options[:value]
27
27
  end
28
28
 
29
29
  # DSL (note `kungfuig` method name)
30
- MyApp.kungfuig do
30
+ MyApp.kungfuigure do
31
31
  set :value, 42
32
32
  end
33
33
  ```
@@ -43,7 +43,7 @@ class MyApp
43
43
  end
44
44
  end
45
45
 
46
- MyApp.kungfuig do
46
+ MyApp.kungfuigure do
47
47
  aspect :report do |result|
48
48
  puts "MyApp#report returned #{result}"
49
49
  end
@@ -11,7 +11,7 @@ module Kungfuig
11
11
  when v.include?('*'), v.include?(:'*') then klazz.instance_methods(false)
12
12
  else klazz.instance_methods & v
13
13
  end
14
- end.reduce(&:-)
14
+ end.reduce(&:-) - klazz.instance_methods(false).select { |m| m.to_s.start_with?('to_') }
15
15
  end
16
16
 
17
17
  def remap_hash_for_easy_iteration hash
@@ -29,15 +29,22 @@ module Kungfuig
29
29
  end
30
30
  end
31
31
 
32
+ # rubocop:disable Metrics/CyclomaticComplexity
33
+ # rubocop:disable Metrics/MethodLength
32
34
  def attach(to, before: nil, after: nil, exclude: nil)
33
- klazz = case to
34
- when String then Kernel.const_get(to) # we are ready to get a class name
35
- when Class then to # got a class! wow, somebody has the documentation read
36
- else class << to; self; end # attach to klazz’s eigenclass if object given
35
+ klazz = begin
36
+ case to
37
+ when Class then to # got a class! wow, somebody has the documentation read
38
+ when String then Kernel.const_get(to) # we are ready to get a class name
39
+ when Symbol then Kernel.const_get(to.to_s)
40
+ else class << to; self; end # attach to klazz’s eigenclass if object given
41
+ end
42
+ rescue => e
43
+ raise ArgumentError, "Unable to attach to #{to}. I need a valid class/method name!\nOriginal exception: #{e.message}"
37
44
  end
38
45
 
39
46
  raise ArgumentError, "Trying to attach nothing to #{klazz}##{to}. I need a block!" unless block_given?
40
- klazz.send(:include, Kungfuig::Aspector) unless klazz.ancestors.include? Kungfuig::Aspector
47
+ klazz.send(:include, Kungfuig) unless klazz.ancestors.include? Kungfuig
41
48
  cb = Proc.new
42
49
 
43
50
  H.new.value_to_method_list(klazz, before, exclude).each do |m|
@@ -53,6 +60,8 @@ module Kungfuig
53
60
  klazz.aspects
54
61
  end
55
62
  module_function :attach
63
+ # rubocop:enable Metrics/MethodLength
64
+ # rubocop:enable Metrics/CyclomaticComplexity
56
65
 
57
66
  # 'Test':
58
67
  # after:
@@ -70,7 +79,7 @@ module Kungfuig
70
79
  raise ArgumentError, [
71
80
  "Bad input to Kungfuig::Aspector##{__callee__}.",
72
81
  "Args: #{methods.inspect}",
73
- "Original exception: “#{e.message}”.",
82
+ "Original exception: #{e.message}.",
74
83
  e.backtrace.unshift("Backtrace:").join("#{$/}⮩ ")
75
84
  ].join($/.to_s)
76
85
  end
@@ -88,7 +88,7 @@ module Kungfuig
88
88
  when Fixnum then Color.new(val, val, val) # Single value, assume grayscale
89
89
  when String
90
90
  str = val.to_s.upcase[/[0-9A-F]{3,8}/] || ''
91
- Color.new(*case str.size
91
+ Color.new(*case str.length
92
92
  when 3, 4 then str.scan(/[0-9A-F]/)
93
93
  when 6, 8 then str.scan(/[0-9A-F]{2}/)
94
94
  else 'FF'
@@ -23,19 +23,35 @@ module Kungfuig
23
23
  )
24
24
  end
25
25
 
26
+ # rubocop:disable Metrics/AbcSize
27
+ # rubocop:disable Metrics/CyclomaticComplexity
28
+ # rubocop:disable Metrics/PerceivedComplexity
29
+ # rubocop:disable Metrics/MethodLength
26
30
  def bottleneck(receiver, method, result, *args)
27
31
  respond_to = ->(m, r) { r.respond_to? m.to_sym }
28
32
  r = case receiver
33
+ when Hash, Array, String then receiver
29
34
  when respond_to.curry[:to_hash] then receiver.to_hash
30
35
  when respond_to.curry[:to_h] then receiver.to_h
31
36
  else receiver
32
37
  end
38
+ return unless (receiver_class = receiver.class.ancestors.detect do |c|
39
+ @hash[c.name] && @hash[c.name][method]
40
+ end)
33
41
 
34
- Kernel.const_get(@hash[receiver.class.name][method])
35
- .perform_async(r, method, result, *args)
42
+ job = Kernel.const_get(@hash[receiver_class.name][method])
43
+ if Kernel.const_defined?('Rails') && Rails.env.development?
44
+ job.new.perform(r, method, result, *args)
45
+ else
46
+ job.perform_async(r, method, result, *args)
47
+ end
36
48
  rescue => e
37
49
  Kungfuig.✍("Fail [#{e.message}] while #{receiver}", method, result, *args)
38
50
  end
51
+ # rubocop:enable Metrics/MethodLength
52
+ # rubocop:enable Metrics/PerceivedComplexity
53
+ # rubocop:enable Metrics/CyclomaticComplexity
54
+ # rubocop:enable Metrics/AbcSize
39
55
  end
40
56
  end
41
57
  end
@@ -1,3 +1,3 @@
1
1
  module Kungfuig
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.3"
3
3
  end
data/lib/kungfuig.rb CHANGED
@@ -7,6 +7,8 @@ require 'kungfuig/aspector'
7
7
 
8
8
  module Kungfuig
9
9
  ASPECT_PREFIX = '♻_'.freeze
10
+ ASPECT_TEMPLATE = -> { %i(after before).map { |k| [k, []] }.to_h }
11
+
10
12
  MX = Mutex.new
11
13
 
12
14
  # rubocop:disable Style/VariableName
@@ -52,7 +54,7 @@ module Kungfuig
52
54
  # Configures everything by hash or yaml from string or file. Whether code block
53
55
  # is passed, it is processed with @options instance.
54
56
  # @param hos [String|Hash|Hashie::Mash] the input data to merge into options
55
- def config hos = nil
57
+ def kungfuig hos = nil
56
58
  MX.synchronize {
57
59
  merge_hash_or_string! hos
58
60
  yield options if block_given?
@@ -113,6 +115,22 @@ module Kungfuig
113
115
  options.deep_merge! Kungfuig.load_stuff hos
114
116
  end
115
117
  private :merge_hash_or_string!
118
+
119
+ def lookup_aspects meth
120
+ meth = meth.to_sym
121
+ eigenclass = class << self; self; end
122
+ return eigenclass.aspects(meth) if eigenclass.respond_to?(:aspects?) && eigenclass.aspects?
123
+ aspected = self.class.ancestors.detect do |c|
124
+ c.respond_to?(:aspects?) && c.aspects?
125
+ end
126
+
127
+ aspected ? aspected.aspects(meth) : Kungfuig::ASPECT_TEMPLATE.call
128
+
129
+ # self.class.ancestors.inject(initial) do |memo, c|
130
+ # c.respond_to?(:aspects?) && c.aspects? ? c.aspects(meth).merge(memo) { |_, c1, c2| c1 | c2 } : memo
131
+ # end
132
+ end
133
+ private :lookup_aspects
116
134
  end
117
135
 
118
136
  def self.included base
@@ -133,7 +151,7 @@ module Kungfuig
133
151
 
134
152
  # A wrapper for the configuration block
135
153
  # @param block the block to be executed in the context of this module
136
- def kungfuig &block
154
+ def kungfuigure &block
137
155
  instance_eval(&block)
138
156
  end
139
157
 
@@ -148,7 +166,7 @@ module Kungfuig
148
166
  class_eval <<-CODE
149
167
  alias_method :'#{ASPECT_PREFIX}#{meth}', :'#{meth}'
150
168
  def #{meth}(*args, &cb)
151
- ps = self.class.aspects(:'#{meth}').merge((class << self; self; end).aspects(:'#{meth}')) { |_, c, ec| c | ec }
169
+ ps = lookup_aspects(:'#{meth}')
152
170
  ps[:before].each do |p|
153
171
  p.call(self, :'#{meth}', nil, *args) # FIXME: allow argument modification!!!
154
172
  end
@@ -165,9 +183,13 @@ module Kungfuig
165
183
  end
166
184
  # rubocop:enable Metrics/MethodLength
167
185
 
186
+ def aspects?
187
+ @aspects.is_a?(Hash)
188
+ end
189
+
168
190
  def aspects meth = nil
169
191
  @aspects ||= {}
170
- meth ? @aspects[meth.to_sym] ||= {after: [], before: []} : @aspects
192
+ meth ? @aspects[meth.to_sym] ||= Kungfuig::ASPECT_TEMPLATE.call : @aspects
171
193
  end
172
194
  alias_method :set, :option!
173
195
  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.5.1
4
+ version: 0.5.3
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-25 00:00:00.000000000 Z
11
+ date: 2016-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie