Sha256: e7deb0c0a70bcfa481fce4d444c470d9769f126513cd0bbc2d71f6fc43842542
Contents?: true
Size: 1.02 KB
Versions: 6
Compression:
Stored size: 1.02 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 # 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
6 entries across 6 versions & 1 rubygems