require "testable/situation" module Testable module Ready include Situation module ClassMethods def ready_validations if superclass.respond_to?(:ready_validations) superclass.ready_validations + _ready_validations else _ready_validations end end def page_ready(&block) _ready_validations << block end alias page_ready_when page_ready def _ready_validations @_ready_validations ||= [] end end attr_accessor :ready, :ready_error def self.included(caller) caller.extend(ClassMethods) end def when_ready(&_block) already_marked_ready = ready no_ready_check_possible unless block_given? self.ready = ready? not_ready_validation(ready_error || 'NO REASON PROVIDED') unless ready yield self ensure self.ready = already_marked_ready end def ready? self.ready_error = nil return true if ready ready_validations_pass? end def ready_validations_pass? self.class.ready_validations.all? do |validation| passed, message = instance_eval(&validation) self.ready_error = message if message && !passed passed end end end end