Sha256: d878ea13e1a2b4cafba8736ab3495755e0b399c3034aee343a495ad7322da9dc

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

require "active_model"

module Shamu

  # Adds `to_model_id` to several classes that are often used to look up
  # models by id.
  module ToModelIdExtension

    # @param [String,Integer,#to_model_id] value
    # @return [Boolean] true if the value looks like an ID.
    def self.model_id?( value )
      case Array( value ).first
      when Integer then true
      when String  then ToModelIdExtension::Strings::NUMERIC_PATTERN =~ value
      end
    end

    # Extend common classes to add `to_model_id` method.
    def self.extend!
      Integer.include Integers
      String.include Strings
      Array.include Enumerables
      NilClass.include Integers

      ActiveRecord::Base.include Models if defined? ActiveRecord::Base
    end

    # Add `to_model_id` to String types.
    module Strings
      NUMERIC_PATTERN = /\A\s*[0-9]+\z/

      def to_model_id
        self =~ NUMERIC_PATTERN ? to_i : nil
      end
    end

    # Add `to_model_id` to Integer types.
    module Integers
      def to_model_id
        self
      end
    end

    # Add `to_model_id` to Enumerable types.
    module Enumerables
      def to_model_id
        map( &:to_model_id )
      end
    end

    # Add `to_model_id` to ActiveModel types.
    module Models
      def to_model_id
        id
      end
    end
  end
end

Shamu::ToModelIdExtension.extend!

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shamu-0.0.24 lib/shamu/to_model_id_extension.rb
shamu-0.0.21 lib/shamu/to_model_id_extension.rb