Created
November 23, 2009 00:13
-
-
Save ravinggenius/240807 to your computer and use it in GitHub Desktop.
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
# book.rb | |
require 'dm-is-list' | |
class Book | |
include DataMapper::Resource | |
property :id, Serial | |
property :name, String, :nullable => false | |
is :list | |
has n, :chapters | |
end | |
# chapter.rb | |
class Chapter | |
include DataMapper::Resource | |
property :id, Serial | |
property :number, Integer, :nullable => false | |
belongs_to :book | |
has n, :verses | |
end | |
# verse.rb | |
class Verse | |
include DataMapper::Resource | |
property :id, Serial | |
property :text, Text, :nullable => false | |
property :number, Integer, :nullable => false | |
belongs_to :chapter | |
end |
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 'rubygems' | |
require 'datamapper' | |
require 'merb_datamapper' | |
require 'pp' | |
require 'app/models/book' | |
require 'app/models/chapter' | |
require 'app/models/verse' | |
merb_root = File.expand_path(File.dirname(__FILE__) / '../') | |
DataMapper.setup(:default, "sqlite3://#{merb_root}/sample_development.db") | |
B = Struct.new :position, :name, :chapters | |
C = Struct.new :number, :verses | |
V = Struct.new :number, :text | |
books = [] | |
books << B.new(1, 'Genesis', [ | |
C.new(1, [ | |
V.new(1, 'In the beginning, God created the heavens and the earth.'), | |
V.new(2, 'And the earth was without form and void, and darkness covered the face of the deep.') | |
]), | |
C.new(2, [ | |
V.new(1, 'book 1, chapter 2, verse 1'), | |
V.new(2, 'book 1, chapter 2, verse 2') | |
]) | |
]) | |
books << B.new(2, 'Exodus', [ | |
C.new(1, [ | |
V.new(1, 'Thou shalt love the Lord thy God.') | |
]), | |
C.new(2, [ | |
V.new(1, 'book 2, chapter 2, verse 1'), | |
V.new(2, 'book 2, chapter 2, verse 2') | |
]), | |
C.new(3, [ | |
V.new(1, 'book 2, chapter 3, verse 1'), | |
V.new(2, 'book 2, chapter 3, verse 2') | |
]) | |
]) | |
puts 'Inserting the following data:' | |
puts | |
books.each do |b| | |
book = Book.new | |
book.name = b.name | |
book.position = b.position | |
book.save | |
puts "b.position => #{b.position}" | |
pp book | |
b.chapters.each do |c| | |
chapter = Chapter.new | |
chapter.book = book | |
chapter.number = c.number | |
chapter.save | |
puts "c.number => #{c.number}" | |
pp chapter | |
c.verses.each do |v| | |
verse = Verse.new | |
verse.chapter = chapter | |
verse.number = v.number | |
verse.text = v.text | |
verse.save | |
puts "v.number => #{v.number}" | |
pp verse | |
end | |
puts unless c.verses.last == c | |
end | |
puts unless books.last == b | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment