Sha256: 404571c5526a95a4f69d3a57087cbf2f5354f66390d3c093284c075be6c36f32

Contents?: true

Size: 868 Bytes

Versions: 2

Compression:

Stored size: 868 Bytes

Contents

# frozen_string_literal: true

module ForwardCalendar
  class Node
    def to_hash
      hash = {}
      attributes = self.class.instance_variable_get(:@attributes)
      elements = self.class.instance_variable_get(:@elements)
      content = retrieve_content_tag
      (attributes.to_a + elements.to_a + content.to_a).each do |k, _|
        value = send(k)
        hash[k] = serialize_attribute(value)
      end
      hash
    end

    private

    def retrieve_content_tag
      content = self.class.instance_variable_get(:@content)
      return {} if content.nil?
      { content.name.to_sym => content }
    end

    def serialize_attribute(attribute)
      if attribute.is_a?(Array)
        attribute.map { |a| serialize_attribute(a) }
      elsif attribute.respond_to?(:to_hash)
        attribute.to_hash
      else
        attribute
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
forward-calendar-0.9.0 lib/forward_calendar/node.rb
forward-calendar-0.8.2 lib/forward_calendar/node.rb