Module: Leap::Subject
- Defined in:
- lib/leap/subject.rb
Overview
In Leap lingo, Subject refers to the host class, instances of which we’re trying to arrive at conclusions about.
It is within Subjects that we establish decision-making strategies using #decide
Instance Attribute Summary collapse
-
#decisions ⇒ Object
readonly
Accumulates
Leap::Decision
objects, having been defined with#decide
.
Class Method Summary collapse
-
.extended(base) ⇒ Object
Establishes the
@decisions
class instance variable for accumulating Leap decisions, along with an accessor for their deliberations.
Instance Method Summary collapse
-
#decide(goal, options = {}, &blk) ⇒ Object
Defines a new
Leap::Decision
on the subject, using a DSL within its block.
Instance Attribute Details
#decisions ⇒ Object (readonly)
Accumulates Leap::Decision
objects, having been defined with #decide
.
22 23 24 |
# File 'lib/leap/subject.rb', line 22 def decisions @decisions end |
Class Method Details
.extended(base) ⇒ Object
Establishes the @decisions
class instance variable for accumulating Leap decisions, along with an accessor for their deliberations. Also injects Leap::ImplicitAttributes
, which provides a low-budget attribute curation method.
13 14 15 16 17 18 |
# File 'lib/leap/subject.rb', line 13 def self.extended(base) base.instance_variable_set :@decisions, {} base.send :include, ::Leap::ImplicitAttributes base.send :include, ::Leap::DeliberationsAccessor base.send :include, ::Leap::GoalMethodsDocumentation end |
Instance Method Details
#decide(goal, options = {}, &blk) ⇒ Object
Defines a new Leap::Decision
on the subject, using a DSL within its block.
The DSL within the block is primarily composed of Leap::Decision#committee
. Using #decide
will define a new method on instances of the subject class, named after the decision’s goal
, that computes the decision through a process called deliberation (see Leap::GoalMethodsDocumentation
).
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/leap/subject.rb', line 49 def decide(goal, = {}, &blk) decisions[goal] = ::Leap::Decision.new goal, Blockenspiel.invoke(blk, decisions[goal]) define_method goal do |*considerations| Leap.instrument.decision goal do @deliberations ||= {} decision = self.class.decisions[goal] characteristics = send(decision.signature_method) @deliberations[goal] = decision.make(characteristics, *considerations) if decision.mastered? and @deliberations[goal][goal].nil? raise ::Leap::NoSolutionError, :goal => goal, :deliberation => @deliberations[goal] elsif decision.mastered? Leap.log.decision "Success", goal @deliberations[goal][goal] else Leap.log.decision "Success", goal @deliberations[goal] end end end end |