class Docman::Command

Attributes

type[R]

Public Class Methods

create(params, context = nil, caller = nil) click to toggle source
# File lib/docman/commands/command.rb, line 19
def self.create(params, context = nil, caller = nil)
  params['type'] = params['type'].to_sym if params['type'].instance_of? String
  c = @@subclasses[params['type']]
  if c
    c.new(params, context, caller)
  else
    raise "Bad command type: #{params['type']}"
  end
end
new(params = nil, context = nil, caller = nil, type = 'command') click to toggle source
# File lib/docman/commands/command.rb, line 33
def initialize(params = nil, context = nil, caller = nil, type = 'command')
  unless params.nil?
    params.each_pair do |k, v|
      self[k] = v
    end
  end
  @context = context
  @caller = caller
  @type = type
  @log = self.has_key?('log') ? self['log'] : true
  @hooks = {}
end
register_command(name) click to toggle source
# File lib/docman/commands/command.rb, line 29
def self.register_command(name)
  @@subclasses[name] = self
end

Public Instance Methods

add_action(name, hook, context = nil) click to toggle source
# File lib/docman/commands/command.rb, line 70
def add_action(name, hook, context = nil)
  version = Docman::Application.instance.config.version
  unless hook['version'].nil? || hook['version'] != version
    hook['order'] = 0 unless hook['order']
    if @hooks.has_key? name
      @hooks[name] << hook
    else
      @hooks[name] = [hook]
    end
  end
end
add_actions(obj, context = nil) click to toggle source
# File lib/docman/commands/command.rb, line 51
def add_actions(obj, context = nil)
  if obj.has_key? 'hooks' and obj['hooks'].has_key? @type and not obj['hooks'][@type].nil?
    obj['hooks'][@type].each_pair do |name, hooks|
      hooks = Marshal::load(Marshal.dump(hooks))
      unless context.nil? or hooks.nil?
        hooks.each do |hook|
          hook['order'] = 0 unless hook['order']
          hook['context'] = context
        end
      end
      if @hooks[name].nil?
        @hooks[name] = hooks
      else
        @hooks[name].concat(hooks)
      end
    end
  end
end
config() click to toggle source
# File lib/docman/commands/command.rb, line 46
def config
  add_actions(self, @context)
  add_actions(@context, @context) if @context
end
describe(type = 'short') click to toggle source
# File lib/docman/commands/command.rb, line 127
def describe(type = 'short')
  "Command: #{properties_info}"
end
execute() click to toggle source

@abstract

# File lib/docman/commands/command.rb, line 105
def execute
  raise NoMethodError.new("Please define #execute for #{self.class.name}", '')
end
perform() click to toggle source
# File lib/docman/commands/command.rb, line 109
def perform
  config if self.respond_to? :config
  validate_command if self.respond_to? :validate_command
  run_with_hooks('execute')
  @execute_result
rescue CommandValidationError => e
  log "Command validation error: #{e.message}", 'error'
  return false
rescue NoChangesError => e
  log "No changes: #{e.message}", 'error'
  return false
rescue StandardError => e
  log e.message, 'error'
  raise
ensure
  @execute_result
end
prefix() click to toggle source
# File lib/docman/commands/command.rb, line 131
def prefix
  prefix = []
  prefix << @caller.prefix if not @caller.nil? and @caller.respond_to? :prefix
  prefix << self.class.name
  prefix.join(' - ')
end
replace_placeholder(value) click to toggle source
# File lib/docman/commands/command.rb, line 138
def replace_placeholder(value)
  value.gsub!('$ROOT$', @context['docroot_config'].root['full_build_path']) unless @context['docroot_config'].root['full_build_path'].nil?
  value.gsub!('$DOCROOT$', @context['docroot_config'].docroot_dir) unless @context['docroot_config'].docroot_dir.nil?
  value.gsub!('$PROJECT$', @context['full_build_path']) unless @context['full_build_path'].nil?
  value.gsub!('$INFO$', @context['full_path']) unless @context['full_path'].nil?
  value.gsub!('$ENVIRONMENT$', @context.environment_name) unless @context.environment_name.nil?
  value.gsub!('$DOCMAN_BIN$', Application::bin)
end
run_actions(name) click to toggle source
# File lib/docman/commands/command.rb, line 82
def run_actions(name)
  if @hooks.has_key?(name) and not @hooks[name].nil?
    @hooks[name].sort_by!{|a| a['order']}
    @hooks[name].each do |hook|
      next if hook.has_key?('run_on_success_only') and hook['run_on_success_only'] and not @execute_result
      context = hook.has_key?('context') ? hook['context'] : @context
      Docman::Command.create(hook, context, self).perform
    end
  end
end
run_with_hooks(method) click to toggle source
# File lib/docman/commands/command.rb, line 93
def run_with_hooks(method)
   with_logging(method) do
    run_actions("before_#{method}")
    run_hook "before_#{method}".to_sym
    result = self.send(method)
    @execute_result = result if method == 'execute'
    run_hook "after_#{method}".to_sym
    run_actions("after_#{method}")
  end
end