Class: Leap::Decision

Inherits:
Object
  • Object
show all
Defined in:
lib/leap/decision.rb

Overview

Encapsulates a set of strategies to determine a potentially non-obvious attribute of a Leap subject.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(goal, options) ⇒ Decision

Creates a Leap::Decision object with a given goal.

Generally you won’t initialize a Leap::Decision directly; Leap::Subject#decide does it for you.

Parameters:

  • goal (Symbol)

    The ultimate goal of the decision–what are we trying to decide about the subject?

  • options (Hash)
  • [optional, (Hash)

    a customizable set of options



20
21
22
23
24
# File 'lib/leap/decision.rb', line 20

def initialize(goal, options)
  @goal = goal
  @signature_method = options[:with] || :_leap_implicit_attributes
  @committees = []
end

Instance Attribute Details

#committeesObject (readonly)

Returns the array of committees defined within the decision block.



11
12
13
# File 'lib/leap/decision.rb', line 11

def committees
  @committees
end

#goalObject (readonly)

The goal of the decision, as defined by the first parameter of Leap::Subject#decide.



5
6
7
# File 'lib/leap/decision.rb', line 5

def goal
  @goal
end

#signature_methodObject (readonly)

The method name used to retrieve a curated hash of the subject instance’s attributes for use during deliberation. Initially defined by the :with option on Leap::Subject#decide.



8
9
10
# File 'lib/leap/decision.rb', line 8

def signature_method
  @signature_method
end

Instance Method Details

#committee(name, options = {}, &blk) ⇒ Object

Define a committee within the decision.

Used within a Leap::Subject#decide block to define a new committee. See Leap::Committee for details.

Parameters:

  • name (Symbol)

    The name of the attribute that the committee will return.

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

    Any options you wish the committee to remember (for third-party use).



56
57
58
59
60
# File 'lib/leap/decision.rb', line 56

def committee(name, options = {}, &blk)
  committee = ::Leap::Committee.new(name, options)
  @committees << committee
  committee.instance_eval(&blk)
end

#make(characteristics, options, *considerations) ⇒ Object

Make the decision.

General you won’t call this directly, but rather use the dynamically-created method with this decision’s goal as its name on the subject instance.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/leap/decision.rb', line 35

def make(characteristics, options, *considerations)
  Leap.log.decision "Leaping to conclusion", goal
  Leap.log.decision "Initial characteristics: #{characteristics.inspect}", goal
  committees.reject { |c| characteristics.keys.include? c.name }.reverse.inject(Deliberation.new(characteristics)) do |deliberation, committee|
    Leap.instrument.committee committee.name do
      if report = committee.report(deliberation.characteristics, considerations, options)
        Leap.log.committee "Success", committee.name
        deliberation.reports.unshift report
        deliberation.characteristics[committee.name] = report.conclusion
      end
      Leap.log.decision "Updated characteristics: #{deliberation.characteristics.inspect}", goal
    end
    deliberation
  end
end

#mastered?Boolean

Answers whether or not the decision has a “master committee”–a committee with the same name as the decision’s goal.

Returns:

  • (Boolean)


27
28
29
# File 'lib/leap/decision.rb', line 27

def mastered?
  !!committees.find { |committee| committee.name == goal }
end