class ChartCandy::Builder::Base

Public Class Methods

new(id, options={}) click to toggle source
# File lib/chart-candy/builder/base.rb, line 2
def initialize(id, options={})
  options.reverse_merge! from: nil, to: nil, step: nil

  @from = options[:from] ? Time.parse(options[:from]) : nil
  @to = options[:to] ? Time.parse(options[:to]) : Time.now

  @chart = { id: id }
  @chart[:step] = options[:step] if options[:step]
  @chart[:title] = t('title')

  # Subtitles are likely to be dynamic and thus cannot be defined in translation files.
  @chart[:subtitle] = options[:subtitle].presence

  @chart[:period] = ChartCandy::Builder.period(@from, @to, step: @chart[:step]) if @from
end

Public Instance Methods

close_chart() click to toggle source
# File lib/chart-candy/builder/base.rb, line 18
def close_chart
  # Hooks before closing a chart
end
filename() click to toggle source
# File lib/chart-candy/builder/base.rb, line 22
def filename
  name = [title.parameterize]
  name << @from.strftime('%Y%m%d') if @from
  name << @to.strftime('%Y%m%d') if @to
  name = name.compact.join('-')
  "#{name}.xls"
end
id() click to toggle source
# File lib/chart-candy/builder/base.rb, line 30
def id
  @chart[:id]
end
l(date, options={}) click to toggle source
# File lib/chart-candy/builder/base.rb, line 34
def l(date, options={})
  options.reverse_merge!(format: :date_long)

  return ChartCandy.localize(date, options)
end
period() click to toggle source
# File lib/chart-candy/builder/base.rb, line 40
def period
  @chart[:period]
end
set_period_from_data(data) click to toggle source
# File lib/chart-candy/builder/base.rb, line 44
def set_period_from_data(data)
  @from = data.first
  @to = data.last

  @chart[:step] = ChartCandy::Builder.get_step_from_interval(data[1] - data[0]) if not @chart[:step]

  @chart[:period] = ChartCandy::Builder.period @from, @to, step: @chart[:step]
end
t(path, vars={}) click to toggle source
# File lib/chart-candy/builder/base.rb, line 53
def t(path, vars={})
  vars.reverse_merge! :default => ''

  ChartCandy.translate("#{id.gsub('-', '_')}.#{path}", vars)
end
title() click to toggle source
# File lib/chart-candy/builder/base.rb, line 59
def title
  @chart[:title]
end
to_json() click to toggle source
# File lib/chart-candy/builder/base.rb, line 63
def to_json
  close_chart

  return @chart.to_json
end
to_xls() click to toggle source
# File lib/chart-candy/builder/base.rb, line 69
def to_xls
  close_chart

  return ChartCandy::Builder::XlsBuilder.chart_to_xls @chart
end