Class: Postmodern::Command

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

Direct Known Subclasses

Backup::Backup, Dummy, Vacuum::Vacuum, WAL::Archive

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Command

Returns a new instance of Command.



35
36
37
38
39
40
# File 'lib/postmodern/command.rb', line 35

def initialize(args)
  @options = self.class.default_options.dup

  parse_args(args)
  validate!
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.default_option(key, value) ⇒ Object



24
25
26
# File 'lib/postmodern/command.rb', line 24

def default_option(key, value)
  default_options[key] = value
end

.default_optionsObject



20
21
22
# File 'lib/postmodern/command.rb', line 20

def default_options
  @default_options ||= {}
end

.inherited(subclass) ⇒ Object



6
7
8
9
# File 'lib/postmodern/command.rb', line 6

def inherited(subclass)
  subclass.instance_variable_set(:@required_options, @required_options)
  subclass.instance_variable_set(:@default_options, @default_options)
end

.required_option(*options) ⇒ Object



15
16
17
18
# File 'lib/postmodern/command.rb', line 15

def required_option(*options)
  required_options.concat(options)
  required_options.uniq!
end

.required_optionsObject



11
12
13
# File 'lib/postmodern/command.rb', line 11

def required_options
  @required_options ||= []
end

Instance Method Details

#missing_paramsObject



52
53
54
# File 'lib/postmodern/command.rb', line 52

def missing_params
  self.class.required_options - self.options.keys
end

#parse_args(args) ⇒ Object



56
57
58
# File 'lib/postmodern/command.rb', line 56

def parse_args(args)
  parser.parse!(args)
end

#parserObject



29
30
31
# File 'lib/postmodern/command.rb', line 29

def parser
  raise "Command needs to define an OptionParser"
end

#runObject



42
43
# File 'lib/postmodern/command.rb', line 42

def run
end

#usage!Object



60
61
62
63
# File 'lib/postmodern/command.rb', line 60

def usage!
  puts parser.to_s
  exit 1
end

#validate!Object



45
46
47
48
49
50
# File 'lib/postmodern/command.rb', line 45

def validate!
  if missing_params.any?
    puts "Missing #{missing_params.join(', ')}"
    usage!
  end
end