Module: Mongoid::Extensions::String

Defined in:
lib/mongoid/extensions/string.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#unconvertable_to_bsonObject

Returns the value of attribute unconvertable_to_bson.



7
8
9
# File 'lib/mongoid/extensions/string.rb', line 7

def unconvertable_to_bson
  @unconvertable_to_bson
end

#unconvertable_to_bson If the document is unconvetable.(Ifthedocumentisunconvetable.) ⇒ Object



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

attr_accessor :unconvertable_to_bson

Instance Method Details

#__evolve_object_id__String, Moped::BSON::ObjectId

Evolve the string into an object id if possible.

Examples:

Evolve the string.

"test".__evolve_object_id__

Returns:

Since:

  • 3.0.0



17
18
19
# File 'lib/mongoid/extensions/string.rb', line 17

def __evolve_object_id__
  convert_to_object_id
end

#__mongoize_object_id__String, ...

Mongoize the string into an object id if possible.

Examples:

Evolve the string.

"test".__mongoize_object_id__

Returns:

Since:

  • 3.0.0



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

def __mongoize_object_id__
  convert_to_object_id unless blank?
end

#__mongoize_time__Time

Mongoize the string for storage.

Examples:

Mongoize the string.

"2012-01-01".__mongoize_time__

Returns:

  • (Time)

    The time.

Since:

  • 3.0.0



41
42
43
# File 'lib/mongoid/extensions/string.rb', line 41

def __mongoize_time__
  ::Time.configured.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.

Since:

  • 1.0.0



53
54
55
# File 'lib/mongoid/extensions/string.rb', line 53

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

#mongoid_id?true, false

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.

Since:

  • 2.3.1



65
66
67
# File 'lib/mongoid/extensions/string.rb', line 65

def mongoid_id?
  self =~ /\A(|_)id$/
end

#numeric?true, false

Is the string a number?

Examples:

Is the string a number.

"1234.23".numeric?

Returns:

  • (true, false)

    If the string is a number.

Since:

  • 3.0.0



77
78
79
# File 'lib/mongoid/extensions/string.rb', line 77

def numeric?
  true if Float(self) rescue false
end

#readerString

Get the string as a getter string.

Examples:

Get the reader/getter

"model=".reader

Returns:

  • (String)

    The string stripped of “=”.

Since:

  • 1.0.0



89
90
91
# File 'lib/mongoid/extensions/string.rb', line 89

def reader
  delete("=")
end

#to_aArray

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

Examples:

Convert the string to an array.

"Testing".to_a

Returns:

  • (Array)

    An array with only the string in it.

Since:

  • 1.0.0



101
102
103
# File 'lib/mongoid/extensions/string.rb', line 101

def to_a
  [ self ]
end

#unconvertable_to_bson?true, false

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.

Since:

  • 2.2.1



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

def unconvertable_to_bson?
  @unconvertable_to_bson ||= false
end

#writer?true, false

Is this string a writer?

Examples:

Is the string a setter method?

"model=".writer?

Returns:

  • (true, false)

    If the string contains “=”.

Since:

  • 1.0.0



113
114
115
# File 'lib/mongoid/extensions/string.rb', line 113

def writer?
  include?("=")
end