[Uttk-Patches] [core_ex] 446: Extend Inflector::Inflections.
Nicolas Pouillard
ertai at lrde.epita.fr
Sun Jan 1 11:22:30 EST 2006
svn://svn.feydakins.org/ruby_ex/trunk/core_ex
Index: ChangeLog
from Nicolas Pouillard <ertai at lrde.epita.fr>
Extend Inflector::Inflections.
* lib/core_ex.rb: Extend Inflector::Inflections to deal with custom
inflections (e.g. URI -> uri not u_r_i).
* lib/core_ex/string.rb: Rename import to import! to match with the
others.
core_ex.rb | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
core_ex/string.rb | 4 +--
2 files changed, 73 insertions(+), 3 deletions(-)
Index: lib/core_ex/string.rb
--- lib/core_ex/string.rb (revision 445)
+++ lib/core_ex/string.rb (working copy)
@@ -26,8 +26,8 @@
self
end
- def import
- to_path.import
+ def import!
+ to_path.import!
end
def to_path
Index: lib/core_ex.rb
--- lib/core_ex.rb (revision 445)
+++ lib/core_ex.rb (working copy)
@@ -83,8 +83,78 @@
# <<< little active_support patch
silence_warnings do
module Inflector
+ class Inflections
+ # <<<
+ attr_reader :underscore_rules, :camelize_rules
+ # >>>
+ def initialize
+ # <<<
+ clear_all
+ # >>>
+ end
+ # <<<
+ def clear_all
+ @plurals, @singulars, @uncountables, @underscore_rules, @camelize_rules = [], [], [], [], []
+ end
+ # >>>
+ def clear ( scope=:all )
+ case scope
+ when :all
+ # <<<
+ clear_all
+ # >>>
+ else
+ instance_variable_set "@#{scope}", []
+ end
+ end
+ def underscore(rule, replacement)
+ @underscore_rules.unshift [rule, replacement]
+ end
+ def camelize(rule, replacement)
+ @camelize_rules.unshift [rule, replacement]
+ end
+ def fixed_case ( camel_cased_word,
+ lower_case_and_underscored_word=camel_cased_word.downcase )
+ camelize(lower_case_and_underscored_word, camel_cased_word)
+ underscore(camel_cased_word, lower_case_and_underscored_word)
+ end
+ def fixed_cases ( *words )
+ for word in words
+ if word.is_a? Array
+ fixed_case(*word)
+ else
+ fixed_case word
+ end
+ end
+ end
+ end
+ # Since Inflections is already loaded and is a Singleton we need to initialize
+ # our variables by hand:
+ Inflections.instance.instance_eval { @underscore_rules, @camelize_rules = [], [] }
+
def underscore(camel_cased_word)
- camel_cased_word.to_s.gsub(/::/, '/').gsub(/([A-Z])/,'_\1').gsub(/(^|\/)_/, '\1').downcase
+ # <<<
+ result = camel_cased_word.to_s.split '::'
+ result.map! do |word|
+ inflections.underscore_rules.each { |(rule, replacement)| break if word.gsub!(rule, replacement) }
+ word.gsub!(/([A-Z])/, '_\1')
+ word.gsub!(/^_/, '')
+ word.downcase!
+ word
+ end
+ result.join '/'
+ # >>>
+ end
+ def camelize(lower_case_and_underscored_word)
+ # <<<
+ result = lower_case_and_underscored_word.to_s.split '/'
+ result.map! do |word|
+ inflections.camelize_rules.each { |(rule, replacement)| break if word.gsub!(rule, replacement) }
+ word.gsub!(/(^|_)(.)/) { $2.upcase }
+ word
+ end
+ result.join '::'
+ # >>>
end
end # module Inflector
end
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 186 bytes
Desc: OpenPGP digital signature
Url : http://rubyforge.org/pipermail/ttk-patches/attachments/20060101/9af04e82/signature.bin
-------------- next part --------------
_______________________________________________
Uttk-Patches mailing list
Uttk-Patches at lists.feydakins.org
http://mailman.feydakins.org/mailman/listinfo/uttk-patches
More information about the ttk-patches
mailing list