Class: FulgurChart::Builder

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

Overview

Fluent, reusable builder. Setters mutate and return self; ‘render` may be called multiple times and the builder may be reconfigured between calls.

Instance Method Summary collapse

Constructor Details

#initialize(spec_json) ⇒ Builder

Returns a new instance of Builder.



27
28
29
30
# File 'lib/fulgur_chart.rb', line 27

def initialize(spec_json)
  @spec = spec_json
  @opts = {}
end

Instance Method Details

#dsl(value) ⇒ Object

Force the input DSL (“chartjs”/“vegalite” or the matching Symbol). Omit to auto-detect.



47
48
49
# File 'lib/fulgur_chart.rb', line 47

def dsl(value)
  set(:dsl, value)
end

#font(bytes) ⇒ Object

TrueType/OpenType font bytes (binary String). Omit to use the bundled Noto Sans JP.



52
53
54
# File 'lib/fulgur_chart.rb', line 52

def font(bytes)
  set(:font, bytes)
end

#format(value) ⇒ Object

Default output format for a terminal ‘render` with no argument (Symbol/String).



62
63
64
# File 'lib/fulgur_chart.rb', line 62

def format(value)
  set(:format, value)
end

#height(value) ⇒ Object



37
38
39
# File 'lib/fulgur_chart.rb', line 37

def height(value)
  set(:height, value)
end

#render(fmt = nil) ⇒ Object

Render to the given format (“svg”/“png” or the matching Symbol). Format precedence: explicit argument > ‘.format()` setter > default :svg. Returns a UTF-8 String for svg and a binary (ASCII-8BIT) String for png.



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fulgur_chart.rb', line 69

def render(fmt = nil)
  # Distinguish "no explicit format" (nil) from a falsy-but-explicit one (e.g. false).
  # An explicit argument always wins per the documented precedence; an invalid value is
  # forwarded to the native layer to raise ParseError rather than silently falling back.
  resolved =
    if fmt.nil?
      @opts.key?(:format) ? @opts[:format] : :svg
    else
      fmt
    end
  FulgurChart.render(@spec, resolved, **@opts.reject { |key, _| key == :format })
end

#scale(value) ⇒ Object

Raster scale factor (ignored when rendering SVG).



42
43
44
# File 'lib/fulgur_chart.rb', line 42

def scale(value)
  set(:scale, value)
end

#strict(value = true) ⇒ Object

Reject unknown keys. ‘strict` => true; `strict(false)` => false.



57
58
59
# File 'lib/fulgur_chart.rb', line 57

def strict(value = true)
  set(:strict, value)
end

#width(value) ⇒ Object

Override the chart width / height (px). Applied before input-limit validation.



33
34
35
# File 'lib/fulgur_chart.rb', line 33

def width(value)
  set(:width, value)
end