Sha256: 8c4efd2f586f514899dd5f82df9b9b135d1ddf51e633d382bc711137bfe461b6

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module Superstore
  module Associations
    class Reflection
      attr_reader :macro, :name, :model, :options
      def initialize(macro, name, model, options)
        @macro  = macro
        @name   = name
        @model  = model
        @options = options
      end

      def association_class
        case macro
        when :belongs_to
          Superstore::Associations::BelongsTo
        when :has_many
          Superstore::Associations::HasMany
        when :has_one
          Superstore::Associations::HasOne
        end

      end

      def instance_variable_name
        "@#{name}"
      end

      def foreign_key
        @foreign_key ||= options[:foreign_key] || derive_foreign_key
      end

      def primary_key
        options[:primary_key] || "id"
      end

      def default_primary_key?
        primary_key == "id"
      end

      def polymorphic_column
        "#{name}_type"
      end

      def polymorphic?
        options[:polymorphic]
      end

      def belongs_to?; false; end

      def class_name
        @class_name ||= (options[:class_name] || name.to_s.classify)
      end

      def inverse_name
        options[:inverse_of]
      end

      private

      def derive_foreign_key
        case macro
        when :has_many, :has_one
          model.name.foreign_key
        when :belongs_to
          "#{name}_id"
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
superstore-3.0.0 lib/superstore/associations/reflection.rb
superstore-2.5.0 lib/superstore/associations/reflection.rb