Skip to content

Instantly share code, notes, and snippets.

@jak
Forked from geekygrappler/promos.rb
Last active June 13, 2017 08:48
Show Gist options
  • Save jak/f44dc6b9cb97c33a8cb2d684e1d9c162 to your computer and use it in GitHub Desktop.
Save jak/f44dc6b9cb97c33a8cb2d684e1d9c162 to your computer and use it in GitHub Desktop.
Pseudo code
## Promocode controller/generate endpoint controller
def generate
if not @promocode.is_valid?
#An array of errors
render json: @promocode.errors
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 = []
# this will end up being a multidimensional array of errors.... because i'm lazy
errors = self.promotion.constraints.map { |con| con.validate }
return errors
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