Sha256: 1985c677152b6d02430015258c87c11edfb6ef82d15f5bf65932167644eb1ff8

Contents?: true

Size: 978 Bytes

Versions: 3

Compression:

Stored size: 978 Bytes

Contents

module Json
  module Streamer
    class Callbacks
      attr_reader :aggregator

      def initialize(aggregator)
        @aggregator = aggregator
      end

      def start_object
        new_level(Hash.new)
      end

      def start_array
        new_level(Array.new)
      end

      def key(k, symbolize_keys)
        @aggregator.key = symbolize_keys ? k.to_sym : k
      end

      def value(value)
        used = yield value
        add_value(value) unless used
      end

      def end_object
        end_level { |obj| yield obj }
      end

      def end_array
        end_level { |obj| yield obj }
      end

      private

      def end_level
        data = @aggregator.value.clone

        @aggregator.pop

        used = yield data
        add_value(data) unless used or @aggregator.empty?
      end

      def add_value(value)
        @aggregator.value = value
      end

      def new_level(type)
        @aggregator.push(value: type)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
json-streamer-2.1.0 lib/json/streamer/callbacks.rb
json-streamer-2.0.1 lib/json/streamer/callbacks.rb
json-streamer-2.0.0 lib/json/streamer/callbacks.rb