Class: AffirmIt::Facilitator

Inherits:
Object
  • Object
show all
Defined in:
lib/affirmit/facilitator.rb

Overview

A facilitator embraces affirmations and notifies any interested classes of behavioral challenges encountered with the affirmed code. This class is similar to a "test runner" in less sensitive behavioral evaluation frameworks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(listener) ⇒ Facilitator

Returns a new instance of Facilitator.



22
23
24
25
26
27
28
29
# File 'lib/affirmit/facilitator.rb', line 22

def initialize(listener)
  @affirmation_count = 0
  @cherished_affirmation_count = 0
  @preference_count = 0
  @bonus_points = 0
  @events = []
  @listener = listener
end

Instance Attribute Details

#affirmation_countObject (readonly)

Returns the value of attribute affirmation_count.



19
20
21
# File 'lib/affirmit/facilitator.rb', line 19

def affirmation_count
  @affirmation_count
end

#bonus_pointsObject (readonly)

Returns the value of attribute bonus_points.



19
20
21
# File 'lib/affirmit/facilitator.rb', line 19

def bonus_points
  @bonus_points
end

#cherished_affirmation_countObject (readonly)

Returns the value of attribute cherished_affirmation_count.



19
20
21
# File 'lib/affirmit/facilitator.rb', line 19

def cherished_affirmation_count
  @cherished_affirmation_count
end

#eventsObject (readonly)

Returns the value of attribute events.



19
20
21
# File 'lib/affirmit/facilitator.rb', line 19

def events
  @events
end

#listenerObject

Returns the value of attribute listener.



18
19
20
# File 'lib/affirmit/facilitator.rb', line 18

def listener
  @listener
end

#preference_countObject (readonly)

Returns the value of attribute preference_count.



19
20
21
# File 'lib/affirmit/facilitator.rb', line 19

def preference_count
  @preference_count
end

Instance Method Details

#add_affirmationObject

Registers that an individual affirmation has been embraced -- not a group hug.



63
64
65
66
# File 'lib/affirmit/facilitator.rb', line 63

def add_affirmation
  @affirmation_count += 1
  @listener.notify self, Event.new(:affirmation_added)
end

#add_bonus_pointObject

Adds another bonus point to the affirmation's embrace. If you get enough bonus points, it can make up for any differing opinions you may have!



82
83
84
85
# File 'lib/affirmit/facilitator.rb', line 82

def add_bonus_point
  @bonus_points += 1
  @listener.notify self, Event.new(:bonus_point_added)
end

#add_event(event) ⇒ Object



163
164
165
166
# File 'lib/affirmit/facilitator.rb', line 163

def add_event event
  @events << event
  @listener.notify self, @events.last
end

#add_preferenceObject

Registers that an affirmation has a certain preference; whether the preference is "true" or "false" in our particular, subjective frame of reference does not matter.



73
74
75
76
# File 'lib/affirmit/facilitator.rb', line 73

def add_preference
  @preference_count += 1
  @listener.notify self, Event.new(:preference_added)
end

#admit_challenge(challenge) ⇒ Object

Some affirmations may present challenges that the facilitator will note.



112
113
114
# File 'lib/affirmit/facilitator.rb', line 112

def admit_challenge challenge
  add_event IssueEvent.new(:behavioral_challenge_admitted, challenge)
end

#behavioral_challenge_countObject



131
132
133
# File 'lib/affirmit/facilitator.rb', line 131

def behavioral_challenge_count
  count_events :behavioral_challenge_admitted
end

#cherish_affirmationObject



55
56
57
58
# File 'lib/affirmit/facilitator.rb', line 55

def cherish_affirmation
  @cherished_affirmation_count += 1
  @listener.notify self, Event.new(:affirmation_cherished)
end

#count_events(type) ⇒ Object



168
169
170
# File 'lib/affirmit/facilitator.rb', line 168

def count_events type
  @events.count { |event| event.type == type }
end

#defer_success(deferral) ⇒ Object

A facilitator may defer the success of an affirmation, cherishing it until it may be praised in fullness.



96
97
98
# File 'lib/affirmit/facilitator.rb', line 96

def defer_success deferral
  add_event IssueEvent.new(:success_deferred, deferral)
end

#deferred_success_countObject



139
140
141
# File 'lib/affirmit/facilitator.rb', line 139

def deferred_success_count
  count_events :success_deferred
end

#differing_opinion_countObject



135
136
137
# File 'lib/affirmit/facilitator.rb', line 135

def differing_opinion_count
  count_events :differing_opinion_encountered
end

#embrace(affirmation) ⇒ Object

Embraces the given affirmation, noting any challenges encountered therein. Intolerant pigs will be expelled immediately.



35
36
37
38
39
# File 'lib/affirmit/facilitator.rb', line 35

def embrace affirmation
  @listener.notify self, AffirmationEvent.new(:embrace_started, affirmation)
  affirmation.embrace self
  @listener.notify self, AffirmationEvent.new(:embrace_ended, affirmation)
end

#espouse_differing_opinion(opinion) ⇒ Object

Facilitators may espouse differing opinions. This is not something so banal as an "failure", of course, since all opinions are completely valid in their own special ways.



105
106
107
# File 'lib/affirmit/facilitator.rb', line 105

def espouse_differing_opinion opinion
  add_event IssueEvent.new(:differing_opinion_encountered, opinion)
end

#expel(pig) ⇒ Object

Intolerant pigs will not be tolerated by a facilitator and will be expelled immediately.



126
127
128
129
# File 'lib/affirmit/facilitator.rb', line 126

def expel pig
  @listener.notify self, IssueEvent.new(:pig_expelled, pig)
  Kernel.exit 3
end

#issue_countObject



143
144
145
# File 'lib/affirmit/facilitator.rb', line 143

def issue_count
  count_events :issue_raised
end

#praise(message) ⇒ Object

Lavish praise with the given message.



89
90
91
# File 'lib/affirmit/facilitator.rb', line 89

def praise message
  add_event PraiseEvent.new(:praise, message)
end

#raise_issue(exception) ⇒ Object

Attempting to embrace an affirmation may at times raise unexpected issues, noted here.



119
120
121
# File 'lib/affirmit/facilitator.rb', line 119

def raise_issue exception
  add_event IssueEvent.new(:issue_raised, exception)
end

#starsObject

The number of stars, from 3.0 to 5.0. We don't want to hurt anybody's feelings by giving them less than 3!



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/affirmit/facilitator.rb', line 150

def stars
  if @affirmation_count == 0 then
    5.0
  else
    non_cherished_affirmations = @affirmation_count - @cherished_affirmation_count - @bonus_points
    if non_cherished_affirmations <= 0
      5.0
    else
      5.0 - ((non_cherished_affirmations) * 2.0 / @affirmation_count.to_f)
    end
  end
end

#with_arms_around(affirmation) ⇒ Object

Wraps the given affirmation in its arms, ensuring that any interested parties are notified that the facilitator's arms are being wrapped around, and disentangled from, the given affirmation.



46
47
48
49
50
51
52
53
# File 'lib/affirmit/facilitator.rb', line 46

def with_arms_around affirmation
  @listener.notify self, AffirmationEvent.new(:arms_wrapped_around, affirmation)
  begin
    yield
  ensure
    @listener.notify self, AffirmationEvent.new(:arms_disentangled_from, affirmation)
  end
end