Sha256: 4beb4433da6e1c39d009ba3a9b4105bcfc735d75bb41c5522e9a5b2396976743

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Goldendocx
  module HasAssociations
    extend ActiveSupport::Concern

    included do
      class_attribute :associations, default: {}
      class_attribute :relationships_xml_path

      delegate :add_relationship, to: :relationships
    end

    class_methods do
      def relationships_at(xml_path)
        self.relationships_xml_path = xml_path
      end

      def associate(name, class_name:, isolate: false)
        named = name.to_s
        associations[named] = { class_name: class_name, isolate: isolate }

        define_method named do
          return instance_variable_get("@#{name}") if instance_variable_defined?("@#{name}")

          new_instance = class_name.constantize.new
          instance_variable_set("@#{name}", new_instance)
        end
      end
    end

    def read_associations(docx_file)
      associations.each do |association, options|
        association_class = options[:class_name].constantize
        association_document_xml = docx_file.read(association_class::XML_PATH)
        instance_variable_set("@#{association}", association_class.parse(association_document_xml))
      end
    end

    def read_relationships(docx_file)
      @relationships = Goldendocx::Models::Relationships.parse(docx_file.read(relationships_xml_path))
    end

    def write_relationships(zos)
      relationships.write_to(zos, relationships_xml_path)
    end

    def relationships
      @relationships ||= Goldendocx::Models::Relationships.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
goldendocx-0.2.3 lib/goldendocx/has_associations.rb