Class: Postsvg::Svg::Element
- Inherits:
-
Object
- Object
- Postsvg::Svg::Element
- 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.
Direct Known Subclasses
Postsvg::Svg::Elements::Circle, Postsvg::Svg::Elements::ClipPath, Postsvg::Svg::Elements::Defs, Postsvg::Svg::Elements::Ellipse, Postsvg::Svg::Elements::Group, Postsvg::Svg::Elements::Image, Postsvg::Svg::Elements::Line, Postsvg::Svg::Elements::Path, Postsvg::Svg::Elements::Polyline, Postsvg::Svg::Elements::Rect, Postsvg::Svg::Elements::Svg, Postsvg::Svg::Elements::Text, OpenElement
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
selfat the call site. {}
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#clip_path_id ⇒ Object
readonly
Returns the value of attribute clip_path_id.
-
#fill ⇒ Object
readonly
Returns the value of attribute fill.
-
#stroke ⇒ Object
readonly
Returns the value of attribute stroke.
-
#stroke_paint ⇒ Object
readonly
Returns the value of attribute stroke_paint.
-
#transform ⇒ Object
readonly
Returns the value of attribute transform.
Class Method Summary collapse
Instance Method Summary collapse
-
#default_fill ⇒ Object
Default paint: black fill, no stroke.
- #element_name ⇒ Object
-
#initialize(transform: nil, clip_path_id: nil, attributes: {}, children: [], fill: nil, stroke_paint: nil, stroke: nil) ⇒ Element
constructor
A new instance of Element.
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
21 22 23 |
# File 'lib/postsvg/svg/element.rb', line 21 def attributes @attributes end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
21 22 23 |
# File 'lib/postsvg/svg/element.rb', line 21 def children @children end |
#clip_path_id ⇒ Object (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 |
#fill ⇒ Object (readonly)
Returns the value of attribute fill.
21 22 23 |
# File 'lib/postsvg/svg/element.rb', line 21 def fill @fill end |
#stroke ⇒ Object (readonly)
Returns the value of attribute stroke.
21 22 23 |
# File 'lib/postsvg/svg/element.rb', line 21 def stroke @stroke end |
#stroke_paint ⇒ Object (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 |
#transform ⇒ Object (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 |
Instance Method Details
#default_fill ⇒ Object
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_name ⇒ Object
40 41 42 |
# File 'lib/postsvg/svg/element.rb', line 40 def element_name self.class::ELEMENT_NAME end |