Sha256: 75d17581ad7f038bd9aea8670552ecac1f686deb22c87128578df0716cfbddbe

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module RenderEditorjs
  class Document
    attr_reader :renderer, :content, :errors

    def initialize(renderer, content)
      @renderer = renderer
      @content = content.is_a?(Hash) ? content : JSON.parse(content)

      @block_renderers = {}
      @errors = []
    end

    def validate_blocks
      content["blocks"].each do |block|
        block_renderer = block_renderers(block["type"])
        next unless block_renderer

        validator = block_renderer.validator(block)
        @errors << validator.errors unless validator.valid?
      end
    end

    def block_renderers(block_type)
      @block_renderers[block_type] ||= renderer.mapping[block_type]
    end

    def validator
      @validator ||= RenderEditorjs::Validator.new(content, renderer.class::SCHEMA)
    end

    def valid?
      validator.validate!
      validate_blocks

      @errors.empty?
    rescue JSON::Schema::ValidationError => e
      @errors << e.message

      false
    end

    def render
      content["blocks"].map do |block|
        block_renderer = block_renderers(block["type"])
        next unless block_renderer

        block_renderer.render(block["data"])
      end.join
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
render_editorjs-0.1.0 lib/render_editorjs/document.rb