跳到內容 跳到搜尋

Active Support Inflections

這個類別的單例實例由 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

新的規則會加在最上面。因此在上面的範例中,cactus 的不規則規則現在會是執行複數化和單數化的規則中的第一個。這保證您的規則會在任何可能已經載入的規則之前執行。

命名空間
方法
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 '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

複數(規則、替換)

指定一個新的複數化規則及其替換。規則可以是字串或正規表示式。替換應始終是一個字串,其中可能包含對規則中匹配資料的參照。

# 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

單數(規則、替換)

指定一個新的單數化規則及其替換。規則可以是字串或正規表示式。替換應始終是一個字串,其中可能包含對規則中匹配資料的參照。

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