Sha256: 6233e61082101edfaecce6aafa21572de58b6ef52d48eab5183819f7a189afd5

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

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

    module ClassMethods
      def acts_in_relation(position = :self, params)
        relation = Relation.new(position, params, class_name.call)
        relation.define
      end

      private

      # TODO: Implement this method to return reliable class name
      def class_name
        -> { caller_locations(3, 1)[0].label.match(/^\<class:(\w+)\>$/)[1].downcase.to_sym }
      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

2 entries across 2 versions & 1 rubygems

Version Path
acts_in_relation-0.1.0 lib/acts_in_relation/core.rb
acts_in_relation-0.0.1 lib/acts_in_relation/core.rb