Module: Executable::Domain

Defined in:
lib/executable/domain.rb

Instance Method Summary collapse

Instance Method Details

#alias_accessor(name, origin) ⇒ Object

Alias an accessor.



68
69
70
71
# File 'lib/executable/domain.rb', line 68

def alias_accessor(name, origin)
  alias_method "#{name}=", "#{origin}="
  alias_method "#{name}",  "#{origin}"
end

#alias_switch(name, origin) ⇒ Object

Alias a switch.



60
61
62
63
# File 'lib/executable/domain.rb', line 60

def alias_switch(name, origin)
  alias_method "#{name}=", "#{origin}="
  alias_method "#{name}?", "#{origin}?"
end

#attr_switch(name) ⇒ Object

Helper method for creating switch attributes.

This is equivalent to:

def name=(val)
  @name = val
end

def name?
  @name
end

TODO: Currently there is an unfortunate issue with using this helper method. If does not correctly record the location the method is called, so default help message is wrong.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/executable/domain.rb', line 45

def attr_switch(name)
  file, line = *caller[0].split(':')[0..1]
  module_eval(<<-END, file, line.to_i)
    def #{name}=(value)
      @#{name}=(value)
    end
    def #{name}?
      @#{name}
    end
  END
end

#execute(argv = ARGV) ⇒ Object Also known as: run

Execute the command.

Parameters:

  • argv (Array) (defaults to: ARGV)

    command-line arguments



105
106
107
108
109
# File 'lib/executable/domain.rb', line 105

def execute(argv=ARGV)
  cli, args = parser.parse(argv)
  cli.call(*args)
  return cli
end

#helpObject Also known as: cli

Interface with cooresponding cli/help object.



91
92
93
# File 'lib/executable/domain.rb', line 91

def help
  @help ||= Help.new(self)
end

#inspectObject

Inspection method. This must be redefined b/c #to_s is overridden.



76
77
78
# File 'lib/executable/domain.rb', line 76

def inspect
  name
end

#parse(argv) ⇒ Array<Executable,Array>

Returns The executable and call arguments.

Returns:

  • (Array<Executable,Array>)

    The executable and call arguments.



119
120
121
# File 'lib/executable/domain.rb', line 119

def parse(argv)
  parser.parse(argv)
end

#parserObject

The parser for this command.



126
127
128
# File 'lib/executable/domain.rb', line 126

def parser
  @parser ||= Parser.new(self)
end

#subcommandsHash

Index of subcommands.

Returns:

  • (Hash)

    name mapped to subcommnd class



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/executable/domain.rb', line 135

def subcommands
  @subcommands ||= (
    consts = constants - superclass.constants
    consts.inject({}) do |h, c|
      c = const_get(c)
      if Class === c && Executable > c
        n = c.name.split('::').last
        n = n.chomp('Command').chomp('CLI')
        n = n.downcase
        h[n] = c
      end
      h
    end
  )
end

#to_sObject

Returns help.to_s. If you want to provide your own help text you can override this method in your command subclass.



84
85
86
# File 'lib/executable/domain.rb', line 84

def to_s
  cli.to_s
end

#usage_nameObject

TODO: Should this be in Help class?



7
8
9
10
11
12
13
14
# File 'lib/executable/domain.rb', line 7

def usage_name
  list = []
  ancestors.each do |ancestor|
    break if Executable == ancestor
    list.unshift calculate_command_name(ancestor).to_s.strip
  end
  list.reject{|n| n.empty?}.join(" ")
end