public
Description: Fork of DataMapper 0.3 with patches to fix major show-stopping bugs
Homepage:
Clone URL: git://github.com/cardmagic/dm-works.git
cardmagic (author)
Thu Apr 24 15:12:22 -0700 2008
commit  4c6160d17c3cfceee66bb7a8cf6cd11d2016af2f
tree    53f374194d738f279b78d3c970312a018d007a27
parent  93ad19c37c9a978525ea260483ba7ecfb8877184
dm-works / example.rb
100755 157 lines (121 sloc) 3.885 kb
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
#!/usr/bin/env ruby
 
ENV['LOG_NAME'] ||= 'example'
require 'environment'
 
# Define a fixtures helper method to load up our test data.
def fixtures(name)
  entry = YAML::load_file(File.dirname(__FILE__) + "/spec/fixtures/#{name}.yaml")
  klass = begin
    Kernel::const_get(Inflector.classify(Inflector.singularize(name)))
  rescue
    nil
  end
 
  unless klass.nil?
    database.logger.debug { "AUTOMIGRATE: #{klass}" }
    klass.auto_migrate!
 
    (entry.kind_of?(Array) ? entry : [entry]).each do |hash|
      if hash['type']
        Object::const_get(hash['type'])::create(hash)
      else
        klass::create(hash)
      end
    end
  else
    table = database.table(name.to_s)
    table.create! true
    table.activate_associations!
 
    #pp database.schema
 
    (entry.kind_of?(Array) ? entry : [entry]).each do |hash|
      table.insert(hash)
    end
  end
end
 
 
# Pre-fill the database so non-destructive tests don't need to reload fixtures.
Dir[File.dirname(__FILE__) + "/spec/fixtures/*.yaml"].each do |path|
  fixtures(File::basename(path).sub(/\.yaml$/, ''))
end
 
require 'irb'
 
# database { IRB::start }
IRB::start
 
if false
 
# Simple example to setup a database:
DataMapper::Database.setup({
  :adapter => 'mysql',
  :database => 'data_mapper_1',
  :username => 'root'
})
 
class Animal #:nodoc:
  include DataMapper::Persistence
  
  set_table_name 'animals' # Just as an example. Same inflector as Rails,
    # so this really isn't necessary.
    
  property :name, :string
  property :notes, :string, :lazy => true
  
  has_and_belongs_to_many :exhibits
end
 
class Exhibit #:nodoc:
  include DataMapper::Persistence
 
  property :name, :string
  belongs_to :zoo
end
 
class Zoo #:nodoc:
  include DataMapper::Persistence
 
  property :name, :string
  has_many :exhibits
end
 
class Person #:nodoc:
  include DataMapper::Persistence
 
  property :name, :string
  property :age, :integer
  property :occupation, :string
  property :notes, :text, :lazy => true
 
  # Generates Person::Address class:
  embed :address do
    property :street, :string
    property :city, :string
    property :state, :string, :size => 2
    property :postal_code, :string
  end
end
 
# Compatible with ActiveRecord finder syntax:
Zoo.find(1)
Zoo.find(:first, :conditions => ['name = ?', 'Galveston'])
Zoo.find(:all)
 
# These are options as well:
Zoo[1]
Zoo.first(:name => 'Galveston')
Zoo.all
 
# Or even this as an alias to ::first:
Zoo[:name => 'Galveston']
 
# EmbeddedValues are just nice sugar to partition
# denormalized data.
Person.first.address.city
 
# Remove all data in a table...
Person.truncate!
  
# Create a new object...
Person::create(:name => 'Sam', :age => 30, :occupation => 'Software Monkey')
 
# Saving only updates the values that have changed,
# and is skipped entirely if the object is not dirty.
dumbo = Animal.first(:name => 'Elephant')
dumbo.notes = 'He can fly!'
dumbo.save # returns true
dumbo.save # The object is no longer dirty, so returns false
  
# DataMapper associations are loaded as sets.
# Here's the code:
Zoo.all.each { |zoo| zoo.exhibits.entries }
# The important bit to understand about the above is that
# every Zoo that was loaded by Zoo.all has a reference to
# every other Zoo it was loaded with through Zoo#loaded_set.
# This is then used to load all other instances in the set
# when the association of one instance is accessed. So while
# it looks like we'd run into the dreaded 1+N query problem
# with the above, we actually avoid it entirely. The above
# code will only execute two queries. The first to find all
# zoos, the second to load all exhibits with zoo_id's that
# are a part of the set of loaded zoos.
 
 
# Objects within the same session are uniqued, so this is both
# faster, and fulfills obvious expectations.
database do
  Zoo.first == Zoo.first
end
 
# DataMapper find_by_sql equivilent
database.query("SELECT * FROM zoos")
 
end