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 |
# 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') @chart[:period] = ChartCandy::Builder.period(@from, @to, step: @chart[:step]) if @from end |
Instance Method Details
#close_chart ⇒ Object
14 15 16 |
# File 'lib/chart-candy/builder/base.rb', line 14 def close_chart # Hooks before closing a chart end |
#filename ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/chart-candy/builder/base.rb', line 18 def filename name = [title.parameterize] name << @from.strftime('%Y%m%d') if @from name << @to.strftime('%Y%m%d') if @to return name.compact.join('-') end |
#id ⇒ Object
26 27 28 |
# File 'lib/chart-candy/builder/base.rb', line 26 def id @chart[:id] end |
#l(date, options = {}) ⇒ Object
30 31 32 33 34 |
# File 'lib/chart-candy/builder/base.rb', line 30 def l(date, ={}) .reverse_merge!(format: :date_long) return ChartCandy.localize(date, ) end |
#period ⇒ Object
36 37 38 |
# File 'lib/chart-candy/builder/base.rb', line 36 def period @chart[:period] end |
#set_period_from_data(data) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/chart-candy/builder/base.rb', line 40 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
49 50 51 52 53 |
# File 'lib/chart-candy/builder/base.rb', line 49 def t(path, vars={}) vars.reverse_merge! :default => '' ChartCandy.translate("#{id.gsub('-', '_')}.#{path}", vars) end |
#title ⇒ Object
55 56 57 |
# File 'lib/chart-candy/builder/base.rb', line 55 def title @chart[:title] end |
#to_json ⇒ Object
59 60 61 62 63 |
# File 'lib/chart-candy/builder/base.rb', line 59 def to_json close_chart return @chart.to_json end |
#to_xls ⇒ Object
65 66 67 68 69 |
# File 'lib/chart-candy/builder/base.rb', line 65 def to_xls close_chart return ChartCandy::Builder::XlsBuilder.chart_to_xls @chart end |