Class: Leap::Quorum

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

Overview

A basic building block of a Leap decision.

A quorum encapsulates a specific methodology for drawing a conclusion based on a set of input information.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options, blk) { ... } ⇒ Object

Defines a quorum within this committee.

A quorum encapsulate a specific methodology for drawing the committe’s conclusion.

Parameters:

  • name (String)

    A descriptive name for this methodology.

  • options (optional, Hash)

Yields:

  • The quorum’s methodology, as a Ruby closure. You may define your block with any number of block vars. If zero, the methodology requires no outside information (useful for “default” quorums). If one, Leap will provide the characteristics hash, accumulated over the course of the deliberation, that represents its total knowledge of the subject at the time. (Note that Leap will actually provide only a subset of the characteristics hash–namely, only attributes that are specifically requested by the :needs and :wants options below.) If more than one, additional block vars will be filled with as many considerations (given during Leap::Decision#make) as is necessary.



23
24
25
26
27
28
29
# File 'lib/leap/quorum.rb', line 23

def initialize(name, options, blk)
  @name = name
  @requirements = Array.wrap options[:needs]
  @supplements = Array.wrap options[:appreciates]
  @compliance = Array.wrap options[:complies]
  @process = blk
end

Instance Attribute Details

#complianceObject (readonly)

Protocols with which this quorum complies.



20
21
22
# File 'lib/leap/quorum.rb', line 20

def compliance
  @compliance
end

#nameObject (readonly)

The name of the quorum



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

def name
  @name
end

#processObject (readonly)

The quorum’s methodology, as a Ruby closure.



17
18
19
# File 'lib/leap/quorum.rb', line 17

def process
  @process
end

#requirementsObject (readonly)

Quorum attribute requirements, as defined by the :needs option



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

def requirements
  @requirements
end

#supplementsObject (readonly)

Useful attributes, as defined by the :wants option



14
15
16
# File 'lib/leap/quorum.rb', line 14

def supplements
  @supplements
end

Instance Method Details

#acknowledge(characteristics, considerations) ⇒ Object

Perform the quorum’s methodology using the given characteristics.

Parameters:

  • characteristics (Hash)

Returns:

  • The methodology’s result



70
71
72
73
74
75
# File 'lib/leap/quorum.rb', line 70

def acknowledge(characteristics, considerations)
  Leap.instrument.quorum name do
    considerations.unshift characteristics
    process.call(*considerations[0...process.arity])
  end
end

#characteristicsObject

All characteristics either needed or wanted by the quorum.



78
79
80
# File 'lib/leap/quorum.rb', line 78

def characteristics
  requirements + supplements
end

#complies_with?(guidelines) ⇒ TrueClass, NilClass

Does the quorum comply with the given set of protocols?

Parameters:

  • guidelines (Array, Hash, Object)

    The list of protocols for which we’re checking compliance.

Returns:

  • (TrueClass, NilClass)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/leap/quorum.rb', line 41

def complies_with?(guidelines)
  case guidelines
  when Hash
    inversion = guidelines.inject({}) do |memo, pair|
      protocol, committees = pair
      Array.wrap(committees).each do |committee|
        if memo[committee]
          memo[committee] << protocol
        else
          memo[committee] = [protocol]
        end
      end
      memo
    end
    inversion.values.any? do |committee_guidelines|
      (committee_guidelines - compliance).empty?
    end
  when Array
    (guidelines - compliance).empty?
  when NilClass
    true
  else
    compliance.include? guidelines
  end
end

#satisfied_by?(characteristics) ⇒ TrueClass, NilClass

Do the provided characteristics satisfy the quorum’s requirements?

Parameters:

  • characteristics (Hash)

Returns:

  • (TrueClass, NilClass)


34
35
36
# File 'lib/leap/quorum.rb', line 34

def satisfied_by?(characteristics)
  (requirements - characteristics.keys).empty?
end