Module: Mongoid::Extensions::String
- Defined in:
- lib/mongoid/extensions/string.rb
Overview
Adds type-casting behavior to String class.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
- #unconvertable_to_bson ⇒ Object deprecated Deprecated.
- #unconvertable_to_bson If the document is unconvertable.(Ifthedocumentisunconvertable.) ⇒ Object deprecated Deprecated.
Instance Method Summary collapse
-
#__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.
-
#before_type_cast? ⇒ true | false
Does the string end with _before_type_cast?.
-
#collectionize ⇒ String
Convert the string to a collection friendly name.
- #mongoid_id? ⇒ true | false deprecated Deprecated.
-
#numeric? ⇒ true | false
Is the string a number? The literals “NaN”, “Infinity”, and “-Infinity” are counted as numbers.
-
#reader ⇒ String
Get the string as a getter string.
- #unconvertable_to_bson? ⇒ true | false deprecated Deprecated.
-
#valid_method_name? ⇒ true | false
Is this string a valid_method_name?.
-
#writer? ⇒ true | false
Is this string a writer?.
Instance Attribute Details
#unconvertable_to_bson ⇒ Object
12 13 14 |
# File 'lib/mongoid/extensions/string.rb', line 12 def unconvertable_to_bson @unconvertable_to_bson end |
#unconvertable_to_bson If the document is unconvertable.(Ifthedocumentisunconvertable.) ⇒ Object
12 |
# File 'lib/mongoid/extensions/string.rb', line 12 attr_accessor :unconvertable_to_bson |
Instance Method Details
#__evolve_object_id__ ⇒ String | BSON::ObjectId
Evolve the string into an object id if possible.
21 22 23 |
# File 'lib/mongoid/extensions/string.rb', line 21 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.
31 32 33 |
# File 'lib/mongoid/extensions/string.rb', line 31 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.
47 48 49 50 51 52 53 54 |
# File 'lib/mongoid/extensions/string.rb', line 47 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 |
#before_type_cast? ⇒ true | false
Does the string end with _before_type_cast?
127 128 129 |
# File 'lib/mongoid/extensions/string.rb', line 127 def before_type_cast? ends_with?("_before_type_cast") end |
#collectionize ⇒ String
Convert the string to a collection friendly name.
62 63 64 |
# File 'lib/mongoid/extensions/string.rb', line 62 def collectionize tableize.gsub("/", "_") end |
#mongoid_id? ⇒ true | false
Is the string a valid value for a Mongoid id?
73 74 75 |
# File 'lib/mongoid/extensions/string.rb', line 73 def mongoid_id? self =~ /\A(|_)id\z/ end |
#numeric? ⇒ true | false
Is the string a number? The literals “NaN”, “Infinity”, and “-Infinity” are counted as numbers.
85 86 87 88 89 |
# File 'lib/mongoid/extensions/string.rb', line 85 def numeric? !!Float(self) rescue ArgumentError (self =~ /\A(?:NaN|-?Infinity)\z/) == 0 end |
#reader ⇒ String
Get the string as a getter string.
97 98 99 |
# File 'lib/mongoid/extensions/string.rb', line 97 def reader delete("=").sub(/\_before\_type\_cast\z/, '') end |
#unconvertable_to_bson? ⇒ true | false
Is the object not to be converted to bson on criteria creation?
139 140 141 |
# File 'lib/mongoid/extensions/string.rb', line 139 def unconvertable_to_bson? @unconvertable_to_bson ||= false end |
#valid_method_name? ⇒ true | false
Is this string a valid_method_name?
117 118 119 |
# File 'lib/mongoid/extensions/string.rb', line 117 def valid_method_name? /[@$"-]/ !~ self end |
#writer? ⇒ true | false
Is this string a writer?
107 108 109 |
# File 'lib/mongoid/extensions/string.rb', line 107 def writer? include?("=") end |