Class: ActiveModel::Name
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
Comparable
|
|
Inherits: | Object |
Defined in: | activemodel/lib/active_model/naming.rb |
Class Method Summary
-
.new(klass, namespace = nil, name = nil) ⇒ Name
constructor
Returns a new
Name
instance.
Instance Attribute Summary
-
#cache_key
readonly
Alias for #collection.
- #collection (also: #cache_key) rw
- #element rw
- #i18n_key rw
- #name rw
- #param_key rw
- #plural rw
- #route_key rw
- #singular rw
- #singular_route_key rw
Instance Method Summary
-
#!~(regexp)
Equivalent to
String#!~
. -
#<=>(other)
Equivalent to
String#<=>
. -
#==(other)
Equivalent to
String#==
. -
#===(other)
Equivalent to #==.
-
#=~(regexp)
Equivalent to
String#=~
. - #as_json
-
#eql?(other) ⇒ Boolean
Equivalent to
String#eql?
. -
#human(options = {})
Transform the model name into a more human format, using
::I18n
. -
#match?(regexp) ⇒ Boolean
Equivalent to
String#match?
. -
#to_s ⇒ ?
Returns the class name.
-
#to_str ⇒ ?
Equivalent to #to_s.
Constructor Details
.new(klass, namespace = nil, name = nil) ⇒ Name
Returns a new Name
instance. By default, the namespace
and #name option will take the namespace and name of the given class respectively.
module Foo
class Bar
end
end
ActiveModel::Name.new(Foo::Bar).to_s
# => "Foo::Bar"
# File 'activemodel/lib/active_model/naming.rb', line 164
def initialize(klass, namespace = nil, name = nil) @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) @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) : @plural.dup) @singular_route_key = ActiveSupport::Inflector.singularize(@route_key) @route_key << "_index" if @plural == @singular end
Instance Attribute Details
#cache_key (readonly)
Alias for #collection.
# File 'activemodel/lib/active_model/naming.rb', line 15
alias_method :cache_key, :collection
#collection (rw) Also known as: #cache_key
[ GitHub ]#element (rw)
[ GitHub ]# File 'activemodel/lib/active_model/naming.rb', line 11
attr_accessor :singular, :plural, :element, :collection, :singular_route_key, :route_key, :param_key, :i18n_key, :name
#i18n_key (rw)
[ GitHub ]# File 'activemodel/lib/active_model/naming.rb', line 11
attr_accessor :singular, :plural, :element, :collection, :singular_route_key, :route_key, :param_key, :i18n_key, :name
#name (rw)
[ GitHub ]# File 'activemodel/lib/active_model/naming.rb', line 11
attr_accessor :singular, :plural, :element, :collection, :singular_route_key, :route_key, :param_key, :i18n_key, :name
#param_key (rw)
[ GitHub ]# File 'activemodel/lib/active_model/naming.rb', line 11
attr_accessor :singular, :plural, :element, :collection, :singular_route_key, :route_key, :param_key, :i18n_key, :name
#plural (rw)
[ GitHub ]# File 'activemodel/lib/active_model/naming.rb', line 11
attr_accessor :singular, :plural, :element, :collection, :singular_route_key, :route_key, :param_key, :i18n_key, :name
#route_key (rw)
[ GitHub ]# File 'activemodel/lib/active_model/naming.rb', line 11
attr_accessor :singular, :plural, :element, :collection, :singular_route_key, :route_key, :param_key, :i18n_key, :name
#singular (rw)
[ GitHub ]# File 'activemodel/lib/active_model/naming.rb', line 11
attr_accessor :singular, :plural, :element, :collection, :singular_route_key, :route_key, :param_key, :i18n_key, :name
#singular_route_key (rw)
[ GitHub ]Instance Method Details
#!~(regexp)
Equivalent to String#!~
. Match the class name against the given regexp. Returns true
if there is no match, otherwise 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 82
rdoc_method :method: !~
#<=>(other)
Equivalent to String#<=>
.
class BlogPost
extend ActiveModel::Naming
end
BlogPost.model_name <=> 'BlogPost' # => 0
BlogPost.model_name <=> 'Blog' # => 1
BlogPost.model_name <=> 'BlogPosts' # => -1
#==(other)
Equivalent to String#==
. Returns true
if the class name and other
are equal, otherwise false
.
class BlogPost
extend ActiveModel::Naming
end
BlogPost.model_name == 'BlogPost' # => true
BlogPost.model_name == 'Blog Post' # => false
#===(other)
Equivalent to #==.
class BlogPost
extend ActiveModel::Naming
end
BlogPost.model_name === 'BlogPost' # => true
BlogPost.model_name === 'Blog Post' # => false
#=~(regexp)
Equivalent to String#=~
. Match the class name against the given regexp. Returns the position where the match starts or nil
if there is no match.
class BlogPost
extend ActiveModel::Naming
end
BlogPost.model_name =~ /Post/ # => 4
BlogPost.model_name =~ /\d/ # => nil
#as_json
[ GitHub ]
#eql?(other) ⇒ Boolean
Equivalent to String#eql?
. Returns true
if the class name and other
have the same length and content, otherwise false
.
class BlogPost
extend ActiveModel::Naming
end
BlogPost.model_name.eql?('BlogPost') # => true
BlogPost.model_name.eql?('Blog Post') # => false
#human(options = {})
Transform the model name into a more human format, using ::I18n
. By default, it will underscore then humanize the class name.
class BlogPost
extend ActiveModel::Naming
end
BlogPost.model_name.human # => "Blog post"
Specify options
with additional translating options.
# File 'activemodel/lib/active_model/naming.rb', line 194
def human( = {}) return @human unless @klass.respond_to?(:lookup_ancestors) && @klass.respond_to?(:i18n_scope) defaults = @klass.lookup_ancestors.map do |klass| klass.model_name.i18n_key end defaults << [:default] if [:default] defaults << @human = { scope: [@klass.i18n_scope, :models], count: 1, default: defaults }.merge!( .except(:default)) I18n.translate(defaults.shift, ** ) end
#match?(regexp) ⇒ Boolean
Equivalent to String#match?
. Match the class name against the given regexp. Returns true
if there is a match, otherwise false
.
class BlogPost
extend ActiveModel::Naming
end
BlogPost.model_name.match?(/Post/) # => true
BlogPost.model_name.match?(/\d/) # => false
#to_s ⇒ ?
Returns the class name.
class BlogPost
extend ActiveModel::Naming
end
BlogPost.model_name.to_s # => "BlogPost"
#to_str ⇒ ?
Equivalent to #to_s.