Class: Mongify::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/mongify/status.rb

Overview

This class is responsible for generating progress bars with status of mongify

Constant Summary collapse

NOTIFICATIONS =

List of known notifications

['copy_data', 'copy_embedded', 'copy_polymorphic', 'update_references', 'remove_pre_mongified', 'set_last_updated_at']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_bar(name, bar) ⇒ Object

Add a new bar to the list of progress bars



13
14
15
# File 'lib/mongify/status.rb', line 13

def add_bar(name, bar)
  self.bars[name] = bar
end

.barsObject

List of all the progress bars.



9
10
11
# File 'lib/mongify/status.rb', line 9

def bars
  @bars ||= {}
end

.registerObject

Registers the ActiveSupport::Notifications for Mongify



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mongify/status.rb', line 18

def register
  ActiveSupport::Notifications.subscribe(/^mongify\./) do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)

    action_name = event.name.split('.', 2)[1]

    if Status::NOTIFICATIONS.include?(action_name)
      case event.payload[:action]
      when 'add'
        self.add_bar(action_name, ProgressBar.new(event.payload[:name], event.payload[:size]))
      when 'inc'
        self.bars[action_name].try(:inc)
      when 'finish'
        self.bars[action_name].try(:finish)
      else
        UI.warn("Unknown Notification Action #{event.payload[:action]}")
      end
      #puts event.payload.inspect
    else
      UI.warn("Unknown Notification Event #{action_name}")
    end
  end

  # Unregisters from {ActiveSupport::Notifications}
  def unregister
    ActiveSupport::Notifications.unsubscribe(/^mongify\./)
  end

  # Publish an notification event
  # This will publish the event as an mongify.[name]
  # @param [String] name Name of the notification
  # @param [Hash] payload to be sent with the notification
  # @return [nil]
  def publish(name, payload={})
    payload.reverse_merge!(:name => name.humanize, :action => 'inc')
    ActiveSupport::Notifications.instrument("mongify.#{name}", payload)
    nil
  end
end

Instance Method Details

#publish(name, payload = {}) ⇒ nil

Publish an notification event This will publish the event as an mongify.

Parameters:

  • name (String)

    Name of the notification

  • payload (Hash) (defaults to: {})

    to be sent with the notification

Returns:

  • (nil)


51
52
53
54
55
# File 'lib/mongify/status.rb', line 51

def publish(name, payload={})
  payload.reverse_merge!(:name => name.humanize, :action => 'inc')
  ActiveSupport::Notifications.instrument("mongify.#{name}", payload)
  nil
end

#unregisterObject

Unregisters from ActiveSupport::Notifications



42
43
44
# File 'lib/mongify/status.rb', line 42

def unregister
  ActiveSupport::Notifications.unsubscribe(/^mongify\./)
end