Skip to content

Instantly share code, notes, and snippets.

@katpadi
Last active August 29, 2015 14:21
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 katpadi/7d6829c373a3aa91002a to your computer and use it in GitHub Desktop.
Save katpadi/7d6829c373a3aa91002a to your computer and use it in GitHub Desktop.
TransactionLimitChecker
class TransactionLimitChecker
def initialize(transaction)
@transaction = transaction
end
def limit_is_reached?(user)
per_user_limit_reached?(user) ||
per_interval_limit_reached?(user) ||
overall_max_limit_reached?
end
def per_user_limit_reached?(user)
# Lots of logic here
# of
# logic
# here...
@transaction.count > some_count
end
def per_interval_limit_reached?(user)
# Logic...
# put here your logic
# Use some private methods...whatever
check_interval_validity!
limit = blah
Time.current.utc <= @transaction.updated_at.time + limit
end
def overall_max_limit_reached?
# More logic!
# Just put 'em here
@transaction.over_all_submission_count > total_something
end
private
def check_interval_validity!
# Check interval unit if valid ActiveSupport::Duration
valid = 1.respond_to? @transaction.submission_interval_unit.to_sym
fail Errors::InvalidIntervalDefined unless valid
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment