Class: Plasticine::Builder::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/plasticine/builder/base.rb

Direct Known Subclasses

Column, Line

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, options = {}) ⇒ Base



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/plasticine/builder/base.rb', line 4

def initialize(id, options={})
  options.reverse_merge! from: nil, chart_style: 'standard', to: nil, step: nil

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

  @visual = { id: id }
  @visual[:step] = options[:step] if options[:step]
  @visual[:title] = t('title')
  @visual[:period] = Plasticine::Builder.period(@from, @to, step: @visual[:step]) if @from
end

Instance Attribute Details

#visualObject

Returns the value of attribute visual.



2
3
4
# File 'lib/plasticine/builder/base.rb', line 2

def visual
  @visual
end

Instance Method Details

#chart_style=(style) ⇒ Object



20
21
22
# File 'lib/plasticine/builder/base.rb', line 20

def chart_style=(style)
  @visual[:chart_style] = "style-#{style}"
end

#close_visualObject



16
17
18
# File 'lib/plasticine/builder/base.rb', line 16

def close_visual
  # Hooks before closing a visual
end

#idObject



24
25
26
# File 'lib/plasticine/builder/base.rb', line 24

def id
  @visual[:id]
end

#l(date, options = {}) ⇒ Object



28
29
30
31
32
# File 'lib/plasticine/builder/base.rb', line 28

def l(date, options={})
  options.reverse_merge!(format: :date_long)

  Plasticine.localize(date, options)
end

#periodObject



34
35
36
# File 'lib/plasticine/builder/base.rb', line 34

def period
  @visual[:period]
end

#set_period_from_data(data) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/plasticine/builder/base.rb', line 38

def set_period_from_data(data)
  @from = data.first
  @to = data.last

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

  @visual[:period] = Plasticine::Builder.period @from, @to, step: @visual[:step]
end

#t(path, vars = {}) ⇒ Object



47
48
49
50
51
# File 'lib/plasticine/builder/base.rb', line 47

def t(path, vars={})
  vars.reverse_merge! default: ''

  Plasticine.translate("#{id.gsub('-', '_')}.#{path}", vars)
end

#titleObject



53
54
55
# File 'lib/plasticine/builder/base.rb', line 53

def title
  @visual[:title]
end

#to_jsonObject



57
58
59
60
61
# File 'lib/plasticine/builder/base.rb', line 57

def to_json
  close_visual

  @visual.to_json
end