Created
March 18, 2019 01:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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