Module: AffirmIt::Esteem
Overview
Mixin for adding esteem to your class. Your class achieves good self-esteem by calling the methods of this class.
Constant Summary collapse
- DEFAULT_PREFERENCE =
AffirmIt::Preference::Is.new true
Instance Attribute Summary collapse
-
#frame_of_reference ⇒ Object
Since we’re embracing relativism, what is true depends entirely on your frame of reference.
Instance Method Summary collapse
-
#add_bonus_point ⇒ Object
Classes that wish to track bonus points may override this method accordingly.
-
#add_preference ⇒ Object
Classes that wish to note the existence of preferences may override this method accordingly.
-
#defer_success(msg = 'Success deferred') ⇒ Object
Use this method when you want to explicitly and immediately defer success of the affirmation.
- #initialize ⇒ Object
- #maybe(something) ⇒ Object
- #method_missing(name, *args, &block) ⇒ Object
-
#praise(object, msg = "Great job!") ⇒ Object
Every object needs a little encouragement sometimes.
- #prefer_no_raise ⇒ Object
-
#prefer_raise(expected) ⇒ Object
This method is used to indicate a preference toward exceptions in certain cases.
-
#prefer_that(actual, preference = DEFAULT_PREFERENCE, msg = '') ⇒ Object
Prefers that a certain value agrees with your preference, thus:.
Methods included from Preferences
#a, #an, #between, #equals, #greater_than, #includes, #is, #isnt, #less_than, #matches, #responds_to, #same_as
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/affirmit/esteem.rb', line 127 def method_missing name, *args, &block # Some programmers will fall back to their old habits of # "asserting" whether something is "true" or "false". # We must stand firm against this sort of thinking and # redirect them to the preferred path. if name =~ /^assert_.*/ then msg = "Who are you to say what's true? Please use prefer_ methods using a suitable frame of reference. Geeze, you probably hate puppies too." raise IntolerantPig.new(msg) else super name, *args, &block end end |
Instance Attribute Details
#frame_of_reference ⇒ Object
Since we’re embracing relativism, what is true depends entirely on your frame of reference. This class will provide a default frame, but if it does not suit your needs – i.e., if it fails to result in sufficiently high self-esteem – you might consider changing your frame of reference.
50 51 52 |
# File 'lib/affirmit/esteem.rb', line 50 def frame_of_reference @frame_of_reference end |
Instance Method Details
#add_bonus_point ⇒ Object
Classes that wish to track bonus points may override this method accordingly.
84 85 |
# File 'lib/affirmit/esteem.rb', line 84 def add_bonus_point end |
#add_preference ⇒ Object
Classes that wish to note the existence of preferences may override this method accordingly.
78 79 |
# File 'lib/affirmit/esteem.rb', line 78 def add_preference end |
#defer_success(msg = 'Success deferred') ⇒ Object
Use this method when you want to explicitly and immediately defer success of the affirmation. This is analogous to the Test::Unit flunk method. Of course, that kind of attitude is not tolerated here.
123 124 125 |
# File 'lib/affirmit/esteem.rb', line 123 def defer_success msg = 'Success deferred' raise ElectiveDeferral.new msg end |
#initialize ⇒ Object
52 53 54 |
# File 'lib/affirmit/esteem.rb', line 52 def initialize @frame_of_reference = DefaultFrameOfReference.new end |
#maybe(something) ⇒ Object
56 57 58 59 60 |
# File 'lib/affirmit/esteem.rb', line 56 def maybe(something) if @frame_of_reference.is_true(something) then add_bonus_point end end |
#praise(object, msg = "Great job!") ⇒ Object
Every object needs a little encouragement sometimes. Subclasses should make sure that this praise is passed on to the facilitator.
91 92 |
# File 'lib/affirmit/esteem.rb', line 91 def praise object, msg = "Great job!" end |
#prefer_no_raise ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/affirmit/esteem.rb', line 109 def prefer_no_raise add_preference begin yield rescue Exception => ex raise BehavioralChallenge.new("We were not expecting a raise, but we got #{ex.class}") end end |
#prefer_raise(expected) ⇒ Object
This method is used to indicate a preference toward exceptions in certain cases.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/affirmit/esteem.rb', line 97 def prefer_raise expected add_preference begin yield rescue Exception => ex raise BehavioralChallenge.new("expected #{expected}, got #{ex.class}") unless ex.is_a?(expected) return ex else raise BehavioralChallenge.new("We really preferred to receive an exception of type #{expected}") unless exception end end |
#prefer_that(actual, preference = DEFAULT_PREFERENCE, msg = '') ⇒ Object
Prefers that a certain value agrees with your preference, thus:
prefer_that (2 + 2), is(5) prefer_that [:lisp], ((includes :infix_operators) & ~(includes :parentheses)) | (is [:ruby])
68 69 70 71 72 73 |
# File 'lib/affirmit/esteem.rb', line 68 def prefer_that actual, preference = DEFAULT_PREFERENCE, msg = '' add_preference unless @frame_of_reference.is_true(preference.is_preferred? actual) raise DifferingOpinion, "Preferred #{preference.description}, got <#{actual}>. #{msg}" end end |