Class: Vishnu::StandardTime::FocusSession

Inherits:
Object
  • Object
show all
Defined in:
lib/vishnu/standard_time.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total_flows:, label: nil, started_at: Vishnu::StandardTime.local_now) ⇒ FocusSession

Returns a new instance of FocusSession.

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
# File 'lib/vishnu/standard_time.rb', line 70

def initialize(total_flows:, label: nil, started_at: Vishnu::StandardTime.local_now)
  @total_flows = Integer(total_flows)
  raise ArgumentError, "total_flows must be positive" unless @total_flows.positive?

  @label = label.to_s.strip.empty? ? "focus" : label.to_s
  @started_at = started_at
  @ends_at = Vishnu::StandardTime.after(flows: @total_flows, from: @started_at)
end

Instance Attribute Details

#ends_atObject (readonly)

Returns the value of attribute ends_at.



68
69
70
# File 'lib/vishnu/standard_time.rb', line 68

def ends_at
  @ends_at
end

#labelObject (readonly)

Returns the value of attribute label.



68
69
70
# File 'lib/vishnu/standard_time.rb', line 68

def label
  @label
end

#started_atObject (readonly)

Returns the value of attribute started_at.



68
69
70
# File 'lib/vishnu/standard_time.rb', line 68

def started_at
  @started_at
end

#total_flowsObject (readonly)

Returns the value of attribute total_flows.



68
69
70
# File 'lib/vishnu/standard_time.rb', line 68

def total_flows
  @total_flows
end

Instance Method Details

#complete?(now_parts = Vishnu::StandardTime.local_now) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/vishnu/standard_time.rb', line 93

def complete?(now_parts = Vishnu::StandardTime.local_now)
  flows_remaining(now_parts).zero?
end

#flows_elapsed(now_parts = Vishnu::StandardTime.local_now) ⇒ Object



79
80
81
82
83
# File 'lib/vishnu/standard_time.rb', line 79

def flows_elapsed(now_parts = Vishnu::StandardTime.local_now)
  elapsed_ms = Vishnu::StandardTime.unix_ms_for(now_parts) - Vishnu::StandardTime.unix_ms_for(started_at)
  flows = Vishnu::StandardTime.flows_for(milliseconds: elapsed_ms)
  [[flows, 0].max, total_flows].min
end

#flows_remaining(now_parts = Vishnu::StandardTime.local_now) ⇒ Object



85
86
87
# File 'lib/vishnu/standard_time.rb', line 85

def flows_remaining(now_parts = Vishnu::StandardTime.local_now)
  [total_flows - flows_elapsed(now_parts), 0].max
end

#percent_complete(now_parts = Vishnu::StandardTime.local_now) ⇒ Object



89
90
91
# File 'lib/vishnu/standard_time.rb', line 89

def percent_complete(now_parts = Vishnu::StandardTime.local_now)
  (flows_elapsed(now_parts).to_f / total_flows) * 100
end

#summary(now_parts = Vishnu::StandardTime.local_now) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/vishnu/standard_time.rb', line 110

def summary(now_parts = Vishnu::StandardTime.local_now)
  "%s · %s / %s flows · %.2f%% complete" % [
    label,
    Vishnu::StandardTime.format_number(flows_elapsed(now_parts)),
    Vishnu::StandardTime.format_number(total_flows),
    percent_complete(now_parts)
  ]
end

#to_h(now_parts = Vishnu::StandardTime.local_now) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/vishnu/standard_time.rb', line 97

def to_h(now_parts = Vishnu::StandardTime.local_now)
  {
    label: label,
    total_flows: total_flows,
    flows_elapsed: flows_elapsed(now_parts),
    flows_remaining: flows_remaining(now_parts),
    percent_complete: percent_complete(now_parts).round(2),
    started_at: Vishnu::StandardTime.to_h(started_at),
    ends_at: Vishnu::StandardTime.to_h(ends_at),
    complete: complete?(now_parts)
  }
end