Sha256: 33e6e647d1e6e1f33d36b605e65ed5c38178100f1bedd57859b2d355c979559b

Contents?: true

Size: 1.12 KB

Versions: 35

Compression:

Stored size: 1.12 KB

Contents

module ForemanInventoryUpload
  module Generators
    class JsonStream
      attr_reader :out
      def initialize(out)
        @out = out
      end

      def array
        @out << '['
        yield
        @out << ']'
      end

      def object
        @out << '{'
        yield
        @out << '}'
      end

      def comma
        @out << ', '
      end

      def raw(string)
        @out << string
      end

      def simple_field(name, value, last = false)
        @out << "\"#{name}\": #{stringify_value(value)}#{last ? '' : ','}" unless value.nil?
      end

      def array_field(name, last = false, &block)
        @out << "\"#{name}\": "
        array(&block)
        @out << ',' unless last
      end

      def object_field(name, last = false, &block)
        @out << "\"#{name}\": "
        object(&block)
        @out << ',' unless last
      end

      def stringify_value(value)
        return value if value.is_a?(Integer)
        return value if value.is_a?(TrueClass)
        return value if value.is_a?(FalseClass)
        return value.to_json if value.is_a?(Hash)

        "\"#{value}\""
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 2 rubygems

Version Path
foreman_rh_cloud-0.9.8 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-1.0.8 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-2.0.8 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-0.9.7 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-1.0.7 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-2.0.7 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-0.9.6 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-1.0.6 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-2.0.6 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-0.9.5 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-1.0.5 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-2.0.5 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-0.9.4.1 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-1.0.4.1 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-1.0.4 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-2.0.4 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_rh_cloud-0.9.4 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_inventory_upload-2.0.4.pre.2 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_inventory_upload-2.0.4.pre.1 lib/foreman_inventory_upload/generators/json_stream.rb
foreman_inventory_upload-2.0.3 lib/foreman_inventory_upload/generators/json_stream.rb