跳至內容 跳至搜尋

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)

透過正規表達式規則或字串對應指定字串的人性化形式。當使用基於正規表達式的取代時,取代後會呼叫正常人性化的格式化。當使用字串時,人性化的形式應該按照希望的方式指定(例如:『名稱』,而不是『名稱』)。

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