Class: Berta::Entities::Expiration

Inherits:
Object
  • Object
show all
Defined in:
lib/berta/entities/expiration.rb

Overview

Class for storing expiration data, also can be created from xml

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, time, action) ⇒ Expiration

Creates expiration class instance from given arguments.

Parameters:

  • id (String)

    Schelude action id

  • time (String)

    Schelude action execution time

  • action (String)

    Schelude action



33
34
35
36
37
# File 'lib/berta/entities/expiration.rb', line 33

def initialize(id, time, action)
  @id = id
  @time = time
  @action = action
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



5
6
7
# File 'lib/berta/entities/expiration.rb', line 5

def action
  @action
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/berta/entities/expiration.rb', line 5

def id
  @id
end

#timeObject (readonly)

Returns the value of attribute time.



5
6
7
# File 'lib/berta/entities/expiration.rb', line 5

def time
  @time
end

Class Method Details

.check_xml!(xml) ⇒ Object

Raises error if XML element is not in correct format.

Parameters:

  • xml (XMLElement)

    XML to check for all required values

Raises:



21
22
23
24
25
26
# File 'lib/berta/entities/expiration.rb', line 21

def self.check_xml!(xml)
  raise Berta::Errors::Entities::InvalidEntityXMLError, 'wrong enxpiration xml recieved' \
    unless %w[ID
              ACTION
              TIME].all? { |path| xml.has_elements? path }
end

.from_xml(xml) ⇒ Berta::Entities::Expiration

Creates Expiration class instance from XMLElement.

Parameters:

  • xml (XMLElement)

    XML from which to create instance

Returns:

Raises:



12
13
14
15
# File 'lib/berta/entities/expiration.rb', line 12

def self.from_xml(xml)
  check_xml!(xml)
  Berta::Entities::Expiration.new(xml['ID'], xml['TIME'], xml['ACTION'])
end

Instance Method Details

#==(other) ⇒ Object

TODO: doc



82
83
84
# File 'lib/berta/entities/expiration.rb', line 82

def ==(other)
  id == other.id && action == other.action && time == other.time
end

#default_action?Boolean

Determines if this expiration has default expiration action set. Default expiration action is action defined in settings file.

Returns:

  • (Boolean)

    Truthy if has default action else falsy



77
78
79
# File 'lib/berta/entities/expiration.rb', line 77

def default_action?
  action == Berta::Settings.expiration.action
end

#in_expiration_interval?Boolean

Determines if this schelude action is in expiration interval. That means its expiration is closer to Time.now that expiration offset.

Returns:

  • (Boolean)

    Truthy if in expiration interval else falsy



68
69
70
71
# File 'lib/berta/entities/expiration.rb', line 68

def in_expiration_interval?
  time_interval = time.to_i - Time.now.to_i
  time_interval <= Berta::Settings.expiration_offset && time_interval >= 0
end

#in_notification_interval?Boolean

Determines if this schelude action is in notification interval. That means its expiration is closer to Time.now than notification deadline.

Returns:

  • (Boolean)

    Truthy if in notification interval else falsy



58
59
60
61
# File 'lib/berta/entities/expiration.rb', line 58

def in_notification_interval?
  time_interval = time.to_i - Time.now.to_i
  time_interval <= Berta::Settings.notification_deadline && time_interval >= 0
end

#templateString

Generate schelude action template that can be used in VMS USER_TEMPLATE/SCHED_ACTION

Returns:

  • (String)

    Schelude action template



43
44
45
46
47
48
49
50
51
# File 'lib/berta/entities/expiration.rb', line 43

def template
  <<-VM_TEMPLATE
SCHED_ACTION = [
    ID     = "#{id}",
    ACTION = "#{action}",
    TIME   = "#{time}"
]
VM_TEMPLATE
end