Sha256: 099b9febab154b7c81a807bd3e4fb041b7a385b108e8a93fa8d7f0b9f9639d31

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module EdgeRider
  module CollectIds

    class Uncollectable < StandardError; end

    module Array

      def collect_ids
        collect do |obj|
          case obj
          when Integer
            obj
          when ActiveRecord::Base
            obj.id
          when String
            if obj.match(/\A\d+\z/)
              obj.to_i
            else
              raise Uncollectable, "Cannot collect an id from #{obj.inspect}"
            end
          else
            raise Uncollectable, "Cannot collect an id from #{obj.inspect}"
          end
        end
      end

    end

    ::Array.send(:include, Array)

    module ActiveRecordValue

      def collect_ids
        [id]
      end

    end

    ::ActiveRecord::Base.send(:include, ActiveRecordValue)

    module ActiveRecordScope

      def collect_ids
        collect_column(:id)
      end

    end

    ::ActiveRecord::Base.send(:extend, ActiveRecordScope)
    ::ActiveRecord::Associations::HasManyAssociation.send(:include, ActiveRecordScope)
    ::ActiveRecord::Associations::HasManyThroughAssociation.send(:include, ActiveRecordScope)

    module Integer

      def collect_ids
        [self]
      end

    end

    ::Integer.send(:include, Integer)

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
edge_rider-2.3.0 lib/edge_rider/collect_ids.rb