Skip to content

Commit

Permalink
starting to fetch hooks defined in ancestors
Browse files Browse the repository at this point in the history
  • Loading branch information
gtrias committed Sep 30, 2024
1 parent a519dbb commit 507e76d
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 6 deletions.
70 changes: 70 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

## Uncomment and set this to only include directories you want to watch
# directories %w(app lib config test spec features) \
# .select{|d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist")}

## Note: if you are using the `directories` clause above and you are not
## watching the project directory ('.'), then you will want to move
## the Guardfile to a watched dir and symlink it back, e.g.
#
# $ mkdir config
# $ mv Guardfile config/
# $ ln -s config/Guardfile .
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"

# Note: The cmd option is now required due to the increasing number of ways
# rspec may be run, below are examples of the most common uses.
# * bundler: 'bundle exec rspec'
# * bundler binstubs: 'bin/rspec'
# * spring: 'bin/rspec' (This will use spring if running and you have
# installed the spring binstubs per the docs)
# * zeus: 'zeus rspec' (requires the server to be started separately)
# * 'just' rspec: 'rspec'

guard :rspec, cmd: "bundle exec rspec" do
require "guard/rspec/dsl"
dsl = Guard::RSpec::Dsl.new(self)

# Feel free to open issues for suggestions and improvements

# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(rspec.spec_files)

# Ruby files
ruby = dsl.ruby
dsl.watch_spec_files_for(ruby.lib_files)

# Rails files
rails = dsl.rails(view_extensions: %w(erb haml slim))
dsl.watch_spec_files_for(rails.app_files)
dsl.watch_spec_files_for(rails.views)

watch(rails.controllers) do |m|
[
rspec.spec.call("routing/#{m[1]}_routing"),
rspec.spec.call("controllers/#{m[1]}_controller"),
rspec.spec.call("acceptance/#{m[1]}")
]
end

# Rails config changes
watch(rails.spec_helper) { rspec.spec_dir }
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }

# Capybara features specs
watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }

# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
end
end
34 changes: 28 additions & 6 deletions lib/hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def self.included(base)
end

def run_around_hooks(method, *args, **kwargs, &block)
self.class.around_hooks.reverse.inject(block) do |chain, hook_configuration|
self.class.get_hooks(:around).reverse.inject(block) do |chain, hook_configuration|
next proc { run_hook(hook_configuration, chain, *args, **kwargs) } if hook_configuration.method.nil?

next chain unless hook_configuration.method == method
Expand All @@ -25,35 +25,55 @@ def run_around_hooks(method, *args, **kwargs, &block)
end

def run_before_hooks(method, *args, **kwargs)
run_hooks(method, self.class.before_hooks, *args, **kwargs)
run_hooks(method, self.class.get_hooks(:before), *args, **kwargs)
end

def run_after_hooks(method, *args, **kwargs)
run_hooks(method, self.class.after_hooks, *args, **kwargs)
run_hooks(method, self.class.get_hooks(:after), *args, **kwargs)
end

def run_hooks(method, hooks, *args, **kwargs)
hooks.each do |hook_configuration|
run_hook(hook_configuration, [], *args, **kwargs) if hook_configuration.method.nil?
body = run_hook(hook_configuration, [], *args, **kwargs) if hook_configuration.method.nil?

return body if hook_error?(body)
next unless hook_configuration.method == method

run_hook(hook_configuration, [], *args, **kwargs)
body = run_hook(hook_configuration, [], *args, **kwargs)

return body if hook_error?(body)
end
end

def run_hook(hook_configuration, chain, *args, **kwargs)
hook_configuration.hook.call(chain, *args, **kwargs)
end

def hook_error?(result)
result.respond_to?(:error?) && result.error?
end

# Class methods for the including class.
module ClassMethods
####
# Hooks logic part
####
def before_hooks
# self.class.ancestors.map { |a| a.respond_to?(:before_hooks) ? a.before_hooks : [] }
@before_hooks ||= []
end

def get_hooks(kind)
case kind
when :before
before_hooks
when :after
after_hooks
when :around
around_hooks
end
end

def after_hooks
@after_hooks ||= []
end
Expand Down Expand Up @@ -111,7 +131,9 @@ def decorate_method!(method_name)
# We decorate the method with the before, after and around hooks
define_method(method_name) do |*args, **kwargs|
run_around_hooks(method_name, *args, **kwargs) do
run_before_hooks(method_name, *args, **kwargs)
before_hook_result = run_before_hooks(method_name, *args, **kwargs)

return before_hook_result if hook_error?(before_hook_result)

# Supporting any kind of method, without arguments, with positional
# or with named parameters. Or any combination of them.
Expand Down
34 changes: 34 additions & 0 deletions spec/hook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

require "pry"

class CustomError
def error?
true
end
end

class CookHook
def call(verb); end
end
Expand All @@ -18,10 +24,17 @@ class BeforeAllHook
def call(foo, bar); end
end

class ErroringHook
def call(_verb)
CustomError.new
end
end

class ResourceWithHooks
include Hook

before method: :cook, hook: CookHook.new
before method: :deliver, hook: ErroringHook.new

after method: :prepare, hook: PrepareHook.new
after method: :invented_one, hook: PrepareHook.new
Expand All @@ -43,6 +56,12 @@ def cook
def serve
puts "servig food"
end

def deliver; end
end

class ResourceChildWithHooks < ResourceWithHooks
def foo; end
end

describe Hook do
Expand Down Expand Up @@ -80,4 +99,19 @@ def serve
subject.serve
end
end

context "a hook returns an object which responds to #error?" do
it do
expect(subject.deliver).to be_a(CustomError)
end
end

context "a subclass of a class with hooks should inherit them" do
it do
expect_any_instance_of(CookHook).to receive(:call).once
expect_any_instance_of(BeforeAllHook).to receive(:call).once

ResourceChildWithHooks.new.foo
end
end
end

0 comments on commit 507e76d

Please sign in to comment.