Class: ZTimeLabel

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

Overview

For ScheduledResource protocol. This is a base UseBlock class for ZTimeHeader use-block subclasses. These are timezone-aware time headers (rows) and labels (use-blocks). The timezone is specified by the last component of the resource id (rid). Eg, for ‘-8’ the timezone is ActiveSupport::TimeZone.new(-8)

Direct Known Subclasses

ZTimeLabelDay, ZTimeLabelHour

Constant Summary collapse

TZ_INT_MAP =
{
    -8 => ActiveSupport::TimeZone.new('Pacific Time (US & Canada)' ), # P
    -7 => ActiveSupport::TimeZone.new('Mountain Time (US & Canada)'), # M
    -6 => ActiveSupport::TimeZone.new('Central Time (US & Canada)' ), # C
    -5 => ActiveSupport::TimeZone.new('Eastern Time (US & Canada)' ), # E
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(starttime, endtime) ⇒ ZTimeLabel

Parameters are TimeWithZone or similar



12
13
14
# File 'lib/z_time_label.rb', line 12

def initialize( starttime, endtime )
  @starttime, @endtime = starttime, endtime
end

Instance Attribute Details

#css_classesObject

Returns the value of attribute css_classes.



7
8
9
# File 'lib/z_time_label.rb', line 7

def css_classes
  @css_classes
end

#endtimeObject

Returns the value of attribute endtime.



7
8
9
# File 'lib/z_time_label.rb', line 7

def endtime
  @endtime
end

#starttimeObject

Returns the value of attribute starttime.



7
8
9
# File 'lib/z_time_label.rb', line 7

def starttime
  @starttime
end

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/z_time_label.rb', line 7

def title
  @title
end

Class Method Details

.end_for_start(t) ⇒ Object

Overridden.



85
# File 'lib/z_time_label.rb', line 85

def self.end_for_start(t) t + 1 end

.floor(t) ⇒ Object

Overridden.



86
# File 'lib/z_time_label.rb', line 86

def self.floor(t)         t     end

.get_all_blocks(ids, t1, t2, inc) ⇒ Object

(ScheduledResource protocol) Returns a hash where each key is an rid and the value is an array of ZTimeLabels (use blocks) in the interval t1...t2, ordered by starttime.

What in means depends on inc. If inc(remental) is nil/false, client is building the interval from scratch. If “hi”, it is an addition to an existing interval on the high side. Similarly for “lo”. This is to avoid re-transmitting blocks that span the current time boundaries on the client.

Here the resource is a ZTimeHeader and the use-blocks are ZTimeLabels.

Parameters

  • rids - A list of schedules resource ids (strings).

  • t1 - Start time.

  • t2 - End time.

  • inc - One of nil, “lo”, “hi” (See above).

Returns

  • Hash - Each key is a rid such as Hour0

and the value is an array of Timelabels in the interval, ordered by starttime.



40
41
42
43
44
# File 'lib/z_time_label.rb', line 40

def self.get_all_blocks(ids, t1, t2, inc)
  h = {}
  ids.each{|id| h[id] = get_timeblocks(id, t1, t2, inc)}
  h
end

.get_timeblocks(id, t1, t2, inc) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/z_time_label.rb', line 47

def self.get_timeblocks(id, t1, t2, inc)
  tz = tz_from_rid( id )
  
  t1 = floor( tz.at(t1) )
  t1 = end_for_start(t1)  if inc == 'hi'

  t2 = tz.at(t2)
  t2 = floor( t2 ) - 1    if inc == 'lo'

  enum_for( :time_blocks_starting_through, t1, t2 ).to_a
end

.offset_from_rid(rid) ⇒ Object



81
82
83
# File 'lib/z_time_label.rb', line 81

def self.offset_from_rid( rid )
  (rid.split('_').last || '').to_i
end

.time_blocks_starting_through(starttime, limit) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/z_time_label.rb', line 60

def self.time_blocks_starting_through( starttime, limit )
  while starttime <= limit do  # Hmm... I would have guessed '<' over '<='...
    endtime = end_for_start starttime
    yield new( starttime, endtime )
    starttime = endtime
  end
end

.tz_from_rid(rid) ⇒ Object



76
77
78
79
# File 'lib/z_time_label.rb', line 76

def self.tz_from_rid( rid )
  # ActiveSupport::TimeZone.new offset_from_rid(rid)
  TZ_INT_MAP[ offset_from_rid(rid) ] || TZ_INT_MAP[-8]
end