Sha256: c4dd73a7e6f93777d0033e9fa1cef673e891a666e68851e5f6c92dca46bb86bc

Contents?: true

Size: 1.56 KB

Versions: 29

Compression:

Stored size: 1.56 KB

Contents

module Lutaml
  module LutamlPath
    class DocumentWrapper
      attr_reader :serialized_document, :original_document

      def initialize(document)
        @original_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

      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

      BASE_OBJECTS = [String, Integer, Float, FalseClass, TrueClass, Symbol, NilClass, Hash]
      def serialize_to_hash(object)
        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
                                          serialize_to_hash(variable)
                                        end
        end
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
lutaml-0.9.26 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.25 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.24 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.23 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.22 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.21 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.20 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.19 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.18 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.17 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.16 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.15 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.14 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.13 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.12 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.11 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.10 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.9 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.8 lib/lutaml/lutaml_path/document_wrapper.rb
lutaml-0.9.7 lib/lutaml/lutaml_path/document_wrapper.rb