Sha256: e2863bd42738b5daf15ee6ca8bcfcd2e19f20e14fa81f6de69d6cee435573eb9

Contents?: true

Size: 1.08 KB

Versions: 14

Compression:

Stored size: 1.08 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::Char>>]
      # @return [String]
      def render(output)
        super(parsed(output))
      end

      private

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

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

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

      # @param output [Array<Array<Vedeu::Char>>]
      # @return [Array]
      def sorted(output)
        Array(output).flatten.sort { |a, b| a.position <=> b.position }
      end

    end # JSON

  end # Renderers

end # Vedeu

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
vedeu-0.5.4 lib/vedeu/output/renderers/json.rb
vedeu-0.5.3 lib/vedeu/output/renderers/json.rb
vedeu-0.5.2 lib/vedeu/output/renderers/json.rb
vedeu-0.5.1 lib/vedeu/output/renderers/json.rb
vedeu-0.5.0 lib/vedeu/output/renderers/json.rb
vedeu-0.4.65 lib/vedeu/output/renderers/json.rb
vedeu-0.4.64 lib/vedeu/output/renderers/json.rb
vedeu-0.4.63 lib/vedeu/output/renderers/json.rb
vedeu-0.4.62 lib/vedeu/output/renderers/json.rb
vedeu-0.4.61 lib/vedeu/output/renderers/json.rb
vedeu-0.4.60 lib/vedeu/output/renderers/json.rb
vedeu-0.4.59 lib/vedeu/output/renderers/json.rb
vedeu-0.4.58 lib/vedeu/output/renderers/json.rb
vedeu-0.4.57 lib/vedeu/output/renderers/json.rb