Skip to content

Instantly share code, notes, and snippets.

@spohlenz
Created January 26, 2010 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save spohlenz/287379 to your computer and use it in GitHub Desktop.
Save spohlenz/287379 to your computer and use it in GitHub Desktop.
class Locale
def initialize(identifier)
@identifier = identifier.to_s
end
def to_mongo
@identifier
end
def self.from_mongo(mongo)
new(mongo)
end
end
class Site < MongoModel::Document
property :name, String
property :locales, Collection[Locale], :default => ['en']
end
>> s = Site.new
=> #<Site id: 4b5f7f119b2a8e436f000003, name: nil, locales: [#<Locale:0x1023a7408 @identifier="en">]>
>> s.locales << 'de'
=> [#<Locale:0x1023a7408 @identifier="en">, #<Locale:0x1023981b0 @identifier="de">]
>> s
=> #<Site id: 4b5f7f119b2a8e436f000003, name: nil, locales: [#<Locale:0x1023a7408 @identifier="en">, #<Locale:0x1023981b0 @identifier="de">]>
class ZipCode
def initialize(digits)
@digits = digits.to_s
end
def to_mongo
@digits
end
def self.from_mongo(mongo)
# mongo should be a string
new(mongo)
end
def self.cast(str)
new(str)
end
end
class Address < MongoModel::Document
property :line1, String
property :line2, String
property :city, String
property :zip, ZipCode
end
a = Address.new
=> #<Address id: 4b5f7e479b2a8e436f000001, line1: nil, line2: nil, city: nil, zip: nil>
>> a.zip = 94941
=> 94941
a
=> #<Address id: 4b5f7e499b2a8e436f000002, line1: nil, line2: nil, city: nil, zip: #<ZipCode:0x1023d64b0 @digits="94941">>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment