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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/jmongo/mongo/utils.rb', line 109

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



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/jmongo/mongo/utils.rb', line 71

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



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jmongo/mongo/utils.rb', line 58

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



51
52
53
54
55
56
# File 'lib/jmongo/mongo/utils.rb', line 51

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



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jmongo/mongo/utils.rb', line 83

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)


13
14
15
# File 'lib/jmongo/mongo/utils.rb', line 13

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

#sort_value(key, value) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/jmongo/mongo/utils.rb', line 131

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)


25
26
27
# File 'lib/jmongo/mongo/utils.rb', line 25

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

#to_dbobject(obj) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/jmongo/mongo/utils.rb', line 97

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



17
18
19
20
21
22
23
# File 'lib/jmongo/mongo/utils.rb', line 17

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jmongo/mongo/utils.rb', line 29

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