Class: NitroRails::TimeInterval
- Inherits:
-
Object
- Object
- NitroRails::TimeInterval
- Defined in:
- app/lib/nitro_rails/time_interval.rb
Instance Attribute Summary collapse
-
#ended_at ⇒ Object
Returns the value of attribute ended_at.
-
#started_at ⇒ Object
Returns the value of attribute started_at.
Class Method Summary collapse
- .from(started_at, **options) ⇒ Object
- .parse(string, date: Date.current, offset: 0) ⇒ Object
- .valid_time_interval_string?(string) ⇒ Boolean
Instance Method Summary collapse
- #change(**attributes) ⇒ Object
- #dates ⇒ Object
- #duration ⇒ Object
- #from(time) ⇒ Object
-
#initialize(**attributes) ⇒ TimeInterval
constructor
A new instance of TimeInterval.
- #inspect ⇒ Object
- #localtime(offset) ⇒ Object
- #times ⇒ Object (also: #to_a)
- #to(time) ⇒ Object
- #to_fs(opt = :time, join: " - ") ⇒ Object
Constructor Details
#initialize(**attributes) ⇒ TimeInterval
27 28 29 |
# File 'app/lib/nitro_rails/time_interval.rb', line 27 def initialize(**attributes) set(**attributes) end |
Instance Attribute Details
#ended_at ⇒ Object
Returns the value of attribute ended_at.
2 3 4 |
# File 'app/lib/nitro_rails/time_interval.rb', line 2 def ended_at @ended_at end |
#started_at ⇒ Object
Returns the value of attribute started_at.
2 3 4 |
# File 'app/lib/nitro_rails/time_interval.rb', line 2 def started_at @started_at end |
Class Method Details
.from(started_at, **options) ⇒ Object
23 24 25 |
# File 'app/lib/nitro_rails/time_interval.rb', line 23 def self.from(started_at, **) new(**.merge(started_at: started_at)) end |
.parse(string, date: Date.current, offset: 0) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'app/lib/nitro_rails/time_interval.rb', line 4 def self.parse(string, date: Date.current, offset: 0) if valid_time_interval_string?(string) times = string.split("-").map do |time| time.to_time(:utc).change( offset: offset, day: date.day, month: date.month, year: date.year ) end new(started_at: times.first, ended_at: times.first > times.last ? times.last + 1.day : times.last) end end |
.valid_time_interval_string?(string) ⇒ Boolean
19 20 21 |
# File 'app/lib/nitro_rails/time_interval.rb', line 19 def self.valid_time_interval_string?(string) string.match?(/\A\s*#{NitroRails::TimeString::TIME_INTERVAL_REGEX}\s*\z/) end |
Instance Method Details
#change(**attributes) ⇒ Object
31 32 33 34 35 |
# File 'app/lib/nitro_rails/time_interval.rb', line 31 def change(**attributes) set(**attributes) reset_duration return self end |
#dates ⇒ Object
55 56 57 |
# File 'app/lib/nitro_rails/time_interval.rb', line 55 def dates [started_at.to_date, ended_at.to_date] end |
#duration ⇒ Object
45 46 47 |
# File 'app/lib/nitro_rails/time_interval.rb', line 45 def duration @duration ||= ActiveSupport::Duration.build(ended_at - started_at) end |
#from(time) ⇒ Object
37 38 39 |
# File 'app/lib/nitro_rails/time_interval.rb', line 37 def from(time) change(started_at: time) end |
#inspect ⇒ Object
70 71 72 |
# File 'app/lib/nitro_rails/time_interval.rb', line 70 def inspect to_fs(:db) end |
#localtime(offset) ⇒ Object
59 60 61 62 63 64 |
# File 'app/lib/nitro_rails/time_interval.rb', line 59 def localtime(offset) NitroRails::TimeInterval.new( started_at: started_at.localtime(offset), ended_at: ended_at.localtime(offset) ) end |
#times ⇒ Object Also known as: to_a
49 50 51 |
# File 'app/lib/nitro_rails/time_interval.rb', line 49 def times [started_at, ended_at] end |
#to(time) ⇒ Object
41 42 43 |
# File 'app/lib/nitro_rails/time_interval.rb', line 41 def to(time) change(ended_at: time) end |
#to_fs(opt = :time, join: " - ") ⇒ Object
66 67 68 |
# File 'app/lib/nitro_rails/time_interval.rb', line 66 def to_fs(opt = :time, join: " - ") times.map { |time| time.to_fs(opt) }.join(join) end |