Sha256: c9f259e5b805113b9219b38e9209e0c91536ea187f63a223313e46dcc0e758d8

Contents?: true

Size: 905 Bytes

Versions: 2

Compression:

Stored size: 905 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)
      model.association_reflections = model.association_reflections.merge(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

2 entries across 2 versions & 1 rubygems

Version Path
superstore-2.0.1 lib/superstore/associations/builder/association.rb
superstore-2.0.0 lib/superstore/associations/builder/association.rb