Class: CLI::Mastermind::Loader::PlanfileLoader::DSL

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cli/mastermind/loader/planfile_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil, &block) ⇒ DSL

Loads and evaluates a local file or a given block.

If given both, the block takes priority.

Examples:

DSL.new(‘path/to/file.plan’)

DSL.new { …methods go here… }

Parameters:

  • filename (String, nil) (defaults to: nil)

    the name of the file that contains this plan

  • block (#call, nil)

    a block to evaluate



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cli/mastermind/loader/planfile_loader.rb', line 31

def initialize(filename=nil, &block)
  @plans = []
  @filename = filename

  if block_given?
    instance_eval(&block)
  elsif File.exists? filename
    instance_eval(File.read(filename), filename, 0)
  else
    raise InvalidPlanfileError, 'Must provide valid path to a planfile or a block'
  end
end

Instance Attribute Details

#plansArray<Plan> (readonly)

Returns the plans defined by the loaded file or block.

Returns:

  • (Array<Plan>)

    the plans defined by the loaded file or block



21
22
23
# File 'lib/cli/mastermind/loader/planfile_loader.rb', line 21

def plans
  @plans
end

Instance Method Details

#description(text) ⇒ Object Also known as: desc

Parameters:

  • text (String)

    the description of the next plan



57
58
59
# File 'lib/cli/mastermind/loader/planfile_loader.rb', line 57

def description(text)
  @description = text
end

#plan(name, plan_class = ExecutablePlan, &block) ⇒ void Also known as: task

This method returns an undefined value.

Defines an executable plan

Parameters:

  • name (String)

    the name of the plan

  • plan_class (Plan) (defaults to: ExecutablePlan)

    the plan class

  • block (#call)

    passed into the newly created plan



68
69
70
71
# File 'lib/cli/mastermind/loader/planfile_loader.rb', line 68

def plan(name, plan_class = ExecutablePlan, &block)
  @plans << plan_class.new(name, @description, @filename, &block)
  @description = nil
end

#plot(name, &block) ⇒ Object Also known as: namespace

Describes a ParentPlan

Parameters:

  • name (String)

    the name of the plan

  • block (#call)

    passed to a new DSL object to define more plans



48
49
50
51
52
53
# File 'lib/cli/mastermind/loader/planfile_loader.rb', line 48

def plot(name, &block)
  plan = ParentPlan.new name, @description, @filename
  @description = nil
  @plans << plan
  plan.add_children DSL.new(@filename, &block).plans
end

#set_alias(alias_to) ⇒ Object

Sets an alias on the previously created plan

Parameters:

  • alias_to (String)

    the alias to add



77
78
79
# File 'lib/cli/mastermind/loader/planfile_loader.rb', line 77

def set_alias(alias_to)
  @plans.last.add_alias(alias_to)
end