Class: PortableText::Serializer
- Inherits:
-
Object
- Object
- PortableText::Serializer
- Defined in:
- lib/portable_text/serializer.rb
Instance Attribute Summary collapse
-
#blocks ⇒ Object
readonly
Returns the value of attribute blocks.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#converted ⇒ Object
readonly
Returns the value of attribute converted.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Instance Method Summary collapse
-
#convert! ⇒ Object
Converts the Portable Text content into a collection of blocks converted to ruby objects along with their children and markDefs.
-
#initialize(content:, to: :html) ⇒ Serializer
constructor
A new instance of Serializer.
-
#render(**options) ⇒ Object
After conversion, the Portable Text content is serialized to the desired format.
-
#serializer ⇒ Object
The serializer is determined by the ‘to` parameter, which defaults to `:html`.
Constructor Details
#initialize(content:, to: :html) ⇒ Serializer
Returns a new instance of Serializer.
5 6 7 8 9 10 |
# File 'lib/portable_text/serializer.rb', line 5 def initialize(content:, to: :html) @content = content @blocks = [] @to = to @converted = false end |
Instance Attribute Details
#blocks ⇒ Object (readonly)
Returns the value of attribute blocks.
3 4 5 |
# File 'lib/portable_text/serializer.rb', line 3 def blocks @blocks end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
3 4 5 |
# File 'lib/portable_text/serializer.rb', line 3 def content @content end |
#converted ⇒ Object (readonly)
Returns the value of attribute converted.
3 4 5 |
# File 'lib/portable_text/serializer.rb', line 3 def converted @converted end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
3 4 5 |
# File 'lib/portable_text/serializer.rb', line 3 def to @to end |
Instance Method Details
#convert! ⇒ Object
Converts the Portable Text content into a collection of blocks converted to ruby objects along with their children and markDefs. Object parameters are symbolized and camelCase keys are converted to snake_case. This method is idempotent.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/portable_text/serializer.rb', line 23 def convert! return if converted content.each do |block_params| params = block_params.transform_keys(&:to_sym) params[:children] = create_children(params[:children]) params[:markDefs] = create_mark_defs(params[:markDefs]) block = block_klass(params.fetch(:_type)).new(**params) add_block(block) end @converted = true end |
#render(**options) ⇒ Object
After conversion, the Portable Text content is serialized to the desired format.
13 14 15 16 17 |
# File 'lib/portable_text/serializer.rb', line 13 def render(**) convert! serializer.new(blocks).content(**) end |
#serializer ⇒ Object
The serializer is determined by the ‘to` parameter, which defaults to `:html`. The serializer must be defined in the PortableText configuration.
40 41 42 |
# File 'lib/portable_text/serializer.rb', line 40 def serializer config.serializers.fetch(to) { raise Errors::UnknownSerializerError } end |