nosql-tutorial 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,20 +1,31 @@
1
- # Not Only SQL — Data Persistence with Ruby
1
+ # NOSQL – Not Only SQL
2
2
 
3
- ## TODO list
3
+ Gem z notatkami do wykładu „Utrwalanie Danych”.
4
4
 
5
- ### Jakieś aplikacje:
6
5
 
7
- 1. [pastie](http://www.dzone.com/links/datamapper_sinatra_tutorial.html),
8
- [pastie2](http://blog.zerosum.org/2008/7/2/clone-pastie-with-sinatra-datamapper-redux).
9
- 2. [apps written in sinatra, uses datamapper](http://github.com/zapnap/mogo/)
10
- 3. [A simple Twitter application template, built in Ruby with
11
- Sinatra and DataMapper](http://github.com/zapnap/retweet/)
6
+ ## Instalacja
12
7
 
13
- ### Data persistence
8
+ Wykonać polecenie:
14
9
 
15
- 1. [using plain-text files for data persistence](/doc/using-plain-text-files-for-data-persistence.pdf)
16
- 1. Datamapper adapter for Redis:
17
- [DataMapper loves Redis](http://whoahbot.com/2009/05/27/redis-and-datamapper.html)
18
- \([source, example](http://github.com/whoahbot/dm-redis-adapter/)\).
10
+ gem install nosql-tutorial
11
+
12
+
13
+ ## Uruchamianie
14
+
15
+ Sprawdzamy gdzie w systemie został zainstalowany gem *nosql-tutorial*:
16
+
17
+ gem which nosql-tutorial
18
+
19
+ Aplikację uruchamiamy w taki sposób:
20
+
21
+ <pre>rackup /«<i>ścieżka do katalogu z gemem</i>»/lib/config.ru -p «<i>numer portu</i>»
22
+ </pre>
23
+
24
+ Na przykład:
25
+
26
+ rackup rackup /usr/lib/ruby/gems/1.8/gems/nosql-tutorial-0.0.0.0/lib/config.ru -p 8098
27
+
28
+ Po uruchomieniu aplikacja jest dostępna z URL:
29
+
30
+ http://localhost:8098/
19
31
 
20
- 1. redis.spec for Fedora rpm?
data/Rakefile CHANGED
@@ -18,11 +18,11 @@ Notatki do wykładu Utrwalanie Danych.
18
18
  s.files = %w[Rakefile README.markdown VERSION.yml] + FileList["lib/**/*"]
19
19
 
20
20
  s.add_runtime_dependency 'rack'
21
- s.add_runtime_dependency 'sinatra'
21
+ s.add_runtime_dependency 'sinatra', '0.9.4'
22
22
  s.add_runtime_dependency 'rdiscount'
23
23
  s.add_runtime_dependency 'ultraviolet'
24
24
  s.add_runtime_dependency 'rack-codehighlighter'
25
- s.add_runtime_dependency 'sinatra-rdiscount'
25
+ s.add_runtime_dependency 'sinatra-rdiscount', '0.9.4.0'
26
26
  s.add_runtime_dependency 'sinatra-static-assets'
27
27
 
28
28
  s.rubyforge_project = 'nosql'
@@ -1,5 +1,5 @@
1
1
  ---
2
- :major: 0
3
2
  :minor: 1
4
- :patch: 0
3
+ :patch: 1
4
+ :major: 0
5
5
  :build:
@@ -0,0 +1,77 @@
1
+ # run with:
2
+ #
3
+ # clear && spec -c -f specdoc 000-activerecord.rb
4
+
5
+ require 'rubygems'
6
+ require 'spec'
7
+ require 'active_record'
8
+
9
+ require 'activerecord_spec_helper'
10
+
11
+ #ActiveRecord::Base.logger = Logger.new(STDOUT)
12
+
13
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
14
+ #ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => 'test.sqlite3')
15
+
16
+ class CreateStuff < ActiveRecord::Migration
17
+ def self.up
18
+ create_table :dogs do |t|
19
+ t.string :name, :null => false
20
+ end
21
+ create_table :toys do |t|
22
+ t.string :name, :null => false
23
+ end
24
+ create_table :dog_toys do |t|
25
+ t.integer :dog_id, :null => false
26
+ t.integer :toy_id, :null => false
27
+ end
28
+ end
29
+
30
+ def self.down
31
+ end
32
+ end
33
+
34
+ CreateStuff.migrate :up
35
+
36
+ class Dog < ActiveRecord::Base
37
+ validates_presence_of :name
38
+ validates_uniqueness_of :name
39
+
40
+ has_many :dog_toys
41
+ has_many :toys, :through => :dog_toys
42
+ end
43
+
44
+ class DogToy < ActiveRecord::Base
45
+ belongs_to :dog
46
+ belongs_to :toy
47
+ end
48
+
49
+ class Toy < ActiveRecord::Base
50
+ has_many :dog_toys
51
+ has_many :dogs, :through => :dog_toys
52
+ end
53
+
54
+ describe Dog do
55
+
56
+ it 'requires a name' do
57
+ Dog.create(:name => nil ).should_not be_valid
58
+ Dog.create(:name => '' ).should_not be_valid
59
+ Dog.create(:name => 'Rover').should be_valid
60
+ end
61
+
62
+ it 'requires unique name' do
63
+ Dog.create(:name => 'Rover').should be_valid
64
+ Dog.create(:name => 'Rover').should_not be_valid
65
+ Dog.create(:name => 'Spot' ).should be_valid
66
+ end
67
+
68
+ it 'has many toys' do
69
+ rover = Dog.create(:name => 'Rover')
70
+ rover.toys.should be_empty
71
+
72
+ rover.toys.create :name => 'Squeeky Toy'
73
+ rover.toys.length.should == 1
74
+ rover.toys.first.name.should == 'Squeeky Toy'
75
+ end
76
+
77
+ end
@@ -0,0 +1,60 @@
1
+ # -*- coding: utf-8 -*-
2
+ # run with:
3
+ #
4
+ # clear && spec -c -f specdoc 100-datamapper.rb
5
+
6
+ require 'rubygems'
7
+ require 'spec'
8
+ require 'dm-core'
9
+ require 'dm-validations'
10
+
11
+ require 'datamapper_spec_helper'
12
+
13
+ #DataMapper::Logger.new(STDOUT, :debug)
14
+
15
+ class Dog
16
+ include DataMapper::Resource
17
+
18
+ property :id, Serial
19
+ property :name, String, :required => true, :unique => true
20
+
21
+ has n, :toys, :through => Resource
22
+ end
23
+
24
+ class Toy
25
+ include DataMapper::Resource
26
+
27
+ property :id, Serial
28
+ property :name, String, :required => true, :unique => true
29
+ end
30
+
31
+ DataMapper.setup :default, 'sqlite3::memory:'
32
+ #DataMapper.setup :default, 'sqlite3:test.sqlite3'
33
+
34
+
35
+ DataMapper.auto_migrate!
36
+
37
+ describe Dog do
38
+
39
+ it 'requires a name' do
40
+ Dog.create(:name => nil ).should_not be_valid
41
+ Dog.create(:name => '' ).should_not be_valid
42
+ Dog.create(:name => 'Rover').should be_valid
43
+ end
44
+
45
+ it 'requires unique name' do
46
+ Dog.create(:name => 'Rover').should be_valid
47
+ Dog.create(:name => 'Rover').should_not be_valid
48
+ Dog.create(:name => 'Spot' ).should be_valid
49
+ end
50
+
51
+ it 'has many toys' do
52
+ rover = Dog.create(:name => 'Rover')
53
+ rover.toys.should be_empty
54
+
55
+ rover.toys.create :name => 'Squeeky Toy'
56
+ rover.toys.length.should == 1
57
+ rover.toys.first.name.should == 'Squeeky Toy'
58
+ end
59
+
60
+ end
@@ -0,0 +1,13 @@
1
+ # ActiveRecord transactional specs (without Rails)
2
+ Spec::Runner.configure do |config|
3
+ config.before do
4
+ ActiveRecord::Base.connection.begin_db_transaction
5
+ ActiveRecord::Base.connection.increment_open_transactions
6
+ end
7
+ config.after do
8
+ if ActiveRecord::Base.connection.open_transactions != 0
9
+ ActiveRecord::Base.connection.rollback_db_transaction
10
+ ActiveRecord::Base.connection.decrement_open_transactions
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ def transactional_specs rspec_config
2
+ rspec_config.before do
3
+ @_spec_transaction = DataMapper::Transaction.new DataMapper.repository(:default).adapter
4
+ @_spec_transaction.begin
5
+ DataMapper.repository(:default).adapter.push_transaction @_spec_transaction
6
+ end
7
+ rspec_config.after do
8
+ DataMapper.repository(:default).adapter.pop_transaction
9
+ @_spec_transaction.rollback
10
+ end
11
+ end
12
+
13
+ Spec::Runner.configure do |config|
14
+ transactional_specs(config)
15
+ end
@@ -0,0 +1,60 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rubygems'
4
+ require 'restclient'
5
+ require 'json'
6
+
7
+ post1 = {
8
+ "author" => "jacek",
9
+ "title" => "Rails 2",
10
+ "content" => "Bla bla…",
11
+ "comments" => [
12
+ {"author" => "agatka", "content" => "…"},
13
+ {"author" => "bolek", "content" => "…"}
14
+ ]
15
+ }
16
+
17
+ post2 = {
18
+ "author" => "jacek",
19
+ "title" => "Rails 3",
20
+ "content" => "Bla bla bla…",
21
+ "comments" => [
22
+ {"author" => "lolek", "content" => "…"},
23
+ {"author" => "bolek", "content" => "…"}
24
+ ]
25
+ }
26
+
27
+ post3 = {
28
+ "author" => "agatka",
29
+ "title" => "Sinatra 1.0",
30
+ "content" => "Bla…",
31
+ "comments" => [
32
+ {"author" => "jacek", "content" => "…"},
33
+ {"author" => "lolek", "content" => "…"},
34
+ {"author" => "bolek", "content" => "…"}
35
+ ]
36
+ }
37
+
38
+ DB="http://127.0.0.1:5984/blog"
39
+
40
+ RestClient.delete DB rescue nil
41
+
42
+ RestClient.put "#{DB}", ""
43
+
44
+ RestClient.put "#{DB}/01", post1.to_json
45
+ RestClient.put "#{DB}/02", post2.to_json
46
+ RestClient.put "#{DB}/03", post3.to_json
47
+
48
+ __END__
49
+
50
+ RestClient.put "#{DB}/_design/test", <<EOS
51
+ {
52
+ "views":{
53
+ "one":{
54
+ "map":"function (doc) { emit(doc.x,null); }"
55
+ }
56
+ }
57
+ }
58
+ EOS
59
+
60
+ puts RestClient.get("#{DB}/_design/test/_view/one")
@@ -0,0 +1,22 @@
1
+ # file: collseq.rb
2
+ require 'rubygems'
3
+ require 'restclient'
4
+ require 'json'
5
+
6
+ DB="http://127.0.0.1:5984/collator"
7
+ RestClient.delete DB rescue nil
8
+ RestClient.put "#{DB}",""
9
+ (32..126).each do |c|
10
+ RestClient.put "#{DB}/#{c.to_s(16)}", {"x"=>c.chr}.to_json
11
+ end
12
+ RestClient.put "#{DB}/_design/test", <<EOS
13
+ {
14
+ "views":{
15
+ "one":{
16
+ "map":"function (doc) { emit(doc.x,null); }"
17
+ }
18
+ }
19
+ }
20
+ EOS
21
+ puts RestClient.get("#{DB}/_design/test/_view/one")
22
+
@@ -0,0 +1,65 @@
1
+ require 'rubygems'
2
+ require 'dm-core'
3
+
4
+ DataMapper.setup(:default,'sqlite3:test.sqlite3')
5
+
6
+ log = DataMapper::Logger.new(STDOUT, :debug)
7
+
8
+ log.push "==== hello datamapper"
9
+
10
+ class Post
11
+ include DataMapper::Resource
12
+
13
+ property :id, Serial
14
+ property :title, String
15
+ property :body, Text
16
+ property :created_at, DateTime
17
+
18
+ has n, :comments
19
+ end
20
+
21
+ class Comment
22
+ include DataMapper::Resource
23
+
24
+ property :id, Serial
25
+ property :posted_by, String
26
+ property :email, String
27
+ property :url, String
28
+ property :body, Text
29
+
30
+ belongs_to :post
31
+ end
32
+
33
+ class Category
34
+ include DataMapper::Resource
35
+
36
+ property :id, Serial
37
+ property :name, String
38
+ end
39
+
40
+ class Categorization
41
+ include DataMapper::Resource
42
+
43
+ property :id, Serial
44
+ property :created_at, DateTime
45
+
46
+ belongs_to :category
47
+ belongs_to :post
48
+ end
49
+
50
+ class Post
51
+ has n, :categorizations
52
+ has n, :categories, :through => :categorizations
53
+ end
54
+
55
+ class Category
56
+ has n, :categorizations
57
+ has n, :posts, :through => :categorizations
58
+ end
59
+
60
+ # Post.auto_migrate!
61
+ # Category.auto_migrate!
62
+ # Comment.auto_migrate!
63
+ # Categorization.auto_migrate!
64
+
65
+ DataMapper.auto_migrate!
@@ -0,0 +1,69 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rubygems'
4
+ require 'dm-core'
5
+ require 'dm-timestamps'
6
+ require 'extlib'
7
+
8
+ Extlib::Inflection.plural_word 'film', 'filmy'
9
+ Extlib::Inflection.plural_word 'recenzja', 'recenzje'
10
+ Extlib::Inflection.plural_word 'kino', 'kina'
11
+
12
+ Extlib::Inflection.plural_word 'sesja', 'sesje'
13
+ # bug:
14
+ # jeśli nazwa modelu kończy się na 's', to
15
+ # datamapper głupieje, np.:
16
+ # Extlib::Inflection.plural_word 'seans', 'seanse'
17
+
18
+ DataMapper.setup :default, 'sqlite3:ale-kino.sqlite3'
19
+
20
+ log = DataMapper::Logger.new(STDOUT, :debug)
21
+
22
+ #log.push "==== hello datamapper"
23
+
24
+ class Film
25
+ include DataMapper::Resource
26
+ property :id, Serial
27
+ property :name, String
28
+ property :minutes, Integer
29
+ property :average_stars, Float
30
+ timestamps :at
31
+
32
+ has n, :recenzja
33
+ has n, :sesja
34
+ has n, :kino, :through => :sesja
35
+ end
36
+
37
+ class Recenzja
38
+ include DataMapper::Resource
39
+ property :id, Serial
40
+ property :author_name, String
41
+ property :stars, Integer
42
+ property :content, Text
43
+ timestamps :at
44
+
45
+ belongs_to :film
46
+ end
47
+
48
+ class Kino
49
+ include DataMapper::Resource
50
+ property :id, Serial
51
+ property :name, String
52
+ timestamps :at
53
+
54
+ has n, :sesja
55
+ has n, :film, :through => :sesja
56
+ end
57
+
58
+ class Sesja
59
+ include DataMapper::Resource
60
+ property :id, Serial
61
+ property :starts_on, Date
62
+ property :ends_on, Date
63
+ timestamps :at
64
+
65
+ belongs_to :kino
66
+ belongs_to :film
67
+ end
68
+
69
+ DataMapper.auto_migrate!