跳至內容 跳至搜尋
方法
#
E
H
M
N
T
U
已包含模組

屬性

[讀寫] cache_key
[讀寫] collection
[讀寫] element
[讀寫] i18n_key
[讀寫] name
[讀寫] param_key
[讀寫] plural
[讀寫] route_key
[讀寫] singular
[讀寫] singular_route_key

類別公開方法

new(klass, namespace = nil, name = nil, locale = :en)

回傳一個新的 ActiveModel::Name 實例。預設情況下,namespacename 選項將分別採用給定類別的命名空間和名稱。使用 locale 參數來單複數化模型名稱。

module Foo
  class Bar
  end
end

ActiveModel::Name.new(Foo::Bar).to_s
# => "Foo::Bar"
# File activemodel/lib/active_model/naming.rb, line 166
def initialize(klass, namespace = nil, name = nil, locale = :en)
  @name = name || klass.name

  raise ArgumentError, "Class name cannot be blank. You need to supply a name argument when anonymous class given" if @name.blank?

  @unnamespaced = @name.delete_prefix("#{namespace.name}::") if namespace
  @klass        = klass
  @singular     = _singularize(@name)
  @plural       = ActiveSupport::Inflector.pluralize(@singular, locale)
  @uncountable  = @plural == @singular
  @element      = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(@name))
  @human        = ActiveSupport::Inflector.humanize(@element)
  @collection   = ActiveSupport::Inflector.tableize(@name)
  @param_key    = (namespace ? _singularize(@unnamespaced) : @singular)
  @i18n_key     = @name.underscore.to_sym

  @route_key          = (namespace ? ActiveSupport::Inflector.pluralize(@param_key, locale) : @plural.dup)
  @singular_route_key = ActiveSupport::Inflector.singularize(@route_key, locale)
  @route_key << "_index" if @uncountable
end

實例公開方法

!~(regexp)

等同於 String#!~。將類別名稱與給定的正規表達式進行匹配。如果沒有匹配,則回傳 true,否則回傳 false

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name !~ /Post/ # => false
BlogPost.model_name !~ /\d/   # => true
# File activemodel/lib/active_model/naming.rb, line 83
    

<=>(other)

等同於 String#<=>

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name <=> 'BlogPost'  # => 0
BlogPost.model_name <=> 'Blog'      # => 1
BlogPost.model_name <=> 'BlogPosts' # => -1
# File activemodel/lib/active_model/naming.rb, line 50
    

==(other)

等同於 String#==。如果類別名稱和 other 相等,則回傳 true,否則回傳 false

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name == 'BlogPost'  # => true
BlogPost.model_name == 'Blog Post' # => false
# File activemodel/lib/active_model/naming.rb, line 19
    

===(other)

等同於 #==

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name === 'BlogPost'  # => true
BlogPost.model_name === 'Blog Post' # => false
# File activemodel/lib/active_model/naming.rb, line 35
    

=~(regexp)

等同於 String#=~。將類別名稱與給定的正規表達式進行匹配。回傳匹配開始的位置,如果沒有匹配,則回傳 nil

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name =~ /Post/ # => 4
BlogPost.model_name =~ /\d/   # => nil
# File activemodel/lib/active_model/naming.rb, line 66
    

eql?(other)

等同於 String#eql?。如果類別名稱和 other 具有相同的長度和內容,則回傳 true,否則回傳 false

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name.eql?('BlogPost')  # => true
BlogPost.model_name.eql?('Blog Post') # => false
# File activemodel/lib/active_model/naming.rb, line 99
    

human(options = {})

使用 I18n 將模型名稱轉換為更人性化的格式。預設情況下,它會先將類別名稱加上底線,然後進行人性化處理。

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name.human # => "Blog post"

使用 options 指定其他翻譯選項。

# File activemodel/lib/active_model/naming.rb, line 197
def human(options = {})
  return @human if i18n_keys.empty? || i18n_scope.empty?

  key, *defaults = i18n_keys
  defaults << options[:default] if options[:default]
  defaults << MISSING_TRANSLATION

  translation = I18n.translate(key, scope: i18n_scope, count: 1, **options, default: defaults)
  translation = @human if translation == MISSING_TRANSLATION
  translation
end

match?(regexp)

等同於 String#match?。將類別名稱與給定的正規表達式進行匹配。如果匹配,則回傳 true,否則回傳 false

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name.match?(/Post/) # => true
BlogPost.model_name.match?(/\d/) # => false
# File activemodel/lib/active_model/naming.rb, line 115
    

to_s()

回傳類別名稱。

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name.to_s # => "BlogPost"
# File activemodel/lib/active_model/naming.rb, line 131
    

to_str()

等同於 to_s

# File activemodel/lib/active_model/naming.rb, line 151
delegate :==, :===, :<=>, :=~, :"!~", :eql?, :match?, :to_s,
         :to_str, :as_json, to: :name

uncountable?()

# File activemodel/lib/active_model/naming.rb, line 209
def uncountable?
  @uncountable
end