Module: Executable::Domain

Defined in:
lib/executable/domain.rb

Instance Method Summary collapse

Instance Method Details

#alias_accessor(name, origin) ⇒ Object



62
63
64
65
# File 'lib/executable/domain.rb', line 62

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

#alias_switch(name, origin) ⇒ Object



54
55
56
57
# File 'lib/executable/domain.rb', line 54

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


42
43
44
45
46
47
48
49
# File 'lib/executable/domain.rb', line 42

def attr_switch(name)
  attr_writer name
  module_eval %{
    def #{name}?
      @#{name}
    end
  }
end

#calculate_command_name(ancestor) ⇒ Object (private)



17
18
19
20
21
22
23
24
# File 'lib/executable/domain.rb', line 17

def calculate_command_name(ancestor)
  if ancestor.methods(false).include?(:command_name)
    command_name.to_s
  else
    cname = ancestor.name.sub(/\#\<.*?\>\:\:/,'').split('::').last.downcase
    cname.chomp('command').chomp('cli')
  end
end

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

Execute the command.

Parameters:

  • argv (Array) (defaults to: ARGV)

    command-line arguments



98
99
100
101
102
# File 'lib/executable/domain.rb', line 98

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.



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

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

#inspectObject

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



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

def inspect
  name
end

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

Returns The executable and call arguments.

Returns:

  • (Array<Executable,Array>)

    The executable and call arguments.



112
113
114
# File 'lib/executable/domain.rb', line 112

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

#parserObject

The parser for this command.



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

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

#subcommandsHash

Index of subcommands.

Returns:

  • (Hash)

    name mapped to subcommnd class



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/executable/domain.rb', line 128

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`.



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

def to_s
  cli.to_s
end

#usage_nameObject



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