Skip to content

Instantly share code, notes, and snippets.

@katpadi

katpadi/gist:7d6829c373a3aa91002a Secret

Last active Aug 29, 2015
Embed
What would you like to do?
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