Module: Mongoid::Extensions::String

Defined in:
build/mongoid-master/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.



9
10
11
# File 'build/mongoid-master/lib/mongoid/extensions/string.rb', line 9

def unconvertable_to_bson
  @unconvertable_to_bson
end

#unconvertable_to_bson If the document is unconvertable.(Ifthedocumentisunconvertable.) ⇒ Object



9
# File 'build/mongoid-master/lib/mongoid/extensions/string.rb', line 9

attr_accessor :unconvertable_to_bson

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:

Since:

  • 3.0.0



19
20
21
# File 'build/mongoid-master/lib/mongoid/extensions/string.rb', line 19

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



31
32
33
# File 'build/mongoid-master/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:

  • (Time | ActiveSupport::TimeWithZone)

    Local time in the configured default time zone corresponding to this string.

Since:

  • 3.0.0



47
48
49
50
51
52
53
54
55
56
57
58
# File 'build/mongoid-master/lib/mongoid/extensions/string.rb', line 47

def __mongoize_time__
  # This 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.
  parsed = ::Time.parse(self)
  if ::Time == ::Time.configured
    parsed
  else
    ::Time.configured.parse(self)
  end
end

#before_type_cast?true, false

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”

Since:

  • 3.1.0



143
144
145
# File 'build/mongoid-master/lib/mongoid/extensions/string.rb', line 143

def before_type_cast?
  ends_with?("_before_type_cast")
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



68
69
70
# File 'build/mongoid-master/lib/mongoid/extensions/string.rb', line 68

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



80
81
82
# File 'build/mongoid-master/lib/mongoid/extensions/string.rb', line 80

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.

Examples:

Is the string a number.

"1234.23".numeric?

Returns:

  • (true, false)

    If the string is a number.

Since:

  • 3.0.0



93
94
95
96
97
# File 'build/mongoid-master/lib/mongoid/extensions/string.rb', line 93

def numeric?
  !!Float(self)
rescue ArgumentError
  (self =~ /\A(?:NaN|-?Infinity)\z/) == 0
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



107
108
109
# File 'build/mongoid-master/lib/mongoid/extensions/string.rb', line 107

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?

Examples:

Is the object unconvertable?

object.unconvertable_to_bson?

Returns:

  • (true, false)

    If the object is unconvertable.

Since:

  • 2.2.1



155
156
157
# File 'build/mongoid-master/lib/mongoid/extensions/string.rb', line 155

def unconvertable_to_bson?
  @unconvertable_to_bson ||= false
end

#valid_method_name?true, false

Is this string a valid_method_name?

Examples:

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

"model=".valid_method_name?

Returns:

  • (true, false)

    If the string contains a valid Ruby identifier.

Since:

  • 3.0.15



131
132
133
# File 'build/mongoid-master/lib/mongoid/extensions/string.rb', line 131

def valid_method_name?
  /[@$"-]/ !~ self
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



119
120
121
# File 'build/mongoid-master/lib/mongoid/extensions/string.rb', line 119

def writer?
  include?("=")
end