cpt_hook 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 74f93a1da89cc4dc1b52ea059d17a148432734ae
4
- data.tar.gz: 9e73cecbe75bf44df3b5e2c18490741d83c66b87
3
+ metadata.gz: 290b3737b4f793c6e0195069dd2619371151acb3
4
+ data.tar.gz: f3acdb2a5c73e36241291f692a3ffa947c066c00
5
5
  SHA512:
6
- metadata.gz: '092e9dbbcd208b37cc42779ad2f6b2cc3f8fe613fae038acaaa13c4e2e9793281ceefdc2a12e098ad177af3d9049d8b1c88db5bcd5c9a01ef6ed44b925427597'
7
- data.tar.gz: 235607f27dda02ec5777496e9f65c1d934bb2a59ea39d98a072e3fea541f025a699067c1e7cab5399bee998daeb334b90c712538b890cfbcffc47359fee1d5d3
6
+ metadata.gz: f23f687885beeed890c16b38251b6a6e3d1facca64c687607dd1784a40544c0f26634b89cf769bf66cf768ae08e51ca0e5e1243c1f05ba1a7cd9c878cc57b95a
7
+ data.tar.gz: 1961025f695ae8c109d83f1e973d6cc15f7c7b364b3941a7c2901c5cc5f0a9aa8fca325222b7dc0cb3b704893744651563f13978a28793dff865f05171cd1bfd
data/bin/console CHANGED
@@ -2,13 +2,58 @@
2
2
 
3
3
  require "bundler/setup"
4
4
  require "cpt_hook"
5
+ require "pry"
5
6
 
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
7
 
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
8
+ my_hooks = CptHook.define_hooks do
9
+ # Simple case call one fn with args
10
+ before(:some_fn).call(:my_hook).with(:var1, :var2)
12
11
 
13
- require "irb"
14
- IRB.start(__FILE__)
12
+ # Complex case, add multiple calls to the fn block style
13
+ after(:some_other_fn) do
14
+ call(:my_other_hook).with(:var3, :var4)
15
+ call(:yet_another_hook)
16
+ call( lambda { | hooked_obj | hooked_obj.some_fn }).with(:self)
17
+ end
18
+
19
+ # Complex case, add multiple calls to the fn fluent style
20
+ before(:some_other_fn).call(:my_other_hook).with(:var3, :var4)
21
+ .call(:yet_another_hook)
22
+ .call( lambda { | hooked_obj | hooked_obj.some_fn }).with(:self)
23
+
24
+ end
25
+
26
+
27
+ class ResTest
28
+ def ext_res_fn
29
+ puts 'Resolved external context'
30
+ end
31
+ end
32
+
33
+ class TestClass
34
+ def res_fn
35
+ puts 'Resolved internal context'
36
+ end
37
+
38
+ def test_fn
39
+ puts 'test_fn'
40
+ end
41
+ end
42
+
43
+ external_context = ResTest.new
44
+
45
+ c = TestClass.new
46
+
47
+ test_hooks = CptHook.define_hooks do
48
+ before(:test_fn).call(:res_fn)
49
+ end
50
+
51
+ test_hooks2 = CptHook.define_hooks do
52
+ before(:test_fn).call(:res_fn).call(:ext_res_fn).using( lambda { return external_context })
53
+ end
54
+
55
+ hooked = CptHook::Hookable.new(c, test_hooks.merge!(test_hooks2))
56
+ hooked.test_fn
57
+ binding.pry
58
+
59
+ puts 'Console exiting' # This is just to ensure a line exists below the pry
data/cpt_hook.gemspec CHANGED
@@ -23,4 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency 'bundler', '~> 1.16'
24
24
  spec.add_development_dependency 'rake', '~> 10.0'
25
25
  spec.add_development_dependency 'rspec', '~> 3.0'
26
+ spec.add_development_dependency 'pry'
27
+ spec.add_development_dependency 'pry-byebug'
28
+ spec.add_development_dependency 'pry-stack_explorer'
26
29
  end
@@ -0,0 +1,42 @@
1
+ module CptHook
2
+
3
+ module DSL
4
+
5
+ class HookDefinition
6
+ attr_reader :method, :hook_type, :call_chain
7
+
8
+ def initialize(method_to_hook, type)
9
+ @method = method_to_hook
10
+ @hook_type = type
11
+ @call_chain = []
12
+ end
13
+
14
+ def call(method_to_call)
15
+ @call_chain << MethodCall.new(method_to_call)
16
+ self
17
+ end
18
+
19
+ def with(*args)
20
+ @call_chain.last.with(*args)
21
+ self
22
+ end
23
+
24
+ def using(*args)
25
+ @call_chain.last.using(*args)
26
+ self
27
+ end
28
+
29
+ def contexts(*args)
30
+ @call_chain.last.contexts(*args)
31
+ self
32
+ end
33
+
34
+ def merge!(other)
35
+ other.call_chain.each do |cc|
36
+ @call_chain << cc unless @call_chain.any? { |c| c.method == cc.method }
37
+ end
38
+ self
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,47 @@
1
+ module CptHook
2
+ module DSL
3
+ class HookDefinitions
4
+
5
+ def initialize
6
+ @hooks = []
7
+ end
8
+
9
+ def hooks(hook_type = nil)
10
+ hook_type.nil? ? @hooks : @hooks.select { |h| h.hook_type == hook_type }
11
+ end
12
+
13
+ def hooked_methods
14
+ @hooks.map { |h| h.method }.uniq
15
+ end
16
+
17
+ def before(method_to_hook, &block)
18
+ _hook_definition(method_to_hook, :before, &block)
19
+ end
20
+
21
+ def after(method_to_hook, &block)
22
+ _hook_definition(method_to_hook, :after, &block)
23
+ end
24
+
25
+ def merge!(other)
26
+ other.hooks.each do |hook|
27
+ my_hook = @hooks.find { |h| (h.method == hook.method) && (h.hook_type == hook.hook_type) }
28
+ if my_hook
29
+ my_hook.merge!(hook)
30
+ else
31
+ @hooks << hook
32
+ end
33
+ end
34
+ self
35
+ end
36
+
37
+ private
38
+
39
+ def _hook_definition(method_to_hook, hook_type, &block)
40
+ hook_def = HookDefinition.new(method_to_hook, hook_type)
41
+ @hooks << hook_def
42
+ hook_def.instance_eval(&block) if block_given?
43
+ hook_def
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,21 @@
1
+ module CptHook
2
+ module DSL
3
+ class MethodCall
4
+ attr_reader :method, :with, :contexts
5
+
6
+ def initialize(method_to_call)
7
+ @method = method_to_call
8
+ @contexts = []
9
+ @with = []
10
+ end
11
+
12
+ def with(*args)
13
+ @with = args
14
+ end
15
+
16
+ def using(*args)
17
+ @contexts = args
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ require 'cpt_hook/dsl/method_call'
2
+ require 'cpt_hook/dsl/hook_definition'
3
+ require 'cpt_hook/dsl/hook_definitions'
@@ -13,10 +13,9 @@ module CptHook
13
13
  _add_hooks(:before, method_hooks, additional_contexts)
14
14
  _add_hooks(:after, method_hooks, additional_contexts)
15
15
 
16
- method_hooks.map {|h| h[:before] || h[:after]}.uniq.each do |method|
16
+ method_hooks.hooked_methods.each do |method|
17
17
  define_singleton_method(method) do |*args, &block|
18
18
  self.send("before_#{method}") if self.respond_to? "before_#{method}"
19
- #val = __getobj__.send(method, *args, &block)
20
19
  val = super(*args, &block)
21
20
  self.send("after_#{method}") if self.respond_to? "after_#{method}"
22
21
  val
@@ -31,19 +30,18 @@ module CptHook
31
30
  private
32
31
 
33
32
  def _add_hooks(which, method_hooks, additional_contexts)
34
- method_hooks.select {|hook| hook.key?(which)}.each do |hook|
35
- hook[:contexts] = hook.fetch(:contexts, []).concat(additional_contexts)
36
- define_singleton_method("#{which}_#{hook[which]}") do |*args, &block|
37
- hook[:call_chain].each do |call_chain|
38
- call_args = call_chain.fetch(:with, []).map { |ca| ca == :self ? self : ca }
39
-
40
- hook_fn = call_chain[:call]
41
- if hook_fn.is_a?(Proc)
42
- hook_fn.call(*call_args)
33
+ method_hooks.hooks(which).each do |hook|
34
+ define_singleton_method("#{which}_#{hook.method}") do |*args, &block|
35
+ hook.call_chain.each do |call_chain|
36
+ call_args = call_chain.with.map { |ca| ca == :self ? self : ca }
37
+ if call_chain.method.is_a?(Proc)
38
+ call_chain.method.call(*call_args)
43
39
  else
44
- context = hook[:contexts].unshift(__getobj__).find {|c| c.respond_to?(hook_fn)}
45
- raise "No context found for #{which} hook: #{hook_fn}" unless context
46
- context.send(hook_fn, *call_args)
40
+ contexts = call_chain.contexts.concat(additional_contexts).unshift(__getobj__)
41
+ contexts.map!{ |c| c.is_a?(Proc) ? c.call : c }
42
+ context = contexts.find {|c| c.respond_to?(call_chain.method)}
43
+ raise "No context found for #{which} hook: #{call_chain.method}" unless context
44
+ context.send(call_chain.method, *call_args)
47
45
  end
48
46
  end
49
47
  end
@@ -1,3 +1,3 @@
1
1
  module CptHook
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0'
3
3
  end
data/lib/cpt_hook.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  require 'cpt_hook/version'
2
2
  require 'cpt_hook/hookable'
3
-
3
+ require 'cpt_hook/dsl'
4
4
  module CptHook
5
- # Your code goes here...
5
+ def self.define_hooks(&block)
6
+ dsl = DSL::HookDefinitions.new
7
+ dsl.instance_eval(&block) if block_given?
8
+ dsl
9
+ end
6
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cpt_hook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - JDonavan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-27 00:00:00.000000000 Z
11
+ date: 2017-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,48 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-stack_explorer
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
55
97
  description:
56
98
  email:
57
99
  - donavan.stanley@gmail.com
@@ -78,6 +120,10 @@ files:
78
120
  - bin/setup
79
121
  - cpt_hook.gemspec
80
122
  - lib/cpt_hook.rb
123
+ - lib/cpt_hook/dsl.rb
124
+ - lib/cpt_hook/dsl/hook_definition.rb
125
+ - lib/cpt_hook/dsl/hook_definitions.rb
126
+ - lib/cpt_hook/dsl/method_call.rb
81
127
  - lib/cpt_hook/hookable.rb
82
128
  - lib/cpt_hook/version.rb
83
129
  homepage: https://github.com/Donavan/cpt_hook