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, context = nil) ⇒ Object



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

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

#add_actions(obj, context = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/docman/commands/command.rb', line 50

def add_actions(obj, context = nil)
  if obj.has_key? 'hooks' and obj['hooks'].has_key? @type
    obj['hooks'][@type].each_pair do |name, hooks|
      hooks = Marshal::load(Marshal.dump(hooks))
      unless context.nil?
        hooks.each do |hook|
          hook['context'] = context
        end
      end
      if @hooks[name].nil?
        @hooks[name] = hooks
      else
        @hooks[name].concat(hooks)
      end
    end
  end
end

#configObject



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

def config
  add_actions(self, @context)
  add_actions(@context, @context) if @context
end

#describe(type = 'short') ⇒ Object



120
121
122
# File 'lib/docman/commands/command.rb', line 120

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

#executeObject

This method is abstract.

Raises:

  • (NoMethodError)


98
99
100
# File 'lib/docman/commands/command.rb', line 98

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

#performObject



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

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

#prefixObject



124
125
126
127
128
129
# File 'lib/docman/commands/command.rb', line 124

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



131
132
133
134
135
136
137
# File 'lib/docman/commands/command.rb', line 131

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?
end

#run_actions(name) ⇒ Object



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

def run_actions(name)
  if @hooks.has_key? name
    @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) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/docman/commands/command.rb', line 86

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