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
-
.add_events(items) ⇒ Icalendar::Calendar
Creates an iCalendar object and adds events to it.
-
.add_hashes(base_hash, additional_hash) ⇒ Hash
Merges two hashes, summing the numeric values of corresponding keys.
-
.assign_event_attributes(event, item) ⇒ Object
Assigns attributes to an iCalendar event.
-
.convert_to_datetime(timestamp) ⇒ DateTime
Converts a timestamp to a DateTime object.
-
.create_event(item) ⇒ Icalendar::Event
Creates an iCalendar event from the given item.
-
.date_ranges ⇒ Hash
Provides predefined date ranges for filtering.
-
.format_item(item) ⇒ Array
Formats an item for CSV export.
-
.valid_date_format?(date_string) ⇒ Boolean
Validates the date format.
Class Method Details
.add_events(items) ⇒ Icalendar::Calendar
Creates an iCalendar object and adds events to it.
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.
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.
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.) 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.
30 31 32 |
# File 'lib/timet/utils.rb', line 30 def self.convert_to_datetime() Time.at().to_datetime end |
.create_event(item) ⇒ Icalendar::Event
Creates an iCalendar event from the given item.
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_ranges ⇒ Hash
The method returns a hash with predefined date ranges for ‘today’, ‘yesterday’, ‘week’, and ‘month’.
Provides predefined date ranges for filtering.
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
The method formats the item’s ID, start time, end time, tag, and notes.
Formats an item for CSV export.
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, , notes = item [ id, TimeHelper.format_time(start_time), TimeHelper.format_time(end_time), , notes ] end |
.valid_date_format?(date_string) ⇒ Boolean
The method validates the date format for single dates and date ranges.
Validates the date format.
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 |