Sha256: 03b2734a608013be8c5883b7202a88abacdf1472146f75b01fc301fd62b02102

Contents?: true

Size: 1.09 KB

Versions: 16

Compression:

Stored size: 1.09 KB

Contents

module Vedeu

  module Renderers

    # Renders a {Vedeu::VirtualBuffer} or {Vedeu::Output} as JSON.
    #
    class JSON < Vedeu::Renderers::File

      # Returns a new instance of Vedeu::Renderers::JSON.
      #
      # @param options [Hash]
      # @return [Vedeu::Renderers::JSON]
      def initialize(options = {})
        @options = options || {}
      end

      # @param output [Array<Array<Vedeu::Views::Char>>]
      # @return [String]
      def render(output)
        super(parsed(output))
      end

      private

      # @param output [Array<Array<Vedeu::Views::Char>>]
      # @return [String]
      def parsed(output)
        return '' if output.nil? || output.empty?

        ::JSON.pretty_generate(as_hash(output))
      end

      # @param output [Array<Array<Vedeu::Views::Char>>]
      # @return [Array]
      def as_hash(output)
        sorted(output).map(&:to_hash)
      end

      # @param output [Array<Array<Vedeu::Views::Char>>]
      # @return [Array]
      def sorted(output)
        Array(output).flatten.sort_by(&:position)
      end

    end # JSON

  end # Renderers

end # Vedeu

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
vedeu-0.6.8 lib/vedeu/output/renderers/json.rb
vedeu-0.6.7 lib/vedeu/output/renderers/json.rb
vedeu-0.6.6 lib/vedeu/output/renderers/json.rb
vedeu-0.6.5 lib/vedeu/output/renderers/json.rb
vedeu-0.6.4 lib/vedeu/output/renderers/json.rb
vedeu-0.6.3 lib/vedeu/output/renderers/json.rb
vedeu-0.6.2 lib/vedeu/output/renderers/json.rb
vedeu-0.6.1 lib/vedeu/output/renderers/json.rb
vedeu-0.6.0 lib/vedeu/output/renderers/json.rb
vedeu-0.5.13 lib/vedeu/output/renderers/json.rb
vedeu-0.5.12 lib/vedeu/output/renderers/json.rb
vedeu-0.5.11 lib/vedeu/output/renderers/json.rb
vedeu-0.5.10 lib/vedeu/output/renderers/json.rb
vedeu-0.5.9 lib/vedeu/output/renderers/json.rb
vedeu-0.5.8 lib/vedeu/output/renderers/json.rb
vedeu-0.5.7 lib/vedeu/output/renderers/json.rb