module Mongoid::Extensions::String

Attributes

unconvertable_to_bson[RW]

@attribute [rw] #unconvertable_to_bson If the document is unconvetable.

Public Instance Methods

__evolve_object_id__() click to toggle source

Evolve the string into an object id if possible.

@example Evolve the string.

"test".__evolve_object_id__

@return [ String, Moped::BSON::ObjectId ] The evolved string.

@since 3.0.0

# File lib/mongoid/extensions/string.rb, line 16
def __evolve_object_id__
  convert_to_object_id
end
__mongoize_object_id__() click to toggle source

Mongoize the string into an object id if possible.

@example Evolve the string.

"test".__mongoize_object_id__

@return [ String, Moped::BSON::ObjectId, nil ] The mongoized string.

@since 3.0.0

# File lib/mongoid/extensions/string.rb, line 28
def __mongoize_object_id__
  convert_to_object_id unless blank?
end
__mongoize_time__() click to toggle source

Mongoize the string for storage.

@example Mongoize the string.

"2012-01-01".__mongoize_time__

@note The extra parse from Time is because ActiveSupport::TimeZone

either returns nil or Time.now if the string is empty or invalid,
which is a regression from pre-3.0 and also does not agree with
the core Time API.

@return [ Time ] The time.

@since 3.0.0

# File lib/mongoid/extensions/string.rb, line 45
def __mongoize_time__
  ::Time.parse(self)
  ::Time.configured.parse(self)
end
before_type_cast?() click to toggle source

Does the string end with _before_type_cast?

@example Is the string a setter method?

"price_before_type_cast".before_type_cast?

@return [ true, false ] If the string ends with “_before_type_cast”

@since 3.1.0

# File lib/mongoid/extensions/string.rb, line 142
def before_type_cast?
  ends_with?("_before_type_cast")
end
collectionize() click to toggle source

Convert the string to a collection friendly name.

@example Collectionize the string.

"namespace/model".collectionize

@return [ String ] The string in collection friendly form.

@since 1.0.0

# File lib/mongoid/extensions/string.rb, line 58
def collectionize
  tableize.gsub("/", "_")
end
mongoid_id?() click to toggle source

Is the string a valid value for a Mongoid id?

@example Is the string an id value?

"_id".mongoid_id?

@return [ true, false ] If the string is id or _id.

@since 2.3.1

# File lib/mongoid/extensions/string.rb, line 70
def mongoid_id?
  self =~ /\A(|_)id$/
end
numeric?() click to toggle source

Is the string a number?

@example Is the string a number.

"1234.23".numeric?

@return [ true, false ] If the string is a number.

@since 3.0.0

# File lib/mongoid/extensions/string.rb, line 82
def numeric?
  true if Float(self) rescue (self == "NaN")
end
reader() click to toggle source

Get the string as a getter string.

@example Get the reader/getter

"model=".reader

@return [ String ] The string stripped of “=”.

@since 1.0.0

# File lib/mongoid/extensions/string.rb, line 94
def reader
  delete("=").sub(/\_before\_type\_cast$/, '')
end
to_a() click to toggle source

Convert the string to an array with the string in it.

@example Convert the string to an array.

"Testing".to_a

@return [ Array ] An array with only the string in it.

@since 1.0.0

# File lib/mongoid/extensions/string.rb, line 106
def to_a
  [ self ]
end
unconvertable_to_bson?() click to toggle source

Is the object not to be converted to bson on criteria creation?

@example Is the object unconvertable?

object.unconvertable_to_bson?

@return [ true, false ] If the object is unconvertable.

@since 2.2.1

# File lib/mongoid/extensions/string.rb, line 154
def unconvertable_to_bson?
  @unconvertable_to_bson ||= false
end
valid_method_name?() click to toggle source

Is this string a valid_method_name?

@example Is the string a valid Ruby idenfier for use as a method name

"model=".valid_method_name?

@return [ true, false ] If the string contains a valid Ruby identifier.

@since 3.0.15

# File lib/mongoid/extensions/string.rb, line 130
def valid_method_name?
  /[@$"]/ !~ to_sym.inspect
end
writer?() click to toggle source

Is this string a writer?

@example Is the string a setter method?

"model=".writer?

@return [ true, false ] If the string contains “=”.

@since 1.0.0

# File lib/mongoid/extensions/string.rb, line 118
def writer?
  include?("=")
end

Private Instance Methods

convert_to_object_id() click to toggle source

If the string is a legal object id, convert it.

@api private

@example Convert to the object id.

string.convert_to_object_id

@return [ String, Moped::BSON::ObjectId ] The string or the id.

@since 3.0.0

# File lib/mongoid/extensions/string.rb, line 170
def convert_to_object_id
  Moped::BSON::ObjectId.legal?(self) ? Moped::BSON::ObjectId.from_string(self) : self
end