Skip to content

Instantly share code, notes, and snippets.

@mrkplt
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrkplt/35d6d1df5b0105fdd577 to your computer and use it in GitHub Desktop.
Save mrkplt/35d6d1df5b0105fdd577 to your computer and use it in GitHub Desktop.
A sort of fast mock up of how I think you could implement business logic that would be separated from data layer and controller.
class BusinessObject
class LambdaError < StandardError; end;
class NotImplementedError < StandardError; end;
def initialize(lambda_hash = {})
post_init(lambda_hash)
end
def perform
raise NotImplementedError, 'must implement perform.'
end
private
def post_init(lambda_hash)
@success = lambda_hash[:success]
@failure = lambda_hash[:failure]
end
def success
begin
@success.call
rescue NoMethodError
raise LambdaError, 'A lambda for success must be provided'
end
end
def failure
begin
@failure.call
rescue NoMethodError
raise LambdaError, 'A lambda for failure must be provided'
end
end
end
class SaveObjekt < BusinessObject
def initialize(objekt, lambdas)
@objekt = objekt
super(lambdas)
end
def perform
!@objekt.nil? ? success : failure
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment