Class: Slamdown::Conversion::NormalisedNode

Inherits:
Object
  • Object
show all
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

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_hObject

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.

Raises:

  • (ArgumentError)


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