Module: DbAgile::Command::ClassMethods

Included in:
DbAgile::Command
Defined in:
lib/dbagile/command/class_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionObject (readonly)

Command description



12
13
14
# File 'lib/dbagile/command/class_methods.rb', line 12

def description
  @description
end

#summaryObject (readonly)

The command summary



6
7
8
# File 'lib/dbagile/command/class_methods.rb', line 6

def summary
  @summary
end

#usageObject (readonly)

The command banner



9
10
11
# File 'lib/dbagile/command/class_methods.rb', line 9

def usage
  @usage
end

Instance Method Details

#build_command_options(options) ⇒ Object

Builds command options



90
91
92
93
94
95
96
97
98
99
# File 'lib/dbagile/command/class_methods.rb', line 90

def build_command_options(options)
  case options
    when Array
      options
    when String
      options.split
    else
      raise ArgumentError, "Invalid options #{options}"
  end
end

#build_me(command_class, file) ⇒ Object

Helper to generate command classes



15
16
17
18
19
20
21
22
23
# File 'lib/dbagile/command/class_methods.rb', line 15

def build_me(command_class, file)
  rdoc = DbAgile::RubyTools::rdoc_file_paragraphs(file)
  summary, usage, description = rdoc.shift, rdoc.shift, rdoc.join("\n")
  command_class.instance_eval{
    @summary     = summary
    @usage       = usage.gsub('#{command_name}', command_class.command_name)
    @description = description.gsub('#{command_name}', command_class.command_name)
  }
end

#categoryObject

Returns command category



56
57
58
59
60
61
62
63
# File 'lib/dbagile/command/class_methods.rb', line 56

def category
  parent = DbAgile::RubyTools::parent_module(self)
  if parent == DbAgile::Command
    :dba
  else
    DbAgile::RubyTools::unqualified_class_name(parent).to_s.downcase.to_sym
  end
end

#command_for(name_symbol_or_class, env) ⇒ Object

Returns a command instance for a given name and environment, returns nil if it cannot be found



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dbagile/command/class_methods.rb', line 77

def command_for(name_symbol_or_class, env)
  subclass = case name_symbol_or_class
    when String
      subclasses.find{|subclass| command_name_of(subclass) == name_symbol_or_class}
    when Symbol
      subclasses.find{|subclass| ruby_method_for(subclass) == name_symbol_or_class}
    when Class
      name_symbol_or_class
  end
  subclass.nil? ? nil : subclass.new(env)
end

#command_nameObject

Returns command name



66
67
68
# File 'lib/dbagile/command/class_methods.rb', line 66

def command_name
  command_name_of(self)
end

#command_name_of(clazz) ⇒ Object

Returns the command name of a given class



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dbagile/command/class_methods.rb', line 43

def command_name_of(clazz)
  name = DbAgile::RubyTools::unqualified_class_name(clazz)
  name = name.gsub(/[A-Z]/){|x| "-#{x.downcase}"}[1..-1]
  parent_module = DbAgile::RubyTools::parent_module(clazz)
  if parent_module == DbAgile::Command
    name
  else
    parent_name = DbAgile::RubyTools::unqualified_class_name(parent_module)
    "#{parent_name.downcase}:#{name}"
  end
end

#each_subclass(&block) ⇒ Object

Yields the block with each subclass in turn



38
39
40
# File 'lib/dbagile/command/class_methods.rb', line 38

def each_subclass(&block)
  subclasses.each(&block)
end

#inherited(subclass) ⇒ Object

Tracks subclasses for maintaining subcommand list



26
27
28
29
30
# File 'lib/dbagile/command/class_methods.rb', line 26

def inherited(subclass) 
  super
  @subclasses ||= [] 
  @subclasses << subclass 
end

#ruby_method_for(clazz) ⇒ Object

Returns the ruby name of a given class



71
72
73
# File 'lib/dbagile/command/class_methods.rb', line 71

def ruby_method_for(clazz)
  command_name_of(clazz).gsub(/[:\-]/, '_').to_sym
end

#subclassesObject

Returns the array of known command sub-classes



33
34
35
# File 'lib/dbagile/command/class_methods.rb', line 33

def subclasses 
  @subclasses 
end