Class: ChartCandy::Builder::Base
- Inherits:
-
Object
- Object
- ChartCandy::Builder::Base
- Defined in:
- lib/chart-candy/builder/base.rb
Instance Method Summary collapse
- #close_chart ⇒ Object
- #filename ⇒ Object
- #id ⇒ Object
-
#initialize(id, options = {}) ⇒ Base
constructor
A new instance of Base.
- #l(date, options = {}) ⇒ Object
- #period ⇒ Object
- #set_period_from_data(data) ⇒ Object
- #t(path, vars = {}) ⇒ Object
- #title ⇒ Object
- #to_json ⇒ Object
- #to_xls ⇒ Object
Constructor Details
#initialize(id, options = {}) ⇒ Base
Returns a new instance of Base.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/chart-candy/builder/base.rb', line 2 def initialize(id, ={}) .reverse_merge! from: nil, to: nil, step: nil @from = [:from] ? Time.parse([:from]) : nil @to = [:to] ? Time.parse([:to]) : Time.now @chart = { id: id } @chart[:step] = [:step] if [:step] @chart[:title] = t('title') # Subtitles are likely to be dynamic and thus cannot be defined in translation files. @chart[:subtitle] = [:subtitle].presence @chart[:period] = ChartCandy::Builder.period(@from, @to, step: @chart[:step]) if @from end |
Instance Method Details
#close_chart ⇒ Object
18 19 20 |
# File 'lib/chart-candy/builder/base.rb', line 18 def close_chart # Hooks before closing a chart end |
#filename ⇒ Object
22 23 24 25 26 27 28 |
# 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 ⇒ Object
30 31 32 |
# File 'lib/chart-candy/builder/base.rb', line 30 def id @chart[:id] end |
#l(date, options = {}) ⇒ Object
34 35 36 37 38 |
# File 'lib/chart-candy/builder/base.rb', line 34 def l(date, ={}) .reverse_merge!(format: :date_long) return ChartCandy.localize(date, ) end |
#period ⇒ Object
40 41 42 |
# File 'lib/chart-candy/builder/base.rb', line 40 def period @chart[:period] end |
#set_period_from_data(data) ⇒ Object
44 45 46 47 48 49 50 51 |
# 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 = {}) ⇒ Object
53 54 55 56 57 |
# 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 ⇒ Object
59 60 61 |
# File 'lib/chart-candy/builder/base.rb', line 59 def title @chart[:title] end |
#to_json ⇒ Object
63 64 65 66 67 |
# File 'lib/chart-candy/builder/base.rb', line 63 def to_json close_chart return @chart.to_json end |
#to_xls ⇒ Object
69 70 71 72 73 |
# File 'lib/chart-candy/builder/base.rb', line 69 def to_xls close_chart return ChartCandy::Builder::XlsBuilder.chart_to_xls @chart end |