Sha256: bbc1a6a40e514e8bfa909f37175c1a61ba8bcc2a46bae42ee07d7505d1e14168

Contents?: true

Size: 1.09 KB

Versions: 16

Compression:

Stored size: 1.09 KB

Contents

module Vedeu

  module Renderers

    # Renders a {Vedeu::Buffers::Terminal} 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

      # Render a cleared output.
      #
      # @return [String]
      def clear
        json = parse({})

        super(json, { compression: false })

        json
      end

      # @param output [Vedeu::Models::Page]
      # @return [String]
      def render(output)
        json = parse(output)

        super(json, { compression: false })

        json
      end

      private

      # @param output [Vedeu::Models::Page]
      # @return [String]
      def parse(output)
        ::JSON.pretty_generate(as_hash(output))
      end

      # @param output [Vedeu::Models::Page]
      # @return [Array]
      def as_hash(output)
        return output if output.is_a?(Hash)

        output.content.map(&:to_hash)
      end

    end # JSON

  end # Renderers

end # Vedeu

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
vedeu-0.8.0 lib/vedeu/renderers/json.rb
vedeu-0.7.4 lib/vedeu/renderers/json.rb
vedeu-0.7.3 lib/vedeu/renderers/json.rb
vedeu-0.7.2 lib/vedeu/renderers/json.rb
vedeu-0.7.1 lib/vedeu/renderers/json.rb
vedeu-0.7.0 lib/vedeu/renderers/json.rb
vedeu-0.6.71 lib/vedeu/renderers/json.rb
vedeu-0.6.70 lib/vedeu/renderers/json.rb
vedeu-0.6.69 lib/vedeu/output/renderers/json.rb
vedeu-0.6.68 lib/vedeu/output/renderers/json.rb
vedeu-0.6.67 lib/vedeu/output/renderers/json.rb
vedeu-0.6.66 lib/vedeu/output/renderers/json.rb
vedeu-0.6.65 lib/vedeu/output/renderers/json.rb
vedeu-0.6.64 lib/vedeu/output/renderers/json.rb
vedeu-0.6.63 lib/vedeu/output/renderers/json.rb
vedeu-0.6.62 lib/vedeu/output/renderers/json.rb