方法
包含的模块
- Comparable
Attributes
| [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 实例。默认情况下,namespace 和 name 选项将分别采用给定类的命名空间和名称。使用 locale 参数来单数化和复数化模型名称。
module Foo class Bar end end ActiveModel::Name.new(Foo::Bar).to_s # => "Foo::Bar"
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/naming.rb, line 165 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
来源: 在 GitHub 上
# File activemodel/lib/active_model/naming.rb, line 82
<=>(other) 链接
等同于 String#<=>。
class BlogPost extend ActiveModel::Naming end BlogPost.model_name <=> 'BlogPost' # => 0 BlogPost.model_name <=> 'Blog' # => 1 BlogPost.model_name <=> 'BlogPosts' # => -1
来源: 在 GitHub 上
# File activemodel/lib/active_model/naming.rb, line 49
==(other) 链接
等同于 String#==。如果类名与 other 相等,则返回 true,否则返回 false。
class BlogPost extend ActiveModel::Naming end BlogPost.model_name == 'BlogPost' # => true BlogPost.model_name == 'Blog Post' # => false
来源: 在 GitHub 上
# File activemodel/lib/active_model/naming.rb, line 18
===(other) 链接
等同于 ==。
class BlogPost extend ActiveModel::Naming end BlogPost.model_name === 'BlogPost' # => true BlogPost.model_name === 'Blog Post' # => false
来源: 在 GitHub 上
# File activemodel/lib/active_model/naming.rb, line 34
=~(regexp) 链接
等同于 String#=~。将类名与给定的正则表达式进行匹配。返回匹配开始的位置,如果未匹配到则返回 nil。
class BlogPost extend ActiveModel::Naming end BlogPost.model_name =~ /Post/ # => 4 BlogPost.model_name =~ /\d/ # => nil
来源: 在 GitHub 上
# File activemodel/lib/active_model/naming.rb, line 65
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
来源: 在 GitHub 上
# File activemodel/lib/active_model/naming.rb, line 98
human(options = {}) 链接
使用 I18n 将模型名称转换为更易读的格式。默认情况下,它将下划线化然后人性化类名。
class BlogPost extend ActiveModel::Naming end BlogPost.model_name.human # => "Blog post"
指定 options 并提供其他翻译选项。
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/naming.rb, line 196 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
来源: 在 GitHub 上
# File activemodel/lib/active_model/naming.rb, line 114
to_s() 链接
返回类名。
class BlogPost extend ActiveModel::Naming end BlogPost.model_name.to_s # => "BlogPost"
来源: 在 GitHub 上
# File activemodel/lib/active_model/naming.rb, line 130
to_str() 链接
等同于 to_s。
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/naming.rb, line 150 delegate :==, :===, :<=>, :=~, :"!~", :eql?, :match?, :to_s, :to_str, :as_json, to: :name
uncountable?() 链接
来源: 显示 | 在 GitHub 上
# File activemodel/lib/active_model/naming.rb, line 208 def uncountable? @uncountable end