Sha256: bf3f7fb17217f8757218fc13c29dfa79eb8a8228f966f9e7c3e10d1628399380

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

require 'caller_class'

module ActsInRelation
  module Core
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      include CallerClass

      def acts_in_relation(position = :self, params)
        relation = Relation.new(position, params, caller_class.downcase)
        relation.define
      end
    end

    class Relation
      def initialize(position, params, class_name)
        @position   = position
        @params     = params
        @class_name = class_name
      end

      def define
        positions = (@position == :self) ? [:source, :target] : [@position]
        positions.each do |position|
          extend "ActsInRelation::#{position.capitalize}".constantize
          define
        end
      end

      private

      def source
        @source ||= @params[:source] || @class_name
      end

      def target
        @target ||= @params[:target] || @class_name
      end

      # TODO: Return instance if exists
      def with
        with = @params[:with]
        with = with.kind_of?(Array) ? with : [with]
        with.map(&:to_s)
      end

      def class_object
        @class_object ||= @class_name.to_s.capitalize.constantize
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts_in_relation-0.1.1 lib/acts_in_relation/core.rb