Module: Sevgi::Graphics::Module

Defined in:
lib/sevgi/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 when it has 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 may 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 Sevgi::Graphics::Module

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

  def call(id)
    draw id
  end

  private

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

Sevgi::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 in the current element context and does not receive the invocation arguments.

Yields:

  • evaluates the drawing DSL in the current element context

Yield Returns:

  • (Object)

    ignored block result

Returns:

  • (nil)

Raises:

  • (Sevgi::ArgumentError)

    when no block is given

  • (FrozenError)

    when the callable module is frozen



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

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

  ArgumentError.("Block required") unless block

  own_configuration
  @sevgi_bases << block
  nil
end