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

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#decisionsObject (readonly)

Accumulates Leap::Decision objects, having been defined with #decide.

See Also:



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).

Examples:

Defining a Leap decision

class Person
  include Leap
  decide :lucky_number do
    # Decision definition elided (see Leap::Decision#committee)
  end
end

Person.new.lucky_number # => 42

Parameters:

  • goal (Symbol)

    The goal of the decision–what is it that we’re trying to decide about the subject?

  • options (optional, Hash) (defaults to: {})
  • [optional, (Hash)

    a customizable set of options

See Also:



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, options = {}, &blk)
  decisions[goal] = ::Leap::Decision.new goal, options
  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