Sha256: 9234f07d4ead1b741f7ceb4d8645a5df31e43b71790cc993322c93ed04a79cff

Contents?: true

Size: 1.08 KB

Versions: 11

Compression:

Stored size: 1.08 KB

Contents

module ActiveFedora::Associations::Builder
  class Contains < SingularAssociation #:nodoc:
    self.macro = :contains
    self.valid_options += [:autocreate, :block]

    def initialize(model, name, options)
      super
      options[:class_name] = 'ActiveFedora::File' if options[:class_name].blank?
    end

    def validate_options
      super
      return unless options[:class_name] && !options[:class_name].is_a?(String)
      raise ArgumentError, ":class_name must be a string for contains '#{name}'"
    end

    def self.define_readers(mixin, name)
      mixin.send(:define_method, name) do |*params|
        association(name).reader(*params).tap do |file|
          set_uri = uri.is_a?(RDF::URI) ? uri.value.present? : uri.present?
          if set_uri
            file_uri = "#{uri}/#{name}"
            begin
              file.uri = file_uri
            rescue ActiveFedora::AlreadyPersistedError
            end
          end
          if file.respond_to?(:exists!)
            file.exists! if contains_assertions.include?(file_uri)
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
active-fedora-9.7.3 lib/active_fedora/associations/builder/contains.rb
active-fedora-9.7.2 lib/active_fedora/associations/builder/contains.rb
active-fedora-9.9.1 lib/active_fedora/associations/builder/contains.rb
active-fedora-9.9.0 lib/active_fedora/associations/builder/contains.rb
active-fedora-9.8.2 lib/active_fedora/associations/builder/contains.rb
active-fedora-9.8.1 lib/active_fedora/associations/builder/contains.rb
active-fedora-9.8.0 lib/active_fedora/associations/builder/contains.rb
active-fedora-9.7.1 lib/active_fedora/associations/builder/contains.rb
active-fedora-9.7.0 lib/active_fedora/associations/builder/contains.rb
active-fedora-9.6.2 lib/active_fedora/associations/builder/contains.rb
active-fedora-9.6.1 lib/active_fedora/associations/builder/contains.rb