class Mhc::Formatter::Base

prepare format_header

format_day_header
  format_item_header
  format_item
  format_item_hooter
format_day_hooter

format_footer teardown

Public Class Methods

new(date_range:, options:nil) click to toggle source
# File lib/mhc/formatter/base.rb, line 13
def initialize(date_range:, options:nil)
  @date_range = date_range
  @options = options
  @occurrences, @events, @items = [], [], {}
  @event_hash = {}
end

Public Instance Methods

<<(occurrence) click to toggle source
# File lib/mhc/formatter/base.rb, line 20
def <<(occurrence)
  event = occurrence.event
  @occurrences << occurrence
  @events << event unless @event_hash[event]
  @event_hash[event] = true

  @items[occurrence.date] ||= []
  @items[occurrence.date] << occurrence
end
to_s() click to toggle source
# File lib/mhc/formatter/base.rb, line 30
def to_s
  context = {:items => @items}.merge(@options)
  prepare(context)
  string = format_header(context) + format_body(context) + format_footer(context)
  teardown(context)
  return string
end

Private Instance Methods

append(item, separator = " ") click to toggle source

helpers

# File lib/mhc/formatter/base.rb, line 93
def append(item, separator = " ")
  return "" if item.to_s.empty?
  return separator + item.to_s
end
enclose(item, bracket = "[]") click to toggle source
# File lib/mhc/formatter/base.rb, line 103
def enclose(item, bracket = "[]")
  return "" if item.to_s.empty?
  return bracket[0] + item.to_s + bracket[1]
end
expand_multiple_days_occurrences() click to toggle source
# File lib/mhc/formatter/base.rb, line 50
def expand_multiple_days_occurrences
  @occurrences.each do |oc|
    next if oc.oneday?
    ((oc.first + 1) .. oc.last).each do |date|
      @items[date] ||= []
      @items[date] << oc
    end
  end
end
format_body(context) click to toggle source
# File lib/mhc/formatter/base.rb, line 65
def format_body(context)
  context[:number] = 0
  @items.keys.sort.map{|date| format_day(context, date, @items[date]) }.join
end
format_day(context, date, items) click to toggle source
# File lib/mhc/formatter/base.rb, line 70
def format_day(context, date, items)
  string = format_day_header(context, date, items.any?{|e| e.holiday?})

  items = sort_items_in_day(items)
  items.each_with_index do |occurrence, count|
    context[:number] += 1
    context[:number_in_day] = count + 1
    string += format_item(context, date, occurrence)
  end

  return string + format_day_footer(context, date)
end
format_day_header(context, date, is_holiday) click to toggle source
# File lib/mhc/formatter/base.rb, line 62
def format_day_header(context, date, is_holiday); ""; end
format_header(context) click to toggle source
# File lib/mhc/formatter/base.rb, line 60
def format_header(context); ""; end
format_item(context, date, item) click to toggle source
# File lib/mhc/formatter/base.rb, line 83
def format_item(context, date, item)
  raise "Implement in subclasses."
end
format_item_header(context, date, item) click to toggle source
# File lib/mhc/formatter/base.rb, line 87
def format_item_header(context, date, item)
  raise "Implement in subclasses."
end
pad_empty_dates() click to toggle source
# File lib/mhc/formatter/base.rb, line 44
def pad_empty_dates
  @date_range.each do |date|
    @items[date] ||= []
  end
end
prepare(context) click to toggle source
# File lib/mhc/formatter/base.rb, line 41
def prepare(context);  end
prepend(item, separator = " ") click to toggle source
# File lib/mhc/formatter/base.rb, line 98
def prepend(item, separator = " ")
  return "" if item.to_s.empty?
  return item.to_s + separator
end
sort_items_in_day(items) click to toggle source

sort occurrences in a day make sure all-day occurrences are prior to others

# File lib/mhc/formatter/base.rb, line 110
def sort_items_in_day(items)
  items.sort do |a,b|
    sign_a = a.allday? ? 0 : 1
    sign_b = b.allday? ? 0 : 1

    if sign_a != sign_b
      sign_a - sign_b
    else
      a <=> b
    end
  end
end
teardown(context) click to toggle source
# File lib/mhc/formatter/base.rb, line 42
def teardown(context); end