Class: Sumi::Graphics::Element
- Inherits:
-
Object
- Object
- Sumi::Graphics::Element
- Defined in:
- graphics/lib/sumi/graphics/element.rb
Overview
SVG element node used by the graphics DSL.
Dynamic SVG element methods accept text, content objects, and any number of attribute Hashes in one call. Hashes
are applied from left to right. Later values replace earlier values unless their names use the + update suffix.
Ordinary dup and clone create independent subtrees, preserve IDs, and detach non-root copies from their parent.
Document copies remain roots. Use Duplicate for DSL-specific ID remapping and attachment.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Sumi::Graphics::Attributes
readonly
Returns the attribute store.
-
#name ⇒ Symbol
readonly
Returns the SVG element name.
Class Method Summary collapse
-
.element(name, *arguments, parent: RootParent) { ... } ⇒ Sumi::Graphics::Element
Builds an element node.
-
.root(*arguments) { ... } ⇒ Sumi::Graphics::Element
Builds an SVG root element.
-
.root?(element) ⇒ Boolean
Reports whether an element is the root element.
-
.valid?(name) ⇒ Boolean
Reports whether a candidate can dispatch as an SVG element name.
Instance Method Summary collapse
-
#children ⇒ Array<Sumi::Graphics::Element>
Returns a read-only snapshot of child elements in rendering order.
-
#contents ⇒ Array<Sumi::Graphics::Content>
Returns a read-only snapshot of element content objects in rendering order.
-
#inspect ⇒ String
Returns a concise summary of the element type, id, and child count.
-
#parent ⇒ Sumi::Graphics::Element?
Returns the parent element.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *arguments) { ... } ⇒ Sumi::Graphics::Element (private)
Dispatches SVG element DSL calls and caches valid element methods.
210 211 212 213 214 |
# File 'graphics/lib/sumi/graphics/element.rb', line 210 def method_missing(name, *arguments, &block) return super if singleton_class.private_method_defined?(name) || singleton_class.protected_method_defined?(name) Element.valid?(tag = Element.send(:id, name)) ? Dispatch.(self, name, tag, *arguments, &block) : super end |
Instance Attribute Details
#attributes ⇒ Sumi::Graphics::Attributes (readonly)
Returns the attribute store.
135 136 137 |
# File 'graphics/lib/sumi/graphics/element.rb', line 135 def attributes @attributes end |
#name ⇒ Symbol (readonly)
Returns the SVG element name.
131 132 133 |
# File 'graphics/lib/sumi/graphics/element.rb', line 131 def name @name end |
Class Method Details
.element(name, *arguments, parent: RootParent) { ... } ⇒ Sumi::Graphics::Element
Builds an element node.
29 30 31 32 |
# File 'graphics/lib/sumi/graphics/element.rb', line 29 def self.element(name, *arguments, parent: RootParent, &block) validate_parent_class(self, parent) new(name, **Dispatch.capture(name, *arguments), parent:, &block) end |
.root(*arguments) { ... } ⇒ Sumi::Graphics::Element
Builds an SVG root element.
41 |
# File 'graphics/lib/sumi/graphics/element.rb', line 41 def self.root(*arguments, &block) = element(:svg, *arguments, parent: RootParent, &block) |
.root?(element) ⇒ Boolean
Reports whether an element is the root element.
46 |
# File 'graphics/lib/sumi/graphics/element.rb', line 46 def self.root?(element) = Element.send(:tree_parent, element).equal?(RootParent) |
.valid?(name) ⇒ Boolean
Reports whether a candidate can dispatch as an SVG element name. With Standard loaded, the name must be known. Standalone Graphics accepts any valid XML name.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'graphics/lib/sumi/graphics/element.rb', line 17 class Element # Builds an element node. # @param name [Symbol, String] SVG element name # @param arguments [Array<Hash, String, Sumi::Graphics::Content>] ordered content and attribute channels. Every # Hash is applied through {Sumi::Graphics::Attributes} from left to right # @param parent [Sumi::Graphics::Element] parent element. Omit to build a document root # @yield evaluates the drawing DSL in the new element # @yieldreturn [Object] ignored block result # @return [Sumi::Graphics::Element] # @raise [Sumi::ArgumentError] when an argument cannot be parsed as attributes or content # @raise [Sumi::ArgumentError] when the parent has a different concrete element class # @raise [Sumi::ArgumentError] when the element name or an attribute is not valid XML def self.element(name, *arguments, parent: RootParent, &block) validate_parent_class(self, parent) new(name, **Dispatch.capture(name, *arguments), parent:, &block) end # Builds an SVG root element. # @param arguments [Array<Hash, String, Sumi::Graphics::Content>] ordered content and attribute channels # @yield evaluates the drawing DSL in the root element # @yieldreturn [Object] ignored block result # @return [Sumi::Graphics::Element] # @raise [Sumi::ArgumentError] when an argument is not a Hash, String, or Content # @raise [Sumi::ArgumentError] when a root attribute or content value is not valid XML def self.root(*arguments, &block) = element(:svg, *arguments, parent: RootParent, &block) # Reports whether an element is the root element. # @param element [Sumi::Graphics::Element] element to test # @return [Boolean] def self.root?(element) = Element.send(:tree_parent, element).equal?(RootParent) class << self private def attach(element, parent, index: nil) children = tree_children(parent) index ? children.insert(index, element) : children << element element.instance_variable_set(:@parent, parent) end def detach(element) parent = tree_parent(element) tree_children(parent).delete(element) if parent.is_a?(element.class) element.instance_variable_set(:@parent, DetachedParent) end def element_method?(name) = Dispatch.cached?(name) def tree_children(element) = element.instance_variable_get(:@children) def tree_parent(element) = element.instance_variable_get(:@parent) def validate_parent(element, parent) validate_parent_class(element.class, parent) end def validate_parent_class(element_class, parent) return if parent.equal?(RootParent) || parent.instance_of?(element_class) ArgumentError.("Element type does not match the parent type: #{element_class}") end end class << self require "sumi/standard" # Reports whether a candidate can dispatch as a known SVG element name. # @param name [Object] candidate element name # @return [Boolean] def valid?(name) Standard.element?(name) rescue Sumi::ArgumentError false end rescue ::LoadError => e raise unless e.path == "sumi/standard" # Reports whether a candidate can dispatch as a valid XML element name. # @param name [Object] candidate element name # @return [Boolean] def valid?(name) return false unless name.is_a?(::String) || name.is_a?(::Symbol) XML.name(name, context: "SVG element name") true rescue Sumi::ArgumentError false end end private_class_method :new # Sentinel parents used by root and detached elements. RootParent = Object.new.tap { def it.inspect = "RootParent" }.freeze DetachedParent = Object.new.tap { def it.inspect = "DetachedParent" }.freeze private_constant :DetachedParent, :RootParent # SVG element method-name normalization. # @api private module Ident # Normalizes a Ruby method name into an SVG element id. # @param given [Symbol, String] method name # @return [Symbol] def id(given) = (@id ||= {})[given] ||= given.to_s.tr("_", "-").to_sym end extend Ident private_class_method :id private_constant :Ident # Returns the SVG element name. # @return [Symbol] attr_reader :name # Returns the attribute store. # @return [Sumi::Graphics::Attributes] attr_reader :attributes # Returns a read-only snapshot of child elements in rendering order. # @return [Array<Sumi::Graphics::Element>] frozen child snapshot def children = @children.dup.freeze # Returns a read-only snapshot of element content objects in rendering order. # @return [Array<Sumi::Graphics::Content>] frozen content snapshot def contents = @contents.dup.freeze # Returns a concise summary of the element type, id, and child count. # @return [String] def inspect = Dispatch.summary(self, @attributes, @children) # Returns the parent element. # @return [Sumi::Graphics::Element, nil] parent element, or nil for a root or detached element # @note Use `Root?` to distinguish a document root from a detached subtree root. def parent @parent if @parent.is_a?(self.class) end # Creates an element. # @param name [Symbol] SVG element name # @param parent [Sumi::Graphics::Element, Object] parent element or root sentinel # @param attributes [Hash] SVG attributes # @param contents [Array<Sumi::Graphics::Content>] content objects # @yield evaluates the drawing DSL in the new element # @yieldreturn [Object] ignored block result # @return [void] # @raise [Sumi::ArgumentError] when the parent has a different concrete element class # @raise [Sumi::ArgumentError] when the element name or an attribute is not valid XML # @api private def initialize(name, parent:, attributes: {}, contents: [], &block) Element.send(:validate_parent, self, parent) unless name.is_a?(::String) || name.is_a?(::Symbol) ArgumentError.("XML element name must be a String or Symbol") end @name = XML.name(name, context: "XML element name").to_sym @attributes = attributes.is_a?(Attributes) ? attributes : Attributes.new(attributes) @children = [] @contents = contents.dup @parent = parent Element.send(:attach, self, parent) unless Element.root?(self) instance_exec(&block) if block end private # Copies owned storage and reconnects child copies without attaching to the source tree. # @param original [Sumi::Graphics::Element] source node # @return [void] # @api private def initialize_copy(original) super @parent = Element.root?(original) ? RootParent : DetachedParent @attributes = original.attributes.dup @contents = original.contents.map(&:dup) @children = original.children.map do |child| child.dup.tap { it.instance_variable_set(:@parent, self) } end end # Dispatches SVG element DSL calls and caches valid element methods. # @param name [Symbol] missing method name # @param arguments [Array<Hash, String, Sumi::Graphics::Content>] ordered content and attribute channels. Later # Hashes replace or update attributes assigned by earlier Hashes # @yield evaluates the drawing DSL in the dispatched child element # @yieldreturn [Object] ignored block result # @return [Sumi::Graphics::Element] # @raise [NameError] when the name is not a valid SVG element # @raise [Sumi::ArgumentError] when an argument cannot be parsed as attributes or content def method_missing(name, *arguments, &block) return super if singleton_class.private_method_defined?(name) || singleton_class.protected_method_defined?(name) Element.valid?(tag = Element.send(:id, name)) ? Dispatch.(self, name, tag, *arguments, &block) : super end # Reports whether a missing method can dispatch to an SVG element. # @param name [Symbol] queried method name # @param include_private [Boolean] standard Ruby respond_to? flag # @return [Boolean] # @api private def respond_to_missing?(name, include_private = false) return false if singleton_class.private_method_defined?(name) || singleton_class.protected_method_defined?(name) Element.valid?(Element.send(:id, name)) || super end # Element method-missing parser and cache. # @api private module Dispatch extend self MUTEX = ::Mutex.new # Dispatches an element DSL call. # @param element [Sumi::Graphics::Element] parent element # @param method [Symbol] Ruby method name # @param tag [Symbol] SVG element name # @return [Sumi::Graphics::Element] def call(element, method, tag, *, &) MUTEX.synchronize do unless Element.method_defined?(method) || Element.private_method_defined?(method) || Element.protected_method_defined?(method) Element.class_exec do define_method(method) { |*args, &block| self.class.element(tag, *args, parent: self, &block) } end (@methods ||= {})[method] = Element.instance_method(method) end end element.class.element(tag, *, parent: element, &) end def cached?(method) MUTEX.synchronize do cached = @methods&.[](method) cached && Element.method_defined?(method) && Element.instance_method(method) == cached end end # Captures element DSL arguments without copying the resulting attribute facade back into a Hash. # @param name [Symbol] SVG element name # @param args [Array<Object>] positional DSL arguments. Hashes are imported from left to right # @return [Hash] captured :attributes facade and :contents array # @raise [Sumi::ArgumentError] when an argument is not a Hash, String, or Content def capture(name, *args) attributes, contents = Attributes.new, [] args.each do |arg| case arg when ::Hash attributes.merge!(arg) when ::String contents << Content.encoded(arg) when Content contents << arg else ArgumentError.("Argument of element '#{name}' must be a Hash, String, or Content: #{arg}") end end {attributes:, contents:} end # Returns a concise summary string for Element#inspect without expanding subtrees. # @param element [Sumi::Graphics::Element] element to describe # @param attributes [Sumi::Graphics::Attributes, nil] backing attribute facade # @param children [Array<Sumi::Graphics::Element>, nil] child list # @return [String] def summary(element, attributes, children) id = (" ##{attributes[:id]}" if attributes&.has?(:id)) "#<#{element.class} <#{element.name}>#{id} children=#{children&.size || 0}>" end # Parses element DSL arguments. # @param name [Symbol] SVG element name # @param args [Array<Object>] positional DSL arguments. Hashes are imported from left to right # @return [Hash] parsed :attributes and :contents # @raise [Sumi::ArgumentError] when an argument is not a Hash, String, or Content def parse(name, *args) = capture(name, *args).tap { it[:attributes] = it[:attributes].to_h } end private_constant :Dispatch end |
Instance Method Details
#children ⇒ Array<Sumi::Graphics::Element>
Returns a read-only snapshot of child elements in rendering order.
139 |
# File 'graphics/lib/sumi/graphics/element.rb', line 139 def children = @children.dup.freeze |
#contents ⇒ Array<Sumi::Graphics::Content>
Returns a read-only snapshot of element content objects in rendering order.
143 |
# File 'graphics/lib/sumi/graphics/element.rb', line 143 def contents = @contents.dup.freeze |
#inspect ⇒ String
Returns a concise summary of the element type, id, and child count.
147 |
# File 'graphics/lib/sumi/graphics/element.rb', line 147 def inspect = Dispatch.summary(self, @attributes, @children) |
#parent ⇒ Sumi::Graphics::Element?
Use Root? to distinguish a document root from a detached subtree root.
Returns the parent element.
152 153 154 |
# File 'graphics/lib/sumi/graphics/element.rb', line 152 def parent @parent if @parent.is_a?(self.class) end |