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.



33
34
35
36
37
38
39
40
41
42
43
44
# 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

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
27
# 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

.register_command(name) ⇒ Object



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

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

Instance Method Details

#add_action(name, hook, context = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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

#configObject



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

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

#describe(type = 'short') ⇒ Object



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

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

#executeObject

This method is abstract.

Raises:

  • (NoMethodError)


105
106
107
# File 'lib/docman/commands/command.rb', line 105

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

#performObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 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

#prefixObject



131
132
133
134
135
136
# 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) ⇒ Object



138
139
140
141
142
143
144
145
# 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) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# 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