Sha256: 06a44efac50ecc0e822122892dee1e634609cdfa0a5d5cb228d424165fd9fb21

Contents?: true

Size: 973 Bytes

Versions: 18

Compression:

Stored size: 973 Bytes

Contents

module Superstore
  module BelongsTo
    class Builder
      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::BelongsTo::Reflection.new(model, name, options)
        model.belongs_to_reflections = model.belongs_to_reflections.merge(name => reflection)
      end

      def mixin
        model.generated_belongs_to_methods
      end

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

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

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
superstore-1.2.0 lib/superstore/belongs_to/builder.rb
superstore-1.1.4 lib/superstore/belongs_to/builder.rb
superstore-1.1.3 lib/superstore/belongs_to/builder.rb
superstore-1.1.2 lib/superstore/belongs_to/builder.rb
superstore-1.1.1 lib/superstore/belongs_to/builder.rb
superstore-1.1.0 lib/superstore/belongs_to/builder.rb
superstore-1.0.12 lib/superstore/belongs_to/builder.rb
superstore-1.0.11 lib/superstore/belongs_to/builder.rb
superstore-1.0.10 lib/superstore/belongs_to/builder.rb
superstore-1.0.9 lib/superstore/belongs_to/builder.rb
superstore-1.0.8 lib/superstore/belongs_to/builder.rb
superstore-1.0.7 lib/superstore/belongs_to/builder.rb
superstore-1.0.6 lib/superstore/belongs_to/builder.rb
superstore-1.0.5 lib/superstore/belongs_to/builder.rb
superstore-1.0.4 lib/superstore/belongs_to/builder.rb
superstore-1.0.3 lib/superstore/belongs_to/builder.rb
superstore-1.0.2 lib/superstore/belongs_to/builder.rb
superstore-1.0.0 lib/superstore/belongs_to/builder.rb