Skip to content

Instantly share code, notes, and snippets.

@geekygrappler
Created June 12, 2017 22:27
Show Gist options
  • Save geekygrappler/87219a5b4e108636a58b51a118d11e2b to your computer and use it in GitHub Desktop.
Save geekygrappler/87219a5b4e108636a58b51a118d11e2b to your computer and use it in GitHub Desktop.
Pseudo code
## Promocode controller/generate endpoint controller
def generate
begin
@promocode.is_valid?
rescue e #An array of errors
render json: e
end
if @promocode.save
blah
end
end
## Promocode model
def is_valid?
# I wan't to map over the constraints and run their validate function which should either return nil or throw a
# ConstraintError, but I want this method to swallow that throw and append it to an array of errors
# and then throw those errors up. But I don't really know how to do that.
errors = []
begin
self.promotion.constraints.map{|con| con.validate}
rescue e
errors << e
next
end
if errors
throw errors
else
true
end
end
## A constraint
class Constraint
def validate
# if some criteria isn't met, throw a Constraint error
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment