Sha256: 0486f8055dec638917f32ad342735c906fc8e366f456cc017dcdac509ee5a958

Contents?: true

Size: 882 Bytes

Versions: 1

Compression:

Stored size: 882 Bytes

Contents

module Superstore::Associations::Builder
  class Association
    def self.build(model, name, options)
      new(model, name, options).build
    end

    attr_reader :model, :name, :options
    def initialize(model, name, options)
      @model, @name, @options = model, name, options
    end

    def build
      define_writer
      define_reader

      reflection = Superstore::Associations::Reflection.new(macro, name, model, options)
      ActiveRecord::Reflection.add_reflection model, name, reflection
    end

    def mixin
      model.generated_association_methods
    end

    def define_writer
      name = self.name
      mixin.redefine_method("#{name}=") do |records|
        association(name).writer(records)
      end
    end

    def define_reader
      name = self.name
      mixin.redefine_method(name) do
        association(name).reader
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
superstore-3.0.0 lib/superstore/associations/builder/association.rb