Class: ToiletTracker::Core::Message

Inherits:
Data
  • Object
show all
Defined in:
lib/toilet_tracker/core/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timestamp:, sender:, content:) ⇒ Message

Returns a new instance of Message.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/toilet_tracker/core/message.rb', line 9

def initialize(timestamp:, sender:, content:)
  parsed_timestamp = case timestamp
  when Time, ActiveSupport::TimeWithZone
                       timestamp
  when String
                       Time.parse(timestamp)
  else
                       raise ArgumentError, "Invalid timestamp type: #{timestamp.class}"
  end

  super(
    timestamp: parsed_timestamp,
    sender: sender.to_s.strip,
    content: content.to_s
  )
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content

Returns:

  • (Object)

    the current value of content



8
9
10
# File 'lib/toilet_tracker/core/message.rb', line 8

def content
  @content
end

#senderObject (readonly)

Returns the value of attribute sender

Returns:

  • (Object)

    the current value of sender



8
9
10
# File 'lib/toilet_tracker/core/message.rb', line 8

def sender
  @sender
end

#timestampObject (readonly)

Returns the value of attribute timestamp

Returns:

  • (Object)

    the current value of timestamp



8
9
10
# File 'lib/toilet_tracker/core/message.rb', line 8

def timestamp
  @timestamp
end

Instance Method Details

#age_in_daysObject



32
33
34
# File 'lib/toilet_tracker/core/message.rb', line 32

def age_in_days
  (Time.current - timestamp) / 1.day
end

#recent?(days: 30) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/toilet_tracker/core/message.rb', line 36

def recent?(days: 30)
  age_in_days <= days
end

#valid?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/toilet_tracker/core/message.rb', line 26

def valid?
  timestamp.is_a?(Time) &&
    !sender.empty? &&
    !content.empty?
end