123456789_123456789_123456789_123456789_123456789_

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

Instance Method Summary

Instance Attribute Details

#before_type_cast?true | false (readonly)

Does the string end with _before_type_cast?

Examples:

Is the string a setter method?

"price_before_type_cast".before_type_cast?

Returns:

  • (true | false)

    If the string ends with “_before_type_cast”

[ GitHub ]

  
# File 'lib/mongoid/extensions/string.rb', line 127

def before_type_cast?
  ends_with?("_before_type_cast")
end

#mongoid_id?true | false (readonly)

Deprecated.

Is the string a valid value for a ::Mongoid id?

Examples:

Is the string an id value?

"_id".mongoid_id?

Returns:

  • (true | false)

    If the string is id or _id.

[ GitHub ]

  
# File 'lib/mongoid/extensions/string.rb', line 73

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.

Examples:

Is the string a number.

"1234.23".numeric?

Returns:

  • (true | false)

    If the string is a number.

[ GitHub ]

  
# File 'lib/mongoid/extensions/string.rb', line 85

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 12

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 12

attr_accessor :unconvertable_to_bson

#unconvertable_to_bson?true | false (rw)

Deprecated.

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

Examples:

Is the object unconvertable?

object.unconvertable_to_bson?

Returns:

  • (true | false)

    If the object is unconvertable.

[ GitHub ]

  
# File 'lib/mongoid/extensions/string.rb', line 139

def unconvertable_to_bson?
  @unconvertable_to_bson ||= false
end

#valid_method_name?true | false (readonly)

Is this string a valid_method_name?

Examples:

Is the string a valid Ruby identifier for use as a method name

"model=".valid_method_name?

Returns:

  • (true | false)

    If the string contains a valid Ruby identifier.

[ GitHub ]

  
# File 'lib/mongoid/extensions/string.rb', line 117

def valid_method_name?
  /[@$"-]/ !~ self
end

#writer?true | false (readonly)

Is this string a writer?

Examples:

Is the string a setter method?

"model=".writer?

Returns:

  • (true | false)

    If the string contains “=”.

[ GitHub ]

  
# File 'lib/mongoid/extensions/string.rb', line 107

def writer?
  include?("=")
end

Instance Method Details

#__evolve_object_id__String | BSON::ObjectId

Evolve the string into an object id if possible.

Examples:

Evolve the string.

"test".__evolve_object_id__

Returns:

[ GitHub ]

  
# 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.

Examples:

Evolve the string.

"test".__mongoize_object_id__

Returns:

[ GitHub ]

  
# File 'lib/mongoid/extensions/string.rb', line 31

def __mongoize_object_id__
  convert_to_object_id unless blank?
end

#__mongoize_time__Time | ActiveSupport::TimeWithZone

Note:

Returns a local time in the default time zone.

Mongoize the string for storage.

Examples:

Mongoize the string.

"2012-01-01".__mongoize_time__
# => 2012-01-01 00:00:00 -0500

Returns:

Raises:

  • (ArgumentError)

    The string is not a valid time string.

[ GitHub ]

  
# 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

#collectionizeString

Convert the string to a collection friendly name.

Examples:

Collectionize the string.

"namespace/model".collectionize

Returns:

  • (String)

    The string in collection friendly form.

[ GitHub ]

  
# File 'lib/mongoid/extensions/string.rb', line 62

def collectionize
  tableize.gsub("/", "_")
end

#convert_to_object_idString | BSON::ObjectId (private)

This method is for internal use only.

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

Examples:

Convert to the object id.

string.convert_to_object_id

Returns:

[ GitHub ]

  
# File 'lib/mongoid/extensions/string.rb', line 154

def convert_to_object_id
  BSON::ObjectId.legal?(self) ? BSON::ObjectId.from_string(self) : self
end

#readerString

Get the string as a getter string.

Examples:

Get the reader/getter

"model=".reader

Returns:

  • (String)

    The string stripped of “=”.

[ GitHub ]

  
# File 'lib/mongoid/extensions/string.rb', line 97

def reader
  delete("=").sub(/\_before\_type\_cast\z/, '')
end