Class: Command::CommandSetup

Inherits:
Object
  • Object
show all
Defined in:
lib/command-set/command.rb

Overview

An abstraction of the lifecycle of a command. Allows invocations to be postponed temporarily, or for the command to be instantiated and still passed around. Client code should almost never need to see this class, so the methods aren’t individually documented.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd = [], args = {}) ⇒ CommandSetup

Returns a new instance of CommandSetup.



40
41
42
43
44
45
# File 'lib/command-set/command.rb', line 40

def initialize(cmd = [], args = {})
  @task_id = nil
  @args_hash = args
  @terms = []
  @command = cmd
end

Instance Attribute Details

#args_hashObject

Returns the value of attribute args_hash.



47
48
49
# File 'lib/command-set/command.rb', line 47

def args_hash
  @args_hash
end

#commandObject

Returns the value of attribute command.



47
48
49
# File 'lib/command-set/command.rb', line 47

def command
  @command
end

#task_idObject

Returns the value of attribute task_id.



47
48
49
# File 'lib/command-set/command.rb', line 47

def task_id
  @task_id
end

#termsObject

Returns the value of attribute terms.



47
48
49
# File 'lib/command-set/command.rb', line 47

def terms
  @terms
end

Instance Method Details

#assign_terms(cmd) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/command-set/command.rb', line 71

def assign_terms(cmd)
  terms = @terms.dup
  cmd.each_consumer do |consumer|
    terms = consumer[terms]
  end

  cmd.consume_hash(@args_hash)
end

#command_instance(command_set, subject) ⇒ Object



80
81
82
83
84
85
# File 'lib/command-set/command.rb', line 80

def command_instance(command_set, subject)
  command_class = resolve_command_class(command_set)
  command = command_class.new(subject, task_id)
  assign_terms(command)
  return command
end

#resolve_command_class(command_set) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/command-set/command.rb', line 49

def resolve_command_class(command_set)
  if Class === @command && Command > @command
    return @command
  end

  command_path = @command
  @command,terms = command_set.find_command(*command_path)

  if CommandSet === @command
    if (cmd = @command.get_root).nil?
      raise CommandException, "Incomplete command #{command_path.join(" ")}"
    else
      terms = [@command]
      @command = cmd
    end
  end

  @terms = terms

  return @command
end