Module: Sevgi::Graphics::Document

Defined in:
lib/sevgi/graphics/document.rb,
lib/sevgi/graphics/document/base.rb,
lib/sevgi/graphics/document/html.rb,
lib/sevgi/graphics/document/default.rb,
lib/sevgi/graphics/document/minimal.rb,
lib/sevgi/graphics/document/inkscape.rb

Overview

SVG document profile factory.

Defined Under Namespace

Classes: Base, Default, HTML, Inkscape, Minimal, Profile, Proto

Constant Summary collapse

DEFAULTS =

Default render-time checks.

{lint: true, validate: true}.freeze

Class Method Summary collapse

Class Method Details

.call(document, canvas = Undefined) { ... } ⇒ Sevgi::Graphics::Document::Proto

Builds a root SVG element from a document profile.

Parameters:

Yields:

  • evaluates the drawing DSL in the new root element

Yield Returns:

  • (Object)

    ignored block result

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when the document profile or root XML attributes are invalid



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sevgi/graphics/document.rb', line 90

def self.call(document, canvas = Undefined, **, &block)
  klass = case document
  when ::Class
    document if document <= Proto
  else
    Profile[document]
  end

  ArgumentError.("Unknown document profile: #{document}") unless klass

  klass.root(**klass.attributes, **canvas_attributes(canvas), **, &block)
end

.define(name = Undefined, preambles: Undefined, attributes: Undefined, overwrite: false) ⇒ Class

Defines or returns a document profile class. Profile metadata is captured before class or thread-atomic registry mutation. Mutable non-container attribute values are stringified once during capture. Successful named definitions return the canonical class stored by the registry, including when identical definitions race.

Parameters:

  • name (Symbol, String, Sevgi::Undefined) (defaults to: Undefined)

    profile name, or Undefined for an anonymous profile

  • preambles (Array<String>, nil, Sevgi::Undefined) (defaults to: Undefined)

    document preamble lines

  • attributes (Hash, nil, Sevgi::Undefined) (defaults to: Undefined)

    default root attributes

  • overwrite (Boolean) (defaults to: false)

    true to replace an existing profile

Returns:

  • (Class)

    document class

Raises:

  • (Sevgi::ArgumentError)

    when a name conflicts or metadata is invalid XML, cyclic, or cannot be stringified



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/sevgi/graphics/document.rb', line 126

def self.define(name = Undefined, preambles: Undefined, attributes: Undefined, overwrite: false)
  return anonymous(attributes:, preambles:) if name == Undefined

  return lookup(name) if preambles == Undefined && attributes == Undefined

  name = Profile.normalize!(name)

  if (current = Profile[name])
    reject_conflict(name, current, attributes:, preambles:) unless overwrite
    return current unless overwrite
  end

  attributes, preambles = defaults(attributes:, preambles:)
  Class.new(Base) { document(name, preambles:, attributes:, overwrite:) }
  Registry[name]
end