Module: Mongo::JavaImpl::Utils

Included in:
Collection, Connection, Cursor, DB
Defined in:
lib/jmongo/mongo/utils.rb

Constant Summary collapse

SortingHash =
Hash.new(0).merge!(
  "ascending" => 1, "asc" => 1, "1" => 1,
  "descending" => -1, "desc" => -1, "-1" => -1
)

Instance Method Summary collapse

Instance Method Details

#from_dbobject(obj) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/jmongo/mongo/utils.rb', line 152

def from_dbobject obj
  # for better upstream compatibility make the objects into ruby hash or array

  case obj
  when Java::ComMongodb::BasicDBObject, Java::ComMongodb::CommandResult
    h = obj.hashify
    Hash[h.keys.zip(h.values.map{|v| from_dbobject(v)})]
  when Java::ComMongodb::BasicDBList
    obj.arrayify.map{|v| from_dbobject(v)}
  when Java::JavaUtil::ArrayList
    obj.map{|v| from_dbobject(v)}
  when Java::JavaUtil::Date
    Time.at(obj.get_time/1000.0)
  when Java::OrgBsonTypes::Symbol
    obj.toString.to_sym
  when Java::JavaUtilRegex::Pattern
    Regexp.new(obj.pattern, (obj.flags/2))
  else
    obj
  end
end

#prep_fields(fields) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/jmongo/mongo/utils.rb', line 114

def prep_fields(fields)
  case fields
  when String, Symbol
    {fields => 1}
  when Array
    fields << "_id" if fields.empty?
    Hash[fields.zip( [1]*fields.size )]
  when Hash
    fields
  end
end

#prep_hint(hint) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jmongo/mongo/utils.rb', line 101

def prep_hint(hint)
  case hint
  when String, Symbol
    {hint => 1}
  when Hash
    hint
  when nil
    nil
  else
    Hash[hint.to_a.zip( [1]*hint.size )]
  end
end

#prep_id(doc) ⇒ Object



94
95
96
97
98
99
# File 'lib/jmongo/mongo/utils.rb', line 94

def prep_id(doc)
  if doc[:_id] && !doc['_id']
    doc['_id'] = doc.delete(:_id)
  end
  doc
end

#prep_sort(key_or_list = nil, direction = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/jmongo/mongo/utils.rb', line 126

def prep_sort(key_or_list=nil, direction=nil)
  return if key_or_list.nil?
  if !direction.nil?
    order = [[key_or_list, direction]]
  elsif key_or_list.is_a?(String) || key_or_list.is_a?(Symbol)
    order = [[key_or_list.to_s, 1]]
  else
    order = [key_or_list]
  end
  hord = {}
  order.flatten.each_slice(2){|k,v| hord[k] = sort_value(k,v)}
  to_dbobject(hord)
end

#raise_not_implementedObject

Raises:

  • (NoMethodError)


56
57
58
# File 'lib/jmongo/mongo/utils.rb', line 56

def raise_not_implemented
  raise NoMethodError, "This method hasn't been implemented yet."
end

#sort_value(key, value) ⇒ Object



174
175
176
177
178
179
180
181
182
183
# File 'lib/jmongo/mongo/utils.rb', line 174

def sort_value(key, value)
  val = value.to_s.downcase
  return val if val == '2d'
  direction = SortingHash[val]
  return direction if direction != 0
  raise InvalidSortValueError.new(
    "for key: #{key}, #{value} was supplied as a sort direction when acceptable values are: " +
    "Mongo::ASCENDING, 'ascending', 'asc', :ascending, :asc, 1, Mongo::DESCENDING, " +
    "'descending', 'desc', :descending, :desc, -1.")
end

#system_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/jmongo/mongo/utils.rb', line 68

def system_name?(name)
  name =~ /((^\$cmd)|(oplog\.\$main))/
end

#to_dbobject(obj) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/jmongo/mongo/utils.rb', line 140

def to_dbobject obj
  if obj.respond_to?('to_bson')
    obj.to_bson
  elsif obj.respond_to?(:merge)
    hash_to_dbobject(obj)
  elsif obj.respond_to?(:compact)
    array_to_dblist(obj)
  else
    obj
  end
end

#trap_raise(ex_class, msg = nil) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/jmongo/mongo/utils.rb', line 60

def trap_raise(ex_class, msg=nil)
  begin
    yield
  rescue => ex
    raise ex_class, msg ? "#{msg} - #{ex.message}" : ex.message
  end
end

#validate_name(new_name) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/jmongo/mongo/utils.rb', line 72

def validate_name(new_name)
  unless [String, Symbol].include?(new_name.class)
    raise TypeError, "db_name must be a string or symbol"
  end

  name = new_name.to_s

  if name.empty?
    raise Mongo::InvalidNSName, "collection names cannot be empty"
  end
  if name.include?("..")
    raise Mongo::InvalidNSName, "collection names cannot contain '..'"
  end
  if name.include? "$"
    raise Mongo::InvalidNSName, "collection names cannot contain '$'" unless name =~ /((^\$cmd)|(oplog\.\$main))/
  end
  if name.match(/^\./) || name.match(/\.$/)
    raise Mongo::InvalidNSName, "collection names cannot start or end with '.'"
  end
  name
end