Module: Mongoid::Extensions::String
| Relationships & Source Files | |
| Namespace Children | |
|
Modules:
| |
| Extension / Inclusion / Inheritance Descendants | |
|
Included In:
| |
| Defined in: | lib/mongoid/extensions/string.rb |
Overview
Adds type-casting behavior to String class.
Instance Attribute Summary
-
#before_type_cast? ⇒ true | false
readonly
Does the string end with _before_type_cast?
- #mongoid_id? ⇒ true | false readonly deprecated Deprecated.
-
#numeric? ⇒ true | false
readonly
Is the string a number? The literals “NaN”, “Infinity”, and “-Infinity” are counted as numbers.
- #unconvertable_to_bson rw deprecated Deprecated.
- #unconvertable_to_bson If the document is unconvertable.(If the document is unconvertable.) rw deprecated Deprecated.
- #unconvertable_to_bson? ⇒ true | false rw deprecated Deprecated.
-
#valid_method_name? ⇒ true | false
readonly
Is this string a valid_method_name?
-
#writer? ⇒ true | false
readonly
Is this string a writer?
Instance Method Summary
-
#__evolve_object_id__ ⇒ String | BSON::ObjectId
Evolve the string into an object id if possible.
-
#__mongoize_object_id__ ⇒ String | BSON::ObjectId | nil
Mongoize the string into an object id if possible.
-
#__mongoize_time__ ⇒ Time | ActiveSupport::TimeWithZone
Mongoize the string for storage.
-
#collectionize ⇒ String
Convert the string to a collection friendly name.
-
#reader ⇒ String
Get the string as a getter string.
-
#convert_to_object_id ⇒ String | BSON::ObjectId
private
Internal use only
Internal use only
If the string is a legal object id, convert it.
Instance Attribute Details
#before_type_cast? ⇒ true | false (readonly)
Does the string end with _before_type_cast?
# File 'lib/mongoid/extensions/string.rb', line 125
def before_type_cast? ends_with?('_before_type_cast') end
#mongoid_id? ⇒ true | false (readonly)
Is the string a valid value for a ::Mongoid id?
# File 'lib/mongoid/extensions/string.rb', line 71
def mongoid_id? self =~ /\A(|_)id\z/ end
#numeric? ⇒ true | false (readonly)
Is the string a number? The literals “NaN”, “Infinity”, and “-Infinity” are counted as numbers.
# File 'lib/mongoid/extensions/string.rb', line 83
def numeric? !!Float(self) rescue ArgumentError (self =~ /\A(?:NaN|-?Infinity)\z/) == 0 end
#unconvertable_to_bson (rw)
[ GitHub ]# File 'lib/mongoid/extensions/string.rb', line 9
attr_accessor :unconvertable_to_bson
#unconvertable_to_bson If the document is unconvertable.(If the document is unconvertable.) (rw)
[ GitHub ]# File 'lib/mongoid/extensions/string.rb', line 9
attr_accessor :unconvertable_to_bson
#unconvertable_to_bson? ⇒ true | false (rw)
Is the object not to be converted to bson on criteria creation?
# File 'lib/mongoid/extensions/string.rb', line 136
def unconvertable_to_bson? @unconvertable_to_bson ||= false end
#valid_method_name? ⇒ true | false (readonly)
Is this string a valid_method_name?
# File 'lib/mongoid/extensions/string.rb', line 115
def valid_method_name? /[@$"-]/ !~ self end
#writer? ⇒ true | false (readonly)
Is this string a writer?
# File 'lib/mongoid/extensions/string.rb', line 105
def writer? include?('=') end
Instance Method Details
#__evolve_object_id__ ⇒ String | BSON::ObjectId
Evolve the string into an object id if possible.
# File 'lib/mongoid/extensions/string.rb', line 19
def __evolve_object_id__ convert_to_object_id end
#__mongoize_object_id__ ⇒ String | BSON::ObjectId | nil
Mongoize the string into an object id if possible.
# File 'lib/mongoid/extensions/string.rb', line 29
def __mongoize_object_id__ convert_to_object_id unless blank? end
#__mongoize_time__ ⇒ Time | ActiveSupport::TimeWithZone
Returns a local time in the default time zone.
Mongoize the string for storage.
# File 'lib/mongoid/extensions/string.rb', line 45
def __mongoize_time__ # This extra Time.parse is required to raise an error if the string # is not a valid time string. ActiveSupport::TimeZone does not # perform this check. ::Time.parse(self) ::Time.zone.parse(self) end
#collectionize ⇒ String
Convert the string to a collection friendly name.
# File 'lib/mongoid/extensions/string.rb', line 60
def collectionize tableize.tr('/', '_') end
#convert_to_object_id ⇒ String | BSON::ObjectId (private)
If the string is a legal object id, convert it.
#reader ⇒ String
Get the string as a getter string.
# File 'lib/mongoid/extensions/string.rb', line 95
def reader delete('=').sub(/_before_type_cast\z/, '') end