Sha256: a2d44f10c98bb912cac192f315f71d468646c1b3080761aea65b98a1d3d994c2

Contents?: true

Size: 1.82 KB

Versions: 9

Compression:

Stored size: 1.82 KB

Contents

class String

  # Changes an underscored string into a class reference.
  def _as_class
    # classify expects self to be plural
    self.classify.constantize
  end

  # For compatibility with the Symbol extensions.
  alias :_singularize :singularize
  alias :_pluralize :pluralize
  alias :_classify :classify
end

class Symbol
  
  # Changes an underscored symbol into a class reference.
  def _as_class; self.to_s._as_class; end
  
  # Changes a plural symbol into a singular symbol.
  def _singularize; self.to_s.singularize.to_sym; end

  # Changes a singular symbol into a plural symbol.
  def _pluralize; self.to_s.pluralize.to_sym; end

  # Changes a symbol into a class name string.
  def _classify; self.to_s.classify; end
end

class Array

  # Flattens the first level of self.
  def _flatten_once
    self.inject([]){|r, el| r + Array(el)}
  end

  # Rails 1.2.3 compatibility method. Copied from http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/core_ext/array/extract_options.rb?rev=7217 
  def _extract_options!
    last.is_a?(::Hash) ? pop : {}
  end
end

class Hash

  # An implementation of select that returns a Hash.
  def _select
    Hash[*self.select do |key, value|
      yield key, value
    end._flatten_once]
  end
end

class Object

  # Returns the metaclass of self.
  def _metaclass; (class << self; self; end); end

  # Logger shortcut.
  def _logger_debug s
    s = "** has_many_polymorphs: #{s}"
    RAILS_DEFAULT_LOGGER.debug(s) if RAILS_DEFAULT_LOGGER
  end  

  # Logger shortcut.  
  def _logger_warn s
    s = "** has_many_polymorphs: #{s}"
    if RAILS_DEFAULT_LOGGER
      RAILS_DEFAULT_LOGGER.warn(s) 
    else
      $stderr.puts(s)
    end    
  end
  
end

class ActiveRecord::Base

  # Return the base class name as a string.
  def _base_class_name
    self.class.base_class.name.to_s
  end
  
end

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
johnsbrn-has_many_polymorphs-2.13.1 lib/has_many_polymorphs/support_methods.rb
johnsbrn-has_many_polymorphs-2.13.3 lib/has_many_polymorphs/support_methods.rb
johnsbrn-has_many_polymorphs-2.13 lib/has_many_polymorphs/support_methods.rb
has_many_polymorphs-2.13 lib/has_many_polymorphs/support_methods.rb
has_many_polymorphs-2.12 lib/has_many_polymorphs/support_methods.rb
has_many_polymorphs-2.11 lib/has_many_polymorphs/support_methods.rb
spree-0.6.0 vendor/plugins/has_many_polymorphs/lib/has_many_polymorphs/support_methods.rb
spree-0.7.0 vendor/plugins/has_many_polymorphs/lib/has_many_polymorphs/support_methods.rb
spree-0.7.1 vendor/plugins/has_many_polymorphs/lib/has_many_polymorphs/support_methods.rb