Skip to content

Instantly share code, notes, and snippets.

@lxbrito
Created March 18, 2019 01:40
Show Gist options
  • Save lxbrito/aefb7f2ff1fb3980dd04103228723d10 to your computer and use it in GitHub Desktop.
Save lxbrito/aefb7f2ff1fb3980dd04103228723d10 to your computer and use it in GitHub Desktop.
ActiveBusiness - Proposal to ease the pain when refactoring legacy code on fat controllers
# frozen_string_literal: true
# :nodoc:
class ActiveBusiness
attr_reader :resolver
def initialize
@resolver = ActiveBusiness::Resolver.new
yield resolver
end
def resolve_to(action)
@resolver.responses.fetch(action) { fail("#{action} not implemented") }
end
# ActiveBusiness response resolver
class Resolver
attr_reader :responses
def initialize
@responses = {}
end
def sucess(&block)
fail 'Response must receive a block!' unless block_given?
@responses[:sucess] = block
end
def error(&block)
fail 'Response must receive a block!' unless block_given?
@responses[:error] = block
end
def method_missing(name, *_args, &block)
# In case success and error not being enough
fail 'Response must receive a block!' unless block_given?
@responses[name] = block
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment