module Caracal::Core::Relationships
This module encapsulates all the functionality related to registering and retrieving relationships.
Public Class Methods
default_relationships()
click to toggle source
# File lib/caracal/core/relationships.rb, line 26 def self.default_relationships [ { target: 'fontTable.xml', type: :font }, { target: 'header1.xml', type: :header }, { target: 'footer1.xml', type: :footer }, { target: 'numbering.xml', type: :numbering }, { target: 'settings.xml', type: :setting }, { target: 'styles.xml', type: :style } ] end
included(base)
click to toggle source
# File lib/caracal/core/relationships.rb, line 12 def self.included(base) base.class_eval do #------------------------------------------------------------- # Configuration #------------------------------------------------------------- attr_reader :relationship_counter #------------------------------------------------------------- # Class Methods #------------------------------------------------------------- def self.default_relationships [ { target: 'fontTable.xml', type: :font }, { target: 'header1.xml', type: :header }, { target: 'footer1.xml', type: :footer }, { target: 'numbering.xml', type: :numbering }, { target: 'settings.xml', type: :setting }, { target: 'styles.xml', type: :style } ] end #------------------------------------------------------------- # Public Methods #------------------------------------------------------------- #============== ATTRIBUTES ========================== def relationship(options={}, &block) id = relationship_counter.to_i + 1 options.merge!({ id: id }) model = Caracal::Core::Models::RelationshipModel.new(options, &block) if model.valid? @relationship_counter = id rel = register_relationship(model) else raise Caracal::Errors::InvalidModelError, 'relationship must specify the :id, :target, and :type attributes.' end rel end #============== GETTERS ============================= def relationships @relationships ||= [] end def find_relationship(target) relationships.find { |r| r.matches?(target) } end #============== REGISTRATION ======================== def register_relationship(model) unless (r = find_relationship(model.relationship_target)) relationships << model r = model end r end def unregister_relationship(target) if (r = find_relationship(target)) relationships.delete(r) end end end end
Public Instance Methods
find_relationship(target)
click to toggle source
# File lib/caracal/core/relationships.rb, line 65 def find_relationship(target) relationships.find { |r| r.matches?(target) } end
register_relationship(model)
click to toggle source
relationship(options={}, &block)
click to toggle source
ATTRIBUTES ==========================¶ ↑
# File lib/caracal/core/relationships.rb, line 44 def relationship(options={}, &block) id = relationship_counter.to_i + 1 options.merge!({ id: id }) model = Caracal::Core::Models::RelationshipModel.new(options, &block) if model.valid? @relationship_counter = id rel = register_relationship(model) else raise Caracal::Errors::InvalidModelError, 'relationship must specify the :id, :target, and :type attributes.' end rel end
relationships()
click to toggle source
unregister_relationship(target)
click to toggle source
# File lib/caracal/core/relationships.rb, line 80 def unregister_relationship(target) if (r = find_relationship(target)) relationships.delete(r) end end