class ForemanInventoryUpload::Generators::JsonStream

Attributes

out[R]

Public Class Methods

new(out) click to toggle source
# File lib/foreman_inventory_upload/generators/json_stream.rb, line 5
def initialize(out)
  @out = out
end

Public Instance Methods

array() { || ... } click to toggle source
# File lib/foreman_inventory_upload/generators/json_stream.rb, line 9
def array
  @out << '['
  yield
  @out << ']'
end
array_field(name, last = false, &block) click to toggle source
# File lib/foreman_inventory_upload/generators/json_stream.rb, line 38
def array_field(name, last = false, &block)
  @out << "\"#{name}\": "
  array(&block)
  @out << ',' unless last
end
comma() click to toggle source
# File lib/foreman_inventory_upload/generators/json_stream.rb, line 21
def comma
  @out << ', '
end
object() { || ... } click to toggle source
# File lib/foreman_inventory_upload/generators/json_stream.rb, line 15
def object
  @out << '{'
  yield
  @out << '}'
end
object_field(name, last = false, &block) click to toggle source
# File lib/foreman_inventory_upload/generators/json_stream.rb, line 54
def object_field(name, last = false, &block)
  @out << "\"#{name}\": "
  object(&block)
  @out << ',' unless last
end
raw(string) click to toggle source
# File lib/foreman_inventory_upload/generators/json_stream.rb, line 25
def raw(string)
  @out << string
end
simple_field(name, value, last = false, &block) click to toggle source
# File lib/foreman_inventory_upload/generators/json_stream.rb, line 29
def simple_field(name, value, last = false, &block)
  return if value.nil? || value.try(:empty?)
  return if value.kind_of?(Array) && value.compact.empty?

  block ||= ->(value) { value }

  @out << "\"#{name}\": #{stringify_value(block.call(value))}#{last ? '' : ','}"
end
string_array_value(name, value, last = false) click to toggle source
# File lib/foreman_inventory_upload/generators/json_stream.rb, line 44
def string_array_value(name, value, last = false)
  return if value.empty?

  string_value = value.map { |v| stringify_value(v) }

  array_field(name, last) do
    raw(string_value.join(', '))
  end
end
stringify_value(value) click to toggle source
# File lib/foreman_inventory_upload/generators/json_stream.rb, line 60
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