跳至内容 跳至搜索

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)

# File activesupport/lib/active_support/inflector/inflections.rb, line 65
def self.instance(locale = :en)
  @__instance__[locale] ||= new
end

instance_or_fallback(locale)

# 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()

# 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)

指定一个新的首字母缩略词。首字母缩略词必须按其出现在驼峰式字符串中的方式指定。包含首字母缩略词的下划线字符串在传递给 camelizehumanizetitleize 时将保留首字母缩略词。包含首字母缩略词的驼峰式字符串在标题化或人文化时将保持首字母缩略词,并在传递给 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'
# 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
# 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'
# 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'
# 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)

指定一个新的复数化规则及其替换。规则可以是字符串或正则表达式。替换应该始终是一个字符串,其中可能包含对规则中匹配数据的引用。

# 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)

指定一个新的单数化规则及其替换。规则可以是字符串或正则表达式。替换应该始终是一个字符串,其中可能包含对规则中匹配数据的引用。

# 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 )
# File activesupport/lib/active_support/inflector/inflections.rb, line 208
def uncountable(*words)
  @uncountables.add(words)
end