Class: Docman::Command

Inherits:
Hash
  • Object
show all
Includes:
Logging, Hooks
Defined in:
lib/docman/commands/command.rb

Constant Summary collapse

@@subclasses =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log, logger, #logger, #properties_info, #with_logging

Constructor Details

#initialize(params = nil, context = nil, caller = nil, type = 'command') ⇒ Command

Returns a new instance of Command.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/docman/commands/command.rb', line 32

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

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/docman/commands/command.rb', line 13

def type
  @type
end

Class Method Details

.create(params, context = nil, caller = nil) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/docman/commands/command.rb', line 19

def self.create(params, context = nil, caller = nil)
  c = @@subclasses[params['type']]
  if c
    c.new(params, context, caller)
  else
    raise "Bad command type: #{params['type']}"
  end
end

.register_command(name) ⇒ Object



28
29
30
# File 'lib/docman/commands/command.rb', line 28

def self.register_command(name)
  @@subclasses[name] = self
end

Instance Method Details

#add_action(name, hook) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/docman/commands/command.rb', line 62

def add_action(name, hook)
  if @hooks.has_key? name
    @hooks[name] << {'type' => hook}
  else
    @hooks[name] = [hook]
  end
end

#add_actions(obj) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/docman/commands/command.rb', line 50

def add_actions(obj)
  if obj.has_key? 'hooks' and obj['hooks'].has_key? @type
    obj['hooks'][@type].each_pair do |name, hook|
      if @hooks[name].nil?
        @hooks[name] = hook
      else
        @hooks[name].concat(hook)
      end
    end
  end
end

#configObject



45
46
47
48
# File 'lib/docman/commands/command.rb', line 45

def config
  add_actions self
  add_actions @context unless @context.nil?
end

#describe(type = 'short') ⇒ Object



117
118
119
# File 'lib/docman/commands/command.rb', line 117

def describe(type = 'short')
  "Command: #{properties_info}"
end

#executeObject

This method is abstract.

Raises:

  • (NoMethodError)


92
93
94
# File 'lib/docman/commands/command.rb', line 92

def execute
  raise NoMethodError.new("Please define #execute for #{self.class.name}", '')
end

#performObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/docman/commands/command.rb', line 100

def perform
  config if self.respond_to? :config
  validate_command if self.respond_to? :validate_command
  @execute_result = run_with_hooks('execute')
rescue CommandValidationError => e
  logger.error "Command validation error: #{e.message}"
  return false
rescue NoChangesError => e
  logger.info "No changes: #{e.message}"
  return false
rescue StandardError => e
  logger.error e.message
  raise
ensure
  @execute_result
end

#prefixObject



121
122
123
124
125
126
# File 'lib/docman/commands/command.rb', line 121

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) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/docman/commands/command.rb', line 128

def replace_placeholder(value)
  value.gsub! '$ROOT$', @context['docroot_config'].root['full_build_path']
  value.gsub! '$DOCROOT$', @context['docroot_config'].docroot_dir
  value.gsub! '$PROJECT$', @context['full_build_path']
  value.gsub! '$INFO$', @context['full_path']
  value.gsub! '$ENVIRONMENT$', @context['docroot_config'].deploy_target['environment']
end

#run_actions(name) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/docman/commands/command.rb', line 70

def run_actions(name)
  if @hooks.has_key? name
    @hooks[name].each do |hook|
      Docman::Command.create(hook, @context, self).perform
    end
  end
end

#run_with_hooks(method) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/docman/commands/command.rb', line 78

def run_with_hooks(method)
   with_logging(method) do
    run_hook "before_#{method}".to_sym
    result = self.send(method)
    @execute_result = result if method == 'execute'
    run_hook "after_#{method}".to_sym
  end
end