Sha256: 1ec59c4e0d47aae508963bdbd70764cb3b31c6d39fcdd89019d3014749a56e29

Contents?: true

Size: 1.22 KB

Versions: 33

Compression:

Stored size: 1.22 KB

Contents

module ActiveFedora::Associations::Builder
  class Association #:nodoc:
    class_attribute :valid_options
    self.valid_options = [:class_name, :property]

    # Set by subclasses
    class_attribute :macro

    attr_reader :model, :name, :options, :reflection, :mixin

    def self.build(model, name, options)
      new(model, name, options).build
    end

    def initialize(model, name, options)
      @model, @name, @options = model, name, options
      @mixin = Module.new
      @model.__send__(:include, @mixin)
    end

    def build
      validate_options
      reflection = model.create_reflection(self.class.macro, name, options, model)
      define_accessors
      reflection
    end

    private

      def validate_options
        options.assert_valid_keys(self.class.valid_options)
      end

      def define_accessors
        define_readers
        define_writers
      end

      def define_readers
        name = self.name

        mixin.send(:define_method, name) do |*params|
          association(name).reader(*params)
        end
      end

      def define_writers
        name = self.name

        mixin.send(:define_method, "#{name}=") do |value|
          association(name).writer(value)
        end
      end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
active-fedora-8.7.0 lib/active_fedora/associations/builder/association.rb
active-fedora-8.6.0 lib/active_fedora/associations/builder/association.rb
active-fedora-8.5.0 lib/active_fedora/associations/builder/association.rb
active-fedora-8.4.2 lib/active_fedora/associations/builder/association.rb
active-fedora-8.4.1 lib/active_fedora/associations/builder/association.rb
active-fedora-7.3.1 lib/active_fedora/associations/builder/association.rb
active-fedora-7.3.0 lib/active_fedora/associations/builder/association.rb
active-fedora-8.4.0 lib/active_fedora/associations/builder/association.rb
active-fedora-8.3.0 lib/active_fedora/associations/builder/association.rb
active-fedora-8.2.2 lib/active_fedora/associations/builder/association.rb
active-fedora-8.2.1 lib/active_fedora/associations/builder/association.rb
active-fedora-8.2.0 lib/active_fedora/associations/builder/association.rb
active-fedora-7.2.0 lib/active_fedora/associations/builder/association.rb
active-fedora-8.1.0 lib/active_fedora/associations/builder/association.rb
active-fedora-8.0.1 lib/active_fedora/associations/builder/association.rb
active-fedora-8.0.0 lib/active_fedora/associations/builder/association.rb
active-fedora-8.0.0.rc3 lib/active_fedora/associations/builder/association.rb
active-fedora-8.0.0.rc2 lib/active_fedora/associations/builder/association.rb
active-fedora-8.0.0.rc1 lib/active_fedora/associations/builder/association.rb
active-fedora-7.1.2 lib/active_fedora/associations/builder/association.rb