Skip to content

Instantly share code, notes, and snippets.

@laser
Last active January 4, 2016 10:19
Show Gist options
  • Save laser/8607949 to your computer and use it in GitHub Desktop.
Save laser/8607949 to your computer and use it in GitHub Desktop.
service.rb
# app/utils/calculator.rb
class StatelessCalculator
def self.add(x, y)
x + y
end
# ...subtract, divide, multiply, etc.
end
# app/services/calculation_isolate.rb
class CalculationService
def initialize(logger)
@logger = logger
end
def calculate(operation, x, y)
result = StatelessCalculator.send operation, x, y
@logger.log operation, x, y, result
result
end
end
# app/controllers/calculation.rb
def create
h = ActiveSupport::JSON.decode(request.body)
@result = MyApp::Services.calculation.calculate h["operation"], h["x"], h["y"]
render :calculation_view
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment