Active Support 屈折变化
此类的单例实例由 Inflector.inflections
生成,可用于指定其他屈折变化规则。如果传递可选的语言环境,则可以指定其他语言的规则。默认语言环境是 :en
。仅提供英语规则。
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.plural /^(ox)$/i, '\1\2en'
inflect.singular /^(ox)en/i, '\1'
inflect.irregular 'cactus', 'cacti'
inflect.uncountable 'equipment'
end
新规则添加到顶部。因此,在上面的示例中,仙人掌的非规则规则现在将是运行的复数化和单数化规则中的第一个。这保证了您的规则在可能已加载的任何规则之前运行。
- A
- C
- H
- I
- N
- P
- S
- U
属性
[R] | acronyms | |
[R] | humans | |
[R] | plurals | |
[R] | singulars | |
[R] | uncountables |
类公共方法
instance(locale = :en) 链接
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/inflector/inflections.rb, line 65 def self.instance(locale = :en) @__instance__[locale] ||= new end
instance_or_fallback(locale) 链接
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/inflector/inflections.rb, line 69 def self.instance_or_fallback(locale) I18n.fallbacks[locale].each do |k| return @__instance__[k] if @__instance__.key?(k) end instance(locale) end
new() 链接
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/inflector/inflections.rb, line 80 def initialize @plurals, @singulars, @uncountables, @humans, @acronyms = [], [], Uncountables.new, [], {} define_acronym_regex_patterns end
实例公共方法
acronym(word) 链接
指定一个新的缩略词。缩略词必须按其在驼峰式字符串中出现的形式指定。包含缩略词的下划线字符串在传递给 camelize
、humanize
或 titleize
时将保留缩略词。包含缩略词的驼峰式字符串在标题化或人性化时将保留缩略词,并在传递给 underscore
时将缩略词转换为非分隔的单个小写单词。
acronym 'HTML'
titleize 'html' # => 'HTML'
camelize 'html' # => 'HTML'
underscore 'MyHTML' # => 'my_html'
但是,缩略词必须作为分隔的单元出现,而不是作为另一个单词的一部分,以便转换识别它。
acronym 'HTTP'
camelize 'my_http_delimited' # => 'MyHTTPDelimited'
camelize 'https' # => 'Https', not 'HTTPs'
underscore 'HTTPS' # => 'http_s', not 'https'
acronym 'HTTPS'
camelize 'https' # => 'HTTPS'
underscore 'HTTPS' # => 'https'
注意:传递给 pluralize
的缩略词将不再被识别,因为缩略词在复数结果中不会作为分隔的单元出现。为了解决这个问题,您必须也指定复数形式作为缩略词。
acronym 'API'
camelize(pluralize('api')) # => 'Apis'
acronym 'APIs'
camelize(pluralize('api')) # => 'APIs'
acronym
可用于指定包含缩略词或以其他方式需要保持非标准大小写的任何单词。唯一的限制是该单词必须以大写字母开头。
acronym 'RESTful'
underscore 'RESTful' # => 'restful'
underscore 'RESTfulController' # => 'restful_controller'
titleize 'RESTfulController' # => 'RESTful Controller'
camelize 'restful' # => 'RESTful'
camelize 'restful_controller' # => 'RESTfulController'
acronym 'McDonald'
underscore 'McDonald' # => 'mcdonald'
camelize 'mcdonald' # => 'McDonald'
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/inflector/inflections.rb, line 142 def acronym(word) @acronyms[word.downcase] = word define_acronym_regex_patterns end
clear(scope = :all) 链接
清除给定范围内的已加载的词形变化(默认值为 :all
)。将范围作为词形变化类型的符号给出,选项包括::plurals
、:singulars
、:uncountables
、:humans
、:acronyms
。
clear :all
clear :plurals
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/inflector/inflections.rb, line 231 def clear(scope = :all) case scope when :all clear(:acronyms) clear(:plurals) clear(:singulars) clear(:uncountables) clear(:humans) when :acronyms @acronyms = {} define_acronym_regex_patterns when :uncountables @uncountables = Uncountables.new when :plurals, :singulars, :humans instance_variable_set "@#{scope}", [] end end
human(rule, replacement) 链接
通过正则表达式规则或字符串映射指定字符串的人性化形式。使用基于正则表达式的替换时,在替换之后调用正常的人性化格式。使用字符串时,应按需要指定人性化形式(例如:‘The name’,而不是 ‘the_name’)。
human /_cnt$/i, '\1_count'
human 'legacy_col_person_name', 'Name'
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/inflector/inflections.rb, line 220 def human(rule, replacement) @humans.prepend([rule, replacement]) end
irregular(singular, plural) 链接
指定一个新的不规则词,同时适用于复数和单数形式。这只能用于字符串,不能用于正则表达式。你只需要传入单数和复数形式的不规则词。
irregular 'cactus', 'cacti'
irregular 'person', 'people'
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/inflector/inflections.rb, line 174 def irregular(singular, plural) @uncountables.delete(singular) @uncountables.delete(plural) s0 = singular[0] srest = singular[1..-1] p0 = plural[0] prest = plural[1..-1] if s0.upcase == p0.upcase plural(/(#{s0})#{srest}$/i, '\1' + prest) plural(/(#{p0})#{prest}$/i, '\1' + prest) singular(/(#{s0})#{srest}$/i, '\1' + srest) singular(/(#{p0})#{prest}$/i, '\1' + srest) else plural(/#{s0.upcase}(?i)#{srest}$/, p0.upcase + prest) plural(/#{s0.downcase}(?i)#{srest}$/, p0.downcase + prest) plural(/#{p0.upcase}(?i)#{prest}$/, p0.upcase + prest) plural(/#{p0.downcase}(?i)#{prest}$/, p0.downcase + prest) singular(/#{s0.upcase}(?i)#{srest}$/, s0.upcase + srest) singular(/#{s0.downcase}(?i)#{srest}$/, s0.downcase + srest) singular(/#{p0.upcase}(?i)#{prest}$/, s0.upcase + srest) singular(/#{p0.downcase}(?i)#{prest}$/, s0.downcase + srest) end end
plural(rule, replacement) 链接
指定一个新的复数化规则及其替换。规则可以是字符串或正则表达式。替换应该始终是一个字符串,可以包含对规则匹配数据引用的引用。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/inflector/inflections.rb, line 151 def plural(rule, replacement) @uncountables.delete(rule) if rule.is_a?(String) @uncountables.delete(replacement) @plurals.prepend([rule, replacement]) end
singular(rule, replacement) 链接
指定一个新的单数化规则及其替换。规则可以是字符串或正则表达式。替换应该始终是一个字符串,可以包含对规则匹配数据引用的引用。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/inflector/inflections.rb, line 161 def singular(rule, replacement) @uncountables.delete(rule) if rule.is_a?(String) @uncountables.delete(replacement) @singulars.prepend([rule, replacement]) end
uncountable(*words) 链接
指定不可数的词,这些词不应该被词形变化。
uncountable 'money'
uncountable 'money', 'information'
uncountable %w( money information rice )
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/inflector/inflections.rb, line 208 def uncountable(*words) @uncountables.add(words) end