Sha256: a0f21ed131b5ddf7eae5ed2ca1e9af1683418488c111f0f8dd462a297a79cf4e

Contents?: true

Size: 942 Bytes

Versions: 3

Compression:

Stored size: 942 Bytes

Contents

require 'caller_class'

module ActsInRelation
  module Roles
    class Base
      include CallerClass
      include ActsInRelation::Supports::Verb

      def initialize(args)
        @class = caller_class.constantize
        @args = recursive_to_s(args)
      end

      def source
        @source ||= @args[:source] || @args[:self] || @class.to_s.downcase
      end

      def target
        @target ||= @args[:target] || @args[:self] || @class.to_s.downcase
      end

      def actions
        @actions ||= [@args[:action]].flatten
      end

      def define
        raise NotImplementedError
      end

      private

      def recursive_to_s(object)
        case object
        when Hash
          object.each do |k, v|
            object[k] = recursive_to_s(v)
          end
        when Array
          object.map { |o| recursive_to_s(o) }
        else
          object.to_s unless object.nil?
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
acts_in_relation-0.2.2 lib/acts_in_relation/roles/base.rb
acts_in_relation-0.2.1 lib/acts_in_relation/roles/base.rb
acts_in_relation-0.2.0 lib/acts_in_relation/roles/base.rb