Class: Postsvg::Svg::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/postsvg/svg/element.rb

Overview

Base value object for an SVG element. Subclasses (Svg::Elements::*) carry typed semantic fields; the base provides a uniform interface for the translation layer's dispatch.

Element instances are immutable: built once from a Nokogiri node via the .from_node factory. The node is not retained.

Every element exposes children, transform, clip_path_id, fill, stroke_paint, stroke, stroke_paint_value, all defaulting to nil / []. Subclasses override only the attributes that are actually present in the source element. This lets the Translation::Handlers::Shared helpers call any of these methods uniformly without resorting to respond_to? type checks.

Constant Summary collapse

ELEMENT_NAME =
"abstract"
REGISTRY =

Registry: SVG element name -> Element subclass. OCP: register new subclasses via Element.register("rect", Elements::Rect) instead of editing a switch. Backed by a constant Hash so subclasses share a single registry regardless of self at the call site.

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transform: nil, clip_path_id: nil, attributes: {}, children: [], fill: nil, stroke_paint: nil, stroke: nil) ⇒ Element

Returns a new instance of Element.



24
25
26
27
28
29
30
31
32
33
# File 'lib/postsvg/svg/element.rb', line 24

def initialize(transform: nil, clip_path_id: nil, attributes: {},
               children: [], fill: nil, stroke_paint: nil, stroke: nil)
  @transform = transform
  @clip_path_id = clip_path_id
  @attributes = attributes.dup.freeze
  @children = children.freeze
  @fill = fill
  @stroke_paint = stroke_paint
  @stroke = stroke
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



21
22
23
# File 'lib/postsvg/svg/element.rb', line 21

def attributes
  @attributes
end

#childrenObject (readonly)

Returns the value of attribute children.



21
22
23
# File 'lib/postsvg/svg/element.rb', line 21

def children
  @children
end

#clip_path_idObject (readonly)

Returns the value of attribute clip_path_id.



21
22
23
# File 'lib/postsvg/svg/element.rb', line 21

def clip_path_id
  @clip_path_id
end

#fillObject (readonly)

Returns the value of attribute fill.



21
22
23
# File 'lib/postsvg/svg/element.rb', line 21

def fill
  @fill
end

#strokeObject (readonly)

Returns the value of attribute stroke.



21
22
23
# File 'lib/postsvg/svg/element.rb', line 21

def stroke
  @stroke
end

#stroke_paintObject (readonly)

Returns the value of attribute stroke_paint.



21
22
23
# File 'lib/postsvg/svg/element.rb', line 21

def stroke_paint
  @stroke_paint
end

#transformObject (readonly)

Returns the value of attribute transform.



21
22
23
# File 'lib/postsvg/svg/element.rb', line 21

def transform
  @transform
end

Class Method Details

.from_node(node) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/postsvg/svg/element.rb', line 60

def from_node(node)
  subclass = registry[node.name]
  return subclass.from_node(node) if subclass

  OpenElement.new(name: node.name,
                  children: node.element_children.map { |c| from_node(c) })
end

.register(element_name, subclass) ⇒ Object



56
57
58
# File 'lib/postsvg/svg/element.rb', line 56

def register(element_name, subclass)
  registry[element_name] = subclass
end

.registryObject



52
53
54
# File 'lib/postsvg/svg/element.rb', line 52

def registry
  Postsvg::Svg::Element::REGISTRY
end

Instance Method Details

#default_fillObject

Default paint: black fill, no stroke. Handlers can override.



36
37
38
# File 'lib/postsvg/svg/element.rb', line 36

def default_fill
  nil
end

#element_nameObject



40
41
42
# File 'lib/postsvg/svg/element.rb', line 40

def element_name
  self.class::ELEMENT_NAME
end