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.(Ifthedocumentisunconvetable.)



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

attr_accessor :unconvertable_to_bson

Instance Method Details

- (String, BSON::ObjectId) __evolve_object_id__

Evolve the string into an object id if possible.

Examples:

Evolve the string.

"test".__evolve_object_id__

Since:

  • 3.0.0



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

def __evolve_object_id__
  convert_to_object_id
end

- (String, ...) __mongoize_object_id__

Mongoize the string into an object id if possible.

Examples:

Evolve the string.

"test".__mongoize_object_id__

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

- (Time) __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.

Mongoize the string for storage.

Examples:

Mongoize the string.

"2012-01-01".__mongoize_time__

Since:

  • 3.0.0



46
47
48
49
# File 'lib/mongoid/extensions/string.rb', line 46

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

- (true, false) before_type_cast?

Does the string end with _before_type_cast?

Examples:

Is the string a setter method?

"price_before_type_cast".before_type_cast?

Since:

  • 3.1.0



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

def before_type_cast?
  ends_with?("_before_type_cast")
end

- (String) collectionize

Convert the string to a collection friendly name.

Examples:

Collectionize the string.

"namespace/model".collectionize

Since:

  • 1.0.0



59
60
61
# File 'lib/mongoid/extensions/string.rb', line 59

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?

Since:

  • 2.3.1



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

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

- (true, false) numeric?

Is the string a number?

Examples:

Is the string a number.

"1234.23".numeric?

Since:

  • 3.0.0



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

def numeric?
  true if Float(self) rescue (self == "NaN")
end

- (String) reader

Get the string as a getter string.

Examples:

Get the reader/getter

"model=".reader

Since:

  • 1.0.0



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

def reader
  delete("=").sub(/\_before\_type\_cast$/, '')
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?

Since:

  • 2.2.1



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

def unconvertable_to_bson?
  @unconvertable_to_bson ||= false
end

- (true, false) valid_method_name?

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?

Since:

  • 3.0.15



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

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

- (true, false) writer?

Is this string a writer?

Examples:

Is the string a setter method?

"model=".writer?

Since:

  • 1.0.0



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

def writer?
  include?("=")
end