Class: Slamdown::Conversion::NormalisedNode
- Inherits:
-
Object
- Object
- Slamdown::Conversion::NormalisedNode
- Defined in:
- lib/slamdown/conversion/normalised_node.rb
Overview
Represents one immutable, renderer-independent construct after element policy and corrective normalisation have been applied.
Constant Summary collapse
- ATTRIBUTES =
[:kind, :source_name, :semantic_kind, :category, :value, :attributes, :classes, :id, :children, :properties, :semantic_values, :representation, :synthetic, :source_path].freeze
Instance Method Summary collapse
-
#initialize(values) ⇒ NormalisedNode
constructor
A new instance of NormalisedNode.
-
#to_h ⇒ Object
Provides a stable plain-Ruby projection for focused normalisation tests and later rendering diagnostics.
-
#with(changes) ⇒ Object
Creates a changed immutable node without making transformation code repeat every unchanged field.
Constructor Details
#initialize(values) ⇒ NormalisedNode
Returns a new instance of NormalisedNode.
12 13 14 15 16 17 |
# File 'lib/slamdown/conversion/normalised_node.rb', line 12 def initialize(values) ATTRIBUTES.each do |attribute| instance_variable_set("@#{attribute}", Immutable.copy(values.fetch(attribute))) end freeze end |
Instance Method Details
#to_h ⇒ Object
Provides a stable plain-Ruby projection for focused normalisation tests and later rendering diagnostics.
31 32 33 34 35 36 |
# File 'lib/slamdown/conversion/normalised_node.rb', line 31 def to_h ATTRIBUTES.each_with_object({}) do |attribute, result| value = public_send(attribute) result[attribute] = attribute == :children ? value.map(&:to_h) : value end end |
#with(changes) ⇒ Object
Creates a changed immutable node without making transformation code repeat every unchanged field.
20 21 22 23 24 25 26 27 28 |
# File 'lib/slamdown/conversion/normalised_node.rb', line 20 def with(changes) unknown = changes.keys - ATTRIBUTES raise ArgumentError, "unknown normalised-node attributes: #{unknown.map(&:inspect).join(', ')}" unless unknown.empty? values = ATTRIBUTES.each_with_object({}) do |attribute, result| result[attribute] = changes.key?(attribute) ? changes[attribute] : public_send(attribute) end self.class.new(values) end |