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
70
71
72
73
74
75
76
77
78
# File 'lib/leap/subject.rb', line 49

def decide(goal, options = {}, &blk)
  decisions[goal] = ::Leap::Decision.new goal, options
  decisions[goal].instance_eval(&blk)
  define_method goal do |*considerations|
    decision = self.class.decisions[goal]
    options = considerations.extract_options!
    @deliberations ||= {}
    Leap.instrument.decision goal do
      characteristics = send(decision.signature_method)
      @deliberations[goal] = decision.make(characteristics, options, *considerations)
    end
    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]
    elsif options[:comply].is_a? Hash
      options[:comply].each do |protocol, committees|
        [committees].flatten.each do |committee|
          @deliberations[goal].compliance(committee).include?(protocol) or raise ::Leap::NoSolutionError, :goal => committee, :deliberation => @deliberations[goal]
        end
      end
      Leap.log.decision "Success", goal
      @deliberations[goal]
    else
      Leap.log.decision "Success", goal
      @deliberations[goal]
    end
  end
end