lib/lutaml/lutaml_path/document_wrapper.rb in lutaml-0.7.5 vs lib/lutaml/lutaml_path/document_wrapper.rb in lutaml-0.8.0

- old
+ new

@@ -3,17 +3,20 @@ class DocumentWrapper attr_reader :serialized_document, :original_document def initialize(document) @original_document = document - @serialized_document = serialize_document(document) end def to_liquid serialized_document end + def serialized_document + @serialized_document ||= serialize_document(@original_document) + end + protected def serialize_document(_path) raise ArgumentError, "implement #serialize_document!" end @@ -24,24 +27,23 @@ end serialize_to_hash(attr_value) end + BASE_OBJECTS = [String, Integer, Float, FalseClass, TrueClass, Symbol, NilClass, Hash] def serialize_to_hash(object) - return object if [String, Integer, Float, FalseClass, TrueClass, Symbol, NilClass, Hash].include?(object.class) + return object if BASE_OBJECTS.include?(object.class) object.instance_variables.each_with_object({}) do |var, res| variable = object.instance_variable_get(var) res[var.to_s.gsub("@", "")] = if variable.is_a?(Array) variable.map do |n| serialize_to_hash(n) end + elsif BASE_OBJECTS.include?(variable.class) || var == :@parent + variable else - if [String, Integer, Float, FalseClass, TrueClass, Symbol, NilClass, Hash].include?(variable.class) || var == :@parent - variable - else - serialize_to_hash(variable) - end + serialize_to_hash(variable) end end end end end