forked from mongodb/mongoid
/
string.rb
181 lines (164 loc) · 4.48 KB
/
string.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# encoding: utf-8
module Mongoid
module Extensions
module String
# @attribute [rw] unconvertable_to_bson If the document is unconvetable.
attr_accessor :unconvertable_to_bson
# Evolve the string into an object id if possible.
#
# @example Evolve the string.
# "test".__evolve_object_id__
#
# @return [ String, Moped::BSON::ObjectId ] The evolved string.
#
# @since 3.0.0
def __evolve_object_id__
convert_to_object_id
end
# Mongoize the string into an object id if possible.
#
# @example Evolve the string.
# "test".__mongoize_object_id__
#
# @return [ String, Moped::BSON::ObjectId, nil ] The mongoized string.
#
# @since 3.0.0
def __mongoize_object_id__
convert_to_object_id unless blank?
end
# Mongoize the string for storage.
#
# @example Mongoize the string.
# "2012-01-01".__mongoize_time__
#
# @return [ Time ] The time.
#
# @since 3.0.0
def __mongoize_time__
::Time.configured.parse(self)
end
# Convert the string to a collection friendly name.
#
# @example Collectionize the string.
# "namespace/model".collectionize
#
# @return [ String ] The string in collection friendly form.
#
# @since 1.0.0
def collectionize
tableize.gsub("/", "_")
end
# Is the string a valid value for a Mongoid id?
#
# @example Is the string an id value?
# "_id".mongoid_id?
#
# @return [ true, false ] If the string is id or _id.
#
# @since 2.3.1
def mongoid_id?
self =~ /\A(|_)id$/
end
# Is the string a number?
#
# @example Is the string a number.
# "1234.23".numeric?
#
# @return [ true, false ] If the string is a number.
#
# @since 3.0.0
def numeric?
true if Float(self) rescue false
end
# Get the string as a getter string.
#
# @example Get the reader/getter
# "model=".reader
#
# @return [ String ] The string stripped of "=".
#
# @since 1.0.0
def reader
delete("=")
end
# Convert the string to an array with the string in it.
#
# @example Convert the string to an array.
# "Testing".to_a
#
# @return [ Array ] An array with only the string in it.
#
# @since 1.0.0
def to_a
[ self ]
end
# Is this string a writer?
#
# @example Is the string a setter method?
# "model=".writer?
#
# @return [ true, false ] If the string contains "=".
#
# @since 1.0.0
def writer?
include?("=")
end
# Is the object not to be converted to bson on criteria creation?
#
# @example Is the object unconvertable?
# object.unconvertable_to_bson?
#
# @return [ true, false ] If the object is unconvertable.
#
# @since 2.2.1
def unconvertable_to_bson?
@unconvertable_to_bson ||= false
end
private
# If the string is a legal object id, convert it.
#
# @api private
#
# @example Convert to the object id.
# string.convert_to_object_id
#
# @return [ String, BSON::ObjectId ] The string or the id.
#
# @since 3.0.0
def convert_to_object_id
BSON::ObjectId.legal?(self) ? BSON::ObjectId.from_string(self) : self
end
module ClassMethods
# Convert the object from it's mongo friendly ruby type to this type.
#
# @example Demongoize the object.
# String.demongoize(object)
#
# @param [ Object ] object The object to demongoize.
#
# @return [ String ] The object.
#
# @since 3.0.0
def demongoize(object)
object.try(:to_s)
end
# Turn the object from the ruby type we deal with to a Mongo friendly
# type.
#
# @example Mongoize the object.
# String.mongoize("123.11")
#
# @param [ Object ] object The object to mongoize.
#
# @return [ String ] The object mongoized.
#
# @since 3.0.0
def mongoize(object)
demongoize(object)
end
end
end
end
end
::String.__send__(:include, Mongoid::Extensions::String)
::String.__send__(:extend, Mongoid::Extensions::String::ClassMethods)