Skip to content

Instantly share code, notes, and snippets.

@laser
Created January 22, 2014 21:34
Show Gist options
  • Save laser/8567785 to your computer and use it in GitHub Desktop.
Save laser/8567785 to your computer and use it in GitHub Desktop.
Real World Rails
# app/services/calculation_service.rb
class CalculationService
def initialize(repository)
@repository = repository
end
def calculate(operation, x, y)
result = StatelessCalculator.send operation, x, y
@repository.save_calculation 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