Class: ForemanInventoryUpload::Generators::JsonStream
- Inherits:
-
Object
- Object
- ForemanInventoryUpload::Generators::JsonStream
- Defined in:
- lib/foreman_inventory_upload/generators/json_stream.rb
Instance Attribute Summary collapse
-
#out ⇒ Object
readonly
Returns the value of attribute out.
Instance Method Summary collapse
- #array ⇒ Object
- #array_field(name, last = false, &block) ⇒ Object
- #comma ⇒ Object
-
#initialize(out) ⇒ JsonStream
constructor
A new instance of JsonStream.
- #object ⇒ Object
- #object_field(name, last = false, &block) ⇒ Object
- #raw(string) ⇒ Object
- #simple_field(name, value, last = false, &block) ⇒ Object
- #string_array_value(name, value, last = false) ⇒ Object
- #stringify_value(value) ⇒ Object
Constructor Details
#initialize(out) ⇒ JsonStream
Returns a new instance of JsonStream.
5 6 7 |
# File 'lib/foreman_inventory_upload/generators/json_stream.rb', line 5 def initialize(out) @out = out end |
Instance Attribute Details
#out ⇒ Object (readonly)
Returns the value of attribute out.
4 5 6 |
# File 'lib/foreman_inventory_upload/generators/json_stream.rb', line 4 def out @out end |
Instance Method Details
#array ⇒ Object
9 10 11 12 13 |
# File 'lib/foreman_inventory_upload/generators/json_stream.rb', line 9 def array @out << '[' yield @out << ']' end |
#array_field(name, last = false, &block) ⇒ Object
38 39 40 41 42 |
# 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 ⇒ Object
21 22 23 |
# File 'lib/foreman_inventory_upload/generators/json_stream.rb', line 21 def comma @out << ', ' end |
#object ⇒ Object
15 16 17 18 19 |
# File 'lib/foreman_inventory_upload/generators/json_stream.rb', line 15 def object @out << '{' yield @out << '}' end |
#object_field(name, last = false, &block) ⇒ Object
54 55 56 57 58 |
# 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) ⇒ Object
25 26 27 |
# File 'lib/foreman_inventory_upload/generators/json_stream.rb', line 25 def raw(string) @out << string end |
#simple_field(name, value, last = false, &block) ⇒ Object
29 30 31 32 33 34 35 36 |
# 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) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# 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) ⇒ Object
60 61 62 63 64 65 66 67 |
# 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) ActiveSupport::JSON.encode(value) end |