Class: CacheCrispies::JsonBuilder
- Inherits:
-
HashBuilder
- Object
- HashBuilder
- CacheCrispies::JsonBuilder
- Defined in:
- lib/cache_crispies/json_builder.rb
Overview
Builds out a JSON string directly with Oj::StringWriter
Instance Method Summary collapse
-
#call(json_writer, flat: false) ⇒ Oj::StringWriter
Builds the JSON string with Oj::StringWriter.
Methods inherited from HashBuilder
Constructor Details
This class inherits a constructor from CacheCrispies::HashBuilder
Instance Method Details
#call(json_writer, flat: false) ⇒ Oj::StringWriter
Builds the JSON string with Oj::StringWriter
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/cache_crispies/json_builder.rb', line 9 def call(json_writer, flat: false) if @serializer.model.nil? json_writer.push_value(nil) return end json_writer.push_object unless flat last_nesting = [] serializer.attributes_by_nesting.each do |nesting, attributes| prefix_length = common_prefix_length(last_nesting, nesting) (last_nesting.length - prefix_length).times do json_writer.pop end nesting[prefix_length..-1].each do |key| json_writer.push_object(key.to_s) end attributes.each do |attrib| write_attribute(json_writer, attrib) if show?(attrib) end last_nesting = nesting end last_nesting.each do json_writer.pop end json_writer.pop unless flat json_writer end |