跳到内容 跳到搜索
方法
#
E
H
M
N
T
U
包含的模块

属性

[RW] cache_key
[RW] collection
[RW] element
[RW] i18n_key
[RW] name
[RW] param_key
[RW] plural
[RW] route_key
[RW] singular
[RW] 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