Sha256: 6a5726b956d23f0d0d1ed669bd0b32445dbbbb102ef4dc84fea45a713154f688

Contents?: true

Size: 1.63 KB

Versions: 10

Compression:

Stored size: 1.63 KB

Contents

module Lutaml
  module LutamlPath
    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

      protected

      def serialize_document(_path)
        raise ArgumentError, "implement #serialize_document!"
      end

      def serialize_value(attr_value)
        if attr_value.is_a?(Array)
          return attr_value.map(&method(:serialize_to_hash))
        end

        serialize_to_hash(attr_value)
      end

      def serialize_to_hash(object)
        return object if [String, Integer, Float, FalseClass, TrueClass, Symbol, NilClass, Hash].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
                                        else
                                          if [String, Integer, Float, FalseClass, TrueClass, Symbol, NilClass, Hash].include?(variable.class) || var == :@parent
                                            variable
                                          else
                                            serialize_to_hash(variable)
                                          end
                                        end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
lutaml-0.7.5 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.7.4 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.7.3 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.7.2 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.7.1 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.7.0 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.6.1 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.6.0 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.5.2 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.5.1 lib/lutaml/lutaml_path/document_wrapper.rb