History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: RUBY-1516
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Critical Critical
Assignee: Roman Chernyatchik
Reporter: Roman Chernyatchik
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Ruby

Underscore and camelize methods behavior differs from rails one.

Created: 05 May 08 21:21   Updated: 23 May 08 18:47
Component/s: Rails
Fix Version/s: 1.5

Original Estimate: Unknown Remaining Estimate: Unknown Time Spent: Unknown


 Description  « Hide
Underscore and camelize methods behavior differs from rails one.
RRAR2RBbbCACYxCC -> 
should be: rrar2_r_bbb_cac_yx_cc
but in plugin: r_r_a_r2_r_bbb_c_a_c_yx_c_c


 All   Comments   Work Log   Change History      Sort Order:
Roman Chernyatchik - 05 May 08 21:25
See : /activesupport-2.0.2/lib/active_support/inflector.rb
base_name, @class_path, @file_path, @class_nesting, @class_nesting_depth = extract_modules(@name)
@class_name_without_nesting, @singular_name, @plural_name = inflect_names(base_name)


      def extract_modules(name)
          modules = name.include?('/') ? name.split('/') : name.split('::')
          name    = modules.pop
          path    = modules.map { |m| m.underscore }
          file_path = (path + [name.underscore]).join('/')
          nesting = modules.map { |m| m.camelize }.join('::')
          [name, path, file_path, nesting, modules.size]
      end
def inflect_names(name)
          camel  = name.camelize
          under  = camel.underscore
          plural = under.pluralize
          [camel, under, plural]
    end
# By default, camelize converts strings to UpperCamelCase. If the argument to camelize
        # is set to ":lower" then camelize produces lowerCamelCase.
        #
        # camelize will also convert '/' to '::' which is useful for converting paths to namespaces 
        #
        # Examples
        #   "active_record".camelize #=> "ActiveRecord"
        #   "active_record".camelize(:lower) #=> "activeRecord"
        #   "active_record/errors".camelize #=> "ActiveRecord::Errors"
        #   "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
        def camelize(first_letter = :upper)
          case first_letter
            when :upper then Inflector.camelize(self, true)
            when :lower then Inflector.camelize(self, false)
          end
        end
# By default, camelize converts strings to UpperCamelCase. If the argument to camelize
  # is set to ":lower" then camelize produces lowerCamelCase.
  #
  # camelize will also convert '/' to '::' which is useful for converting paths to namespaces
  #
  # Examples
  #   "active_record".camelize #=> "ActiveRecord"
  #   "active_record".camelize(:lower) #=> "activeRecord"
  #   "active_record/errors".camelize #=> "ActiveRecord::Errors"
  #   "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
  def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
    if first_letter_in_uppercase
      lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
    else
      lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
    end
  end
# The reverse of +camelize+. Makes an underscored form from the expression in the string.
  #
  # Changes '::' to '/' to convert namespaces to paths.
  #
  # Examples
  #   "ActiveRecord".underscore #=> "active_record"
  #   "ActiveRecord::Errors".underscore #=> active_record/errors
  def underscore(camel_cased_word)
    camel_cased_word.to_s.gsub(/::/, '/').
      gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
      gsub(/([a-z\d])([A-Z])/,'\1_\2').
      tr("-", "_").
      downcase
  end

Roman Chernyatchik - 23 May 08 18:47
done. rev.16570