Class: Vizcore::DSL::StyleBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/vizcore/dsl/style_builder.rb

Overview

Collects reusable layer parameter presets for the ‘style` DSL.

Instance Method Summary collapse

Constructor Details

#initialize(name:, kind: "style") ⇒ StyleBuilder

Returns a new instance of StyleBuilder.

Parameters:

  • name (Symbol, String)

    style identifier

  • kind (String) (defaults to: "style")

    user-facing DSL kind for error messages



9
10
11
12
13
# File 'lib/vizcore/dsl/style_builder.rb', line 9

def initialize(name:, kind: "style")
  @name = name.to_sym
  @kind = kind
  @params = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Stores one-argument style setters into ‘params`.



45
46
47
48
49
50
51
52
# File 'lib/vizcore/dsl/style_builder.rb', line 45

def method_missing(method_name, *args, &block)
  if block.nil? && args.length == 1
    @params[method_name.to_sym] = args.first
    return args.first
  end

  super
end

Instance Method Details

#evaluate { ... } ⇒ Vizcore::DSL::StyleBuilder

Evaluate a style block.

Yields:

  • Style parameter declarations

Returns:

Raises:

  • (ArgumentError)


19
20
21
22
23
24
# File 'lib/vizcore/dsl/style_builder.rb', line 19

def evaluate(&block)
  instance_eval(&block) if block
  raise ArgumentError, "#{@kind} #{@name} requires at least one parameter" if @params.empty?

  self
end

#palette(*colors) ⇒ Array<String>

Store an ordered color palette for styles and themes.

Parameters:

  • colors (Array<String, Array<String>>)

    color values such as “#00ffff”

Returns:

  • (Array<String>)

Raises:

  • (ArgumentError)

    when no non-blank colors are supplied



39
40
41
# File 'lib/vizcore/dsl/style_builder.rb', line 39

def palette(*colors)
  @params[:palette] = normalize_palette(colors)
end

#respond_to_missing?(_method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/vizcore/dsl/style_builder.rb', line 54

def respond_to_missing?(_method_name, _include_private = false)
  true
end

#to_hHash

Returns serialized style payload.

Returns:

  • (Hash)

    serialized style payload



27
28
29
30
31
32
# File 'lib/vizcore/dsl/style_builder.rb', line 27

def to_h
  {
    name: @name,
    params: @params.dup
  }
end