Class: CLI::Mastermind::ParentPlan

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Plan, Enumerable
Defined in:
lib/cli/mastermind/parent_plan.rb

Overview

Plan implementation designed to hold other plans forming the intermediate nodes on the tree of loaded plans.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Plan

#add_alias, #call, included

Constructor Details

#initialize(name, description = nil, filename = nil, &block) ⇒ ParentPlan

Returns a new instance of ParentPlan.



16
17
18
19
20
# File 'lib/cli/mastermind/parent_plan.rb', line 16

def initialize(name, description=nil, filename=nil, &block)
  super

  @children = {}
end

Instance Attribute Details

#childrenObject (readonly)

Used in the interactive plan selector to display child plans



12
13
14
# File 'lib/cli/mastermind/parent_plan.rb', line 12

def children
  @children
end

Instance Method Details

#add_children(plans) ⇒ Void

Adds new children to this plan

Parameters:

  • plans (Array<Plan>)

    the plans to add

Returns:

  • (Void)

Raises:



43
44
45
46
# File 'lib/cli/mastermind/parent_plan.rb', line 43

def add_children(plans)
  raise InvalidPlanError, 'Cannot add child plans to a plan with an action' unless @block.nil?
  plans.each(&method(:incorporate_plan))
end

#get_child(name) ⇒ Plan? Also known as: [], dig

Get the child plan with the specified name.

This method also checks plan aliases, so the given name can also be a plan’s alias.

Parameters:

  • name (String)

    the name of the child plan to retrieve

Returns:

  • (Plan, nil)

    the child plan, if it exists



29
30
31
32
# File 'lib/cli/mastermind/parent_plan.rb', line 29

def get_child(name)
  return @children[name] if @children.has_key? name
  @children.each_value.find { |child| child.aliases.include? name }
end

#has_children?Boolean

Returns if this plan has any children.

Returns:

  • (Boolean)

    if this plan has any children



49
50
51
# File 'lib/cli/mastermind/parent_plan.rb', line 49

def has_children?
  @children.any?
end