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

- (Object) unconvertable_to_bson

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

- (Object) unconvertable_to_bson If the document is unconvetable.



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

attr_accessor :unconvertable_to_bson

Instance Method Details

- (String, ...) __evolve_object_id__

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
20
21
# File 'lib/mongoid/extensions/string.rb', line 17

def __evolve_object_id__
  unless blank?
    BSON::ObjectId.legal?(self) ? BSON::ObjectId.from_string(self) : self
  end
end

- (Time) __mongoize_time__

Mongoize the string for storage.

Examples:

Mongoize the string.

"2012-01-01".__mongoize_time__

Returns:

  • (Time)

    The time.

Since:

  • 3.0.0



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

def __mongoize_time__
  ::Time.configured.parse(self)
end

- (String) collectionize

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



43
44
45
# File 'lib/mongoid/extensions/string.rb', line 43

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

- (true, false) mongoid_id?

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



55
56
57
# File 'lib/mongoid/extensions/string.rb', line 55

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

- (true, false) numeric?

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



67
68
69
# File 'lib/mongoid/extensions/string.rb', line 67

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

- (String) reader

Get the string as a getter string.

Examples:

Get the reader/getter

"model=".reader

Returns:

  • (String)

    The string stripped of “=”.

Since:

  • 1.0.0



79
80
81
# File 'lib/mongoid/extensions/string.rb', line 79

def reader
  delete("=")
end

- (Array) to_a

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



91
92
93
# File 'lib/mongoid/extensions/string.rb', line 91

def to_a
  [ self ]
end

- (true, false) unconvertable_to_bson?

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



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

def unconvertable_to_bson?
  @unconvertable_to_bson ||= false
end

- (true, false) writer?

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



103
104
105
# File 'lib/mongoid/extensions/string.rb', line 103

def writer?
  include?("=")
end