Sha256: f7fe9bffef184167886adcece82a7371cd60be2d7a8f91ed32df4ce2e92de47c

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

module Vedeu

  module Templating

    # Converts an object or collection of objects into an encoded
    # String.
    #
    class Encoder

      # @param (see #initialize)
      # @return [String]
      def self.process(data)
        new(data).process
      end

      # Returns a new instance of Vedeu::Templating::Encoder.
      #
      # @param data [Object]
      # @return [Vedeu::Templating::Encoder]
      def initialize(data)
        @data = data
      end

      # Converts an object or collection of objects into an encoded
      # String.
      #
      # @return [String]
      def process
        wrap
      end

      protected

      # @!attribute [r] data
      # @return [Object]
      attr_reader :data

      private

      # Adds delimiters to the start and end of the data.
      #
      # @return [String]
      def wrap
        "{{#{encode64}}}".freeze
      end

      # Encode the compressed, marshalled object or objects into a
      # Base64 string.
      #
      # @return [String]
      def encode64
        Base64.strict_encode64(compress)
      end

      # Compress the marshalled object or objects.
      #
      # @return [String]
      def compress
        Zlib::Deflate.deflate(marshal)
      end

      # Convert the object or objects into marshalled object(s).
      #
      # @return [String]
      def marshal
        Marshal.dump(data)
      end

    end # Encoder

  end # Templating

end # Vedeu

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vedeu-0.6.50 lib/vedeu/templating/encoder.rb
vedeu-0.6.49 lib/vedeu/templating/encoder.rb
vedeu-0.6.48 lib/vedeu/templating/encoder.rb