Created
December 29, 2010 18:09
-
-
Save hartbit/758818 to your computer and use it in GitHub Desktop.
DataMapper Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'dm-core' | |
require 'dm-migrations' | |
require 'dm-serializer' | |
DataMapper::Logger.new($stdout, :debug) | |
DataMapper.setup(:default, 'sqlite:test.sqlite') | |
# ========================================= | |
# MODEL | |
# ========================================= | |
class Device | |
include DataMapper::Resource | |
property :id, Serial | |
property :udid, String, :required => true | |
has n, :apps, :through => Resource | |
end | |
class App | |
include DataMapper::Resource | |
property :id, Serial | |
property :bundle_id, String, :required => true | |
has n, :devices, :through => Resource | |
def to_json(*args) | |
json_hash = { "bundle_id" => bundle_id } | |
if args.first[:deep] then | |
json_hash["devices"] = devices.to_json(*args) | |
end | |
json_hash.to_json(*args) | |
end | |
def self.json_create(object) | |
new(*object) | |
end | |
end | |
DataMapper.finalize | |
DataMapper.auto_upgrade! | |
# ========================================= | |
# TEST | |
# ========================================= | |
Device.first_or_create(:udid => 'd1') | |
Device.first_or_create(:udid => 'd2') | |
Device.first_or_create(:udid => 'd3') | |
Device.first_or_create(:udid => 'd4') | |
Device.first_or_create(:udid => 'd5') | |
Device.first_or_create(:udid => 'd6') | |
app = App.first_or_create(:bundle_id => 'a') | |
udids = ['d2', 'd4', 'd6'] | |
app.update(:devices => udids.map {|udid| Device.first_or_create(:udid => udid) }) | |
puts app.devices | |
puts "Device.all(:apps => nil) = #{Device.all(:apps => nil).inspect}" | |
puts "Device.all(:apps => []) = #{Device.all(:apps => []).inspect}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment