Module: Sumi::Graphics::Module

Defined in:
graphics/lib/sumi/graphics/mixtures/call.rb

Overview

Callable drawing module support. Extend a plain Ruby module with this API to make its public instance methods callable drawing steps. Name the method call when the module has a single drawing step. Use descriptive method names for multiple steps. Base blocks add argument-independent shared SVG content once per invocation before the public drawing methods. Invocation does not change the configured module, so it can be frozen after its drawing steps are defined. A duplicate or clone owns an independent configuration snapshot. Freezing a callable module prevents later base registration while leaving invocation available.

Examples:

Define and call a drawing module

Widget = Module.new do
  extend Sumi::Graphics::Module

  base { css ".widget" => { fill: "red" } }

  def call(id)
    draw id
  end

  private

  def draw(id) = rect id:, class: "widget"
end

Sumi::Graphics.SVG { Call Widget, "box" }

Instance Method Summary collapse

Instance Method Details

#base { ... } ⇒ nil

Registers argument-independent shared drawing steps. Every invocation runs inherited base blocks parent-first, then locally registered base blocks in registration order, before the module's public drawing methods. The block runs once on the callable receiver, which delegates drawing words to the current SVG element. It receives no invocation arguments. Callable methods receive those arguments and the caller's block unchanged.

Yields:

  • evaluates shared drawing steps on the callable receiver

Yield Returns:

  • (Object) —

    ignored block result

Returns:

  • (nil)

Raises:

  • (Sumi::ArgumentError) —

    when no block is given

  • (FrozenError) —

    when the callable module is frozen



83
84
85
86
87
88
89
90
91
# File 'graphics/lib/sumi/graphics/mixtures/call.rb', line 83

def base(&block)
  raise ::FrozenError, "can't modify frozen callable module" if frozen?

  ArgumentError.("Block required") unless block

  own_configuration
  @sumi_bases << block
  nil
end