Class: Quickl::Command::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/quickl/command/builder.rb

Instance Method Summary collapse

Instance Method Details

#callback(&block) ⇒ Object

Installs a callback block to execute at install time



38
39
40
41
# File 'lib/quickl/command/builder.rb', line 38

def callback(&block)
  @callbacks ||= []
  @callbacks << block
end

#class_modules(*mods) ⇒ Object Also known as: class_module

Adds some command class modules



11
12
13
14
15
16
17
# File 'lib/quickl/command/builder.rb', line 11

def class_modules(*mods)
  @class_modules ||= [ 
    Command::ClassMethods,
    Command::Options::ClassMethods,
  ]
  @class_modules += mods
end

#command_parent=(p) ⇒ Object

Sets the parent of the command currently built



32
33
34
# File 'lib/quickl/command/builder.rb', line 32

def command_parent=(p)
  @parent = p
end

#document(file, line) ⇒ Object

Adds document tracking information



6
7
8
# File 'lib/quickl/command/builder.rb', line 6

def document(file, line)
  @file, @line = file, line
end

#instance_modules(*mods) ⇒ Object Also known as: instance_module

Adds some command instance modules



21
22
23
24
25
26
27
28
# File 'lib/quickl/command/builder.rb', line 21

def instance_modules(*mods)
  @instance_modules ||= [ 
    Command::InstanceMethods, 
    Command::Robustness,
    Command::Options::InstanceMethods
  ]
  @instance_modules += mods
end

#run(command) ⇒ Object

Installs on a command subclass



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/quickl/command/builder.rb', line 44

def run(command)
  # install class and instance methods
  class_modules.each{|mod|
    command.extend(mod)
  }
  instance_modules.each{|mod|
    command.instance_eval{ include mod } 
  }

  # install documentation
  if @file and @line
    command.doc_place = [@file, @line]
  end

  # install hierarchy
  parent = (defined?(@parent) && @parent) || RubyTools::parent_module(command)
  if parent && parent.ancestors.include?(Command)
    command.super_command = parent
    parent.subcommands << command
  end
  
  # execute callbacks
  Array(@callbacks).each do |blk|
    blk.call(command)
  end if defined?(@callbacks)

  command
end