github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

jnunemaker / mongomapper

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 714
    • 142
  • Source
  • Commits
  • Network (142)
  • Downloads (33)
  • Wiki (4)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (33)
    • v0.6.10
    • v0.6.9
    • v0.6.8
    • v0.6.7
    • v0.6.6
    • v0.6.5
    • v0.6.4
    • v0.6.3
    • v0.6.2
    • v0.6.1
    • v0.6.0
    • v0.5.8
    • v0.5.7
    • v0.5.6
    • v0.5.5
    • v0.5.4
    • v0.5.3
    • v0.5.2
    • v0.5.1
    • v0.5.0
    • v0.4.2
    • v0.4.1
    • v0.4.0
    • v0.3.5
    • v0.3.4
    • v0.3.3
    • v0.3.2
    • v0.3.1
    • v0.3.0
    • v0.2.0
    • v0.1.2
    • v0.1.1
    • v0.1.0
Sending Request…
Click here to lend your support to: mongomapper and make a donation at www.pledgie.com ! Edit Pledgie Setup

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Awesome gem for modeling your domain and storing it in mongo — Read more

  cancel

http://mongomapper.com

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Moved some things around and autoloaded some stuff. 
jnunemaker (author)
Tue Feb 09 17:35:17 -0800 2010
commit  67590db2835efc1110cdd3057e07a01072123d0e
tree    aa6b6ccf9856fc6234e793cbb8e5f71f3b961111
parent  64c24faa470e10cbd330afdaa08493b593441479
mongomapper / lib / mongo_mapper / support.rb lib/mongo_mapper/support.rb
100644 216 lines (181 sloc) 4.088 kb
edit raw blame history
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
class Array
  def self.to_mongo(value)
    value = value.respond_to?(:lines) ? value.lines : value
    value.to_a
  end
  
  def self.from_mongo(value)
    value || []
  end
end
 
class Binary
  def self.to_mongo(value)
    if value.is_a?(ByteBuffer)
      value
    else
      value.nil? ? nil : ByteBuffer.new(value)
    end
  end
 
  def self.from_mongo(value)
    value
  end
end
 
class Boolean
  BOOLEAN_MAPPING = {
    true => true, 'true' => true, 'TRUE' => true, 'True' => true, 't' => true, 'T' => true, '1' => true, 1 => true, 1.0 => true,
    false => false, 'false' => false, 'FALSE' => false, 'False' => false, 'f' => false, 'F' => false, '0' => false, 0 => false, 0.0 => false, nil => false
  }
  
  def self.to_mongo(value)
    if value.is_a?(Boolean)
      value
    else
      v = BOOLEAN_MAPPING[value]
      v = value.to_s.downcase == 'true' if v.nil? # Check all mixed case spellings for true
      v
    end
  end
 
  def self.from_mongo(value)
    !!value
  end
end
 
class Date
  def self.to_mongo(value)
    if value.nil? || value == ''
      nil
    else
      date = value.is_a?(Date) || value.is_a?(Time) ? value : Date.parse(value.to_s)
      Time.utc(date.year, date.month, date.day)
    end
  rescue
    nil
  end
  
  def self.from_mongo(value)
    value.to_date if value.present?
  end
end
 
class Float
  def self.to_mongo(value)
    value.to_f
  end
end
 
class Hash
  def self.from_mongo(value)
    HashWithIndifferentAccess.new(value || {})
  end
  
  def to_mongo
    self
  end
end
 
class Integer
  def self.to_mongo(value)
    value_to_i = value.to_i
    if value_to_i == 0 && value != value_to_i
      value.to_s =~ /^(0x|0b)?0+/ ? 0 : nil
    else
      value_to_i
    end
  end
end
 
class NilClass
  def to_mongo(value)
    value
  end
  
  def from_mongo(value)
    value
  end
end
 
class Object
  # The hidden singleton lurks behind everyone
  def metaclass
    class << self; self end
  end
 
  def meta_eval(&blk)
    metaclass.instance_eval(&blk)
  end
 
  # Adds methods to a metaclass
  def meta_def(name, &blk)
    meta_eval { define_method(name, &blk) }
  end
 
  # Defines an instance method within a class
  def class_def(name, &blk)
    class_eval { define_method(name, &blk) }
  end
  
  def self.to_mongo(value)
    value
  end
  
  def self.from_mongo(value)
    value
  end
end
 
class ObjectId
  def self.to_mongo(value)
    if value.blank?
      nil
    elsif value.is_a?(Mongo::ObjectID)
      value
    else
      Mongo::ObjectID.from_string(value.to_s)
    end
  end
  
  def self.from_mongo(value)
    value
  end
end
 
class Set
  def self.to_mongo(value)
    value.to_a
  end
  
  def self.from_mongo(value)
    Set.new(value || [])
  end
end
 
class String
  def self.to_mongo(value)
    value.nil? ? nil : value.to_s
  end
  
  def self.from_mongo(value)
    value.nil? ? nil : value.to_s
  end
end
 
class SymbolOperator
  attr_reader :field, :operator
 
  def initialize(field, operator, options={})
    @field, @operator = field, operator
  end unless method_defined?(:initialize)
end
 
class Symbol
  %w(gt lt gte lte ne in nin mod all size where exists asc desc).each do |operator|
    define_method(operator) do
      SymbolOperator.new(self, operator)
    end unless method_defined?(operator)
  end
end
 
class Time
  def self.to_mongo(value)
    if value.nil? || value == ''
      nil
    else
      time = value.is_a?(Time) ? value : MongoMapper.time_class.parse(value.to_s)
      # Convert time to milliseconds since BSON stores dates with that accurracy, but Ruby uses microseconds
      Time.at((time.to_f * 1000).round / 1000.0).utc if time
    end
  end
  
  def self.from_mongo(value)
    if MongoMapper.use_time_zone? && value.present?
      value.in_time_zone(Time.zone)
    else
      value
    end
  end
end
 
class Mongo::ObjectID
  alias_method :original_to_json, :to_json
  
  def to_json(options = nil)
    %Q("#{to_s}")
  end
end
 
module MongoMapper
  module Support
    autoload :DescendantAppends, 'mongo_mapper/support/descendant_appends'
    autoload :Find, 'mongo_mapper/support/find'
  end
end
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server