Class: Highcharts::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/highcharts/base.rb', line 6

def initialize(opts = {})
  @options = opts
  @suboptions ||= {}

  # We want to make sure that options in the end is actually a Hash of options, or an Array of hashes of options.
  # When default is defined in a subclass and the passed options are not a Hash or Array, we want to replace options
  # with a Hash where the only key/value pair is the default key and the options value.
  if !options.is_a?(Hash) || (options.is_a?(Array) && !options.first.is_a?(Hash))
    if default.present?
      @options = {default => options}
    else
      raise ArgumentError, "You must pass a Hash to #{self.class}. You passed #{options.inspect}"
    end
  end

  # If there is an option that is available as a suboption to the current class,
  # let's set the option to an instance of that subclass.
  options.each do |k, v|
    @options[k] = "Highcharts::#{suboptions[k]}".constantize.new(v) if suboptions.keys.include?(k)
  end
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



4
5
6
# File 'lib/highcharts/base.rb', line 4

def default
  @default
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/highcharts/base.rb', line 4

def options
  @options
end

#skip_quotationObject (readonly)

Returns the value of attribute skip_quotation.



4
5
6
# File 'lib/highcharts/base.rb', line 4

def skip_quotation
  @skip_quotation
end

#suboptionsObject (readonly)

Returns the value of attribute suboptions.



4
5
6
# File 'lib/highcharts/base.rb', line 4

def suboptions
  @suboptions
end

Instance Method Details

#inspectObject



28
29
30
# File 'lib/highcharts/base.rb', line 28

def inspect
  "#<#{self.class}:0x#{object_id} #{options.inspect}>"
end

#to_jsonObject

This method is used in the parent class, Highcharts, in order to render the options in a JavaScript-friendly (JSON) format.



33
34
35
36
37
38
39
40
41
# File 'lib/highcharts/base.rb', line 33

def to_json
  json = options.collect do |k, v|
    "\"#{k}\":" +
    (suboptions.keys.include?(k) && !v.is_a?(Array) ? '{' : '') +
    (v.is_a?(Array) && suboptions.keys.include?(k) ? "[{#{v.collect(&:to_json).join('},{')}}]" : check_quotation(k, v.to_json)) +
    (suboptions.keys.include?(k) && !v.is_a?(Array) ? '}' : '')
  end
  json.join(',')
end