Module: Timet::Utils

Defined in:
lib/timet/utils.rb

Overview

The Utils module provides a collection of general utility methods used across the Timet gem.

Class Method Summary collapse

Class Method Details

.add_events(items) ⇒ Icalendar::Calendar

Creates an iCalendar object and adds events to it.

Parameters:

  • items (Array)

    the items containing event details

Returns:

  • (Icalendar::Calendar)

    the populated iCalendar object



120
121
122
123
124
125
126
127
128
# File 'lib/timet/utils.rb', line 120

def self.add_events(items)
  cal = Icalendar::Calendar.new
  items.each do |item|
    event = create_event(item)
    cal.add_event(event)
  end
  cal.publish
  cal
end

.add_hashes(base_hash, additional_hash) ⇒ Hash

Merges two hashes, summing the numeric values of corresponding keys.

Examples:

base_hash = { 'key1' => [10, 'tag1'], 'key2' => [20, 'tag2'] }
additional_hash = { 'key1' => [5, 'tag1'], 'key3' => [15, 'tag3'] }
Utils.add_hashes(base_hash, additional_hash)
#=> { 'key1' => [15, 'tag1'], 'key2' => [20, 'tag2'], 'key3' => [15, 'tag3'] }

Parameters:

  • base_hash (Hash)

    The base hash to which the additional hash will be merged.

  • additional_hash (Hash)

    The additional hash whose values will be added to the base hash.

Returns:

  • (Hash)

    A new hash with the summed values.



19
20
21
22
23
24
# File 'lib/timet/utils.rb', line 19

def self.add_hashes(base_hash, additional_hash)
  base_hash.merge(additional_hash) do |_key, old_value, new_value|
    summed_number = old_value[0] + new_value[0]
    [summed_number, old_value[1]]
  end
end

.assign_event_attributes(event, item) ⇒ Object

Assigns attributes to an iCalendar event.

Parameters:

  • event (Icalendar::Event)

    the event object

  • item (Array)

    the item containing event details



95
96
97
98
99
100
101
102
103
104
# File 'lib/timet/utils.rb', line 95

def self.assign_event_attributes(event, item)
  dtstart = convert_to_datetime(item[1])
  dtend = convert_to_datetime(item[2] || TimeHelper.current_timestamp)

  event.dtstart     = dtstart
  event.dtend       = dtend
  event.summary     = item[3]
  event.description = item[4]
  event.ip_class    = 'PRIVATE'
end

.convert_to_datetime(timestamp) ⇒ DateTime

Converts a timestamp to a DateTime object.

Parameters:

  • timestamp (Integer)

    the timestamp to convert

Returns:

  • (DateTime)

    the converted DateTime object



30
31
32
# File 'lib/timet/utils.rb', line 30

def self.convert_to_datetime(timestamp)
  Time.at(timestamp).to_datetime
end

.create_event(item) ⇒ Icalendar::Event

Creates an iCalendar event from the given item.

Parameters:

  • item (Array)

    the item containing event details

Returns:

  • (Icalendar::Event)

    the created event



110
111
112
113
114
# File 'lib/timet/utils.rb', line 110

def self.create_event(item)
  event = Icalendar::Event.new
  assign_event_attributes(event, item)
  event
end

.date_rangesHash

Note:

The method returns a hash with predefined date ranges for ‘today’, ‘yesterday’, ‘week’, and ‘month’.

Provides predefined date ranges for filtering.

Examples:

Get the predefined date ranges

Utils.date_ranges

Returns:

  • (Hash)

    A hash containing predefined date ranges.



42
43
44
45
46
47
48
49
50
51
# File 'lib/timet/utils.rb', line 42

def self.date_ranges
  today = Date.today
  tomorrow = today + 1
  {
    'today' => [today, nil],
    'yesterday' => [today - 1, nil],
    'week' => [today - 7, tomorrow],
    'month' => [today - 30, tomorrow]
  }
end

.format_item(item) ⇒ Array

Note:

The method formats the item’s ID, start time, end time, tag, and notes.

Formats an item for CSV export.

Examples:

Format an item for CSV export

Utils.format_item(item)

Parameters:

  • item (Array)

    The item to format.

Returns:

  • (Array)

    The formatted item.



63
64
65
66
67
68
69
70
71
72
# File 'lib/timet/utils.rb', line 63

def self.format_item(item)
  id, start_time, end_time, tags, notes = item
  [
    id,
    TimeHelper.format_time(start_time),
    TimeHelper.format_time(end_time),
    tags,
    notes
  ]
end

.valid_date_format?(date_string) ⇒ Boolean

Note:

The method validates the date format for single dates and date ranges.

Validates the date format.

Examples:

Validate the date format

Utils.valid_date_format?('2021-10-01') # => true

Parameters:

  • date_string (String)

    The date string to validate.

Returns:

  • (Boolean)

    True if the date format is valid, otherwise false.



84
85
86
87
88
89
# File 'lib/timet/utils.rb', line 84

def self.valid_date_format?(date_string)
  date_format_single = /^\d{4}-\d{2}-\d{2}$/
  date_format_range = /^\d{4}-\d{2}-\d{2}\.\.\d{4}-\d{2}-\d{2}$/

  date_string.match?(date_format_single) || date_string.match?(date_format_range)
end