Module: Executable::Domain
- Defined in:
- lib/executable/domain.rb
Instance Method Summary collapse
- #alias_accessor(name, origin) ⇒ Object
- #alias_switch(name, origin) ⇒ Object
-
#attr_switch(name) ⇒ Object
Helper method for creating switch attributes.
- #calculate_command_name(ancestor) ⇒ Object private
-
#execute(argv = ARGV) ⇒ Object
(also: #run)
Execute the command.
-
#help ⇒ Object
(also: #cli)
Interface with cooresponding cli/help object.
-
#inspect ⇒ Object
Inspection method.
-
#parse(argv) ⇒ Array<Executable,Array>
The executable and call arguments.
-
#parser ⇒ Object
The parser for this command.
-
#subcommands ⇒ Hash
Index of subcommands.
-
#to_s ⇒ Object
Returns ‘help.to_s`.
- #usage_name ⇒ Object
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.
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 |
#help ⇒ Object 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 |
#inspect ⇒ Object
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.
112 113 114 |
# File 'lib/executable/domain.rb', line 112 def parse(argv) parser.parse(argv) end |
#parser ⇒ Object
The parser for this command.
119 120 121 |
# File 'lib/executable/domain.rb', line 119 def parser @parser ||= Parser.new(self) end |
#subcommands ⇒ Hash
Index of subcommands.
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_s ⇒ Object
Returns ‘help.to_s`.
77 78 79 |
# File 'lib/executable/domain.rb', line 77 def to_s cli.to_s end |
#usage_name ⇒ Object
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 |