Class: Rufio::NotificationManager

Inherits:
Object
  • Object
show all
Defined in:
lib/rufio/notification_manager.rb

Overview

Noice風の通知を管理するクラス画面右上に最大3個までの通知を表示

Constant Summary collapse

MAX_NOTIFICATIONS =
3
DEFAULT_DISPLAY_DURATION =

3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNotificationManager

Returns a new instance of NotificationManager.



12
13
14
# File 'lib/rufio/notification_manager.rb', line 12

def initialize
  @notifications = []
end

Instance Attribute Details

#notificationsObject (readonly)

Returns the value of attribute notifications.



10
11
12
# File 'lib/rufio/notification_manager.rb', line 10

def notifications
  @notifications
end

Instance Method Details

#add(name, type, duration:, exit_code: nil, display_duration: DEFAULT_DISPLAY_DURATION) ⇒ Object

通知を追加

Parameters:

  • name (String)

    タスク名

  • type (Symbol)

    :success または :error

  • duration (Float)

    タスクの実行時間

  • exit_code (Integer, nil) (defaults to: nil)

    終了コード(エラー時のみ)

  • display_duration (Integer) (defaults to: DEFAULT_DISPLAY_DURATION)

    通知の表示時間(秒)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rufio/notification_manager.rb', line 22

def add(name, type, duration:, exit_code: nil, display_duration: DEFAULT_DISPLAY_DURATION)
  notification = {
    name: name,
    type: type,
    duration: duration,
    exit_code: exit_code,
    created_at: Time.now,
    display_duration: display_duration,
    border_color: type == :success ? :green : :red,
    status_text: build_status_text(type, duration)
  }

  @notifications << notification

  # 最大3個を超えた場合、最も古い通知を削除
  @notifications.shift if @notifications.size > MAX_NOTIFICATIONS
end

#clearObject

全ての通知をクリア



55
56
57
# File 'lib/rufio/notification_manager.rb', line 55

def clear
  @notifications.clear
end

#countInteger

通知の数

Returns:

  • (Integer)


50
51
52
# File 'lib/rufio/notification_manager.rb', line 50

def count
  @notifications.size
end

#expire_old_notificationsObject

期限切れの通知を削除



41
42
43
44
45
46
# File 'lib/rufio/notification_manager.rb', line 41

def expire_old_notifications
  now = Time.now
  @notifications.reject! do |notification|
    now - notification[:created_at] > notification[:display_duration]
  end
end