Permalink
Browse files

Added graph creator and example. Minor denormalisation fix.

  • Loading branch information...
1 parent 8e81263 commit cd63e0d51a293cd578523de2e8f9dec472203853 @paulcarey committed Sep 11, 2008
Showing with 76 additions and 8 deletions.
  1. +1 −0 .gitignore
  2. +42 −0 lib/more/grapher.rb
  3. +1 −0 lib/relaxdb.rb
  4. +5 −3 lib/relaxdb/document.rb
  5. +1 −5 lib/relaxdb/relaxdb.rb
  6. +1 −0 relaxdb.gemspec
  7. +21 −0 scratch/grapher_demo.rb
  8. +4 −0 spec/denormalisation_spec.rb
View
@@ -1,3 +1,4 @@
+graphs
pkg
.irb_history
uuid.state
View
@@ -0,0 +1,42 @@
+module RelaxDB
+
+ class GraphCreator
+
+ def self.create
+ system "mkdir -p graphs"
+
+ data = JSON.parse(RelaxDB.db.get("_all_docs").body)
+ all_ids = data["rows"].map { |r| r["id"] }
+ all_ids = all_ids.reject { |id| id =~ /_/ }
+
+ dot = "digraph G { \nrankdir=LR;\nnode [shape=record];\n"
+ all_ids.each do |id|
+ doc = RelaxDB.load(id)
+ atts = "#{doc.class}\\l|"
+ doc.properties.each do |prop|
+ # we don't care about the revision
+ next if prop == :_rev
+
+ prop_val = doc.instance_variable_get("@#{prop}".to_sym)
+ atts << "#{prop}\\l#{prop_val}|" if prop_val
+ end
+ atts = atts[0, atts.length-1]
+
+ dot << %Q%#{doc._id} [ label ="#{atts}"];\n%
+
+ doc.class.belongs_to_rels.each do |relationship, opts|
+ id = doc.instance_variable_get("@#{relationship}_id".to_sym)
+ dot << %Q%#{id} -> #{doc._id} [ label = "#{relationship}"];\n% if id
+ end
+
+ end
+ dot << "}"
+
+ File.open("graphs/data.dot", "w") { |f| f.write(dot) }
+
+ system "dot -Tpng -o graphs/all_docs.png graphs/data.dot"
+ end
+
+ end
+
+end
View
@@ -29,6 +29,7 @@
require 'relaxdb/view_object'
require 'relaxdb/view_uploader'
require 'relaxdb/views'
+require 'more/grapher.rb'
module RelaxDB
end
View
@@ -128,9 +128,11 @@ def to_json
# permanent fixture of RelaxDB, but quite likely in a different form to this one
def add_denormalised_data(data, relationship, opts)
obj = send(relationship)
- opts[:denormalise].each do |prop_name|
- val = obj.send(prop_name)
- data["#{relationship}_#{prop_name}"] = val
+ if obj
+ opts[:denormalise].each do |prop_name|
+ val = obj.send(prop_name)
+ data["#{relationship}_#{prop_name}"] = val
+ end
end
end
View
@@ -97,11 +97,7 @@ def reduce_result(data)
end
def create_from_hash(data)
- @objects = []
- data["rows"].each do |row|
- @objects << create_object(row["value"])
- end
- @objects
+ data["rows"].map { |row| create_object(row["value"]) }
end
def create_object(data)
View
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
"lib/relaxdb/view_object.rb",
"lib/relaxdb/view_uploader.rb",
"lib/relaxdb/views.rb",
+ "lib/more/grapher.rb",
"lib/relaxdb.rb",
"spec/belongs_to_spec.rb",
"spec/callbacks_spec.rb",
View
@@ -0,0 +1,21 @@
+require 'rubygems'
+require 'relaxdb'
+require 'spec/spec_models'
+
+# Configure RelaxDB
+RelaxDB::UuidGenerator.id_length = 5
+RelaxDB.configure :host => "localhost", :port => 5984
+RelaxDB.delete_db "relaxdb_grapher_demo_db" rescue "ok"
+RelaxDB.use_db "relaxdb_grapher_demo_db"
+
+# Create the data
+paul = User.new(:name => "paul").save
+gromit = User.new(:name => "gromit", :age => 8).save
+
+paul.items << Item.new(:name => "dog brush")
+paul.items << Item.new(:name => "futon")
+
+gromit.invites_sent << Invite.new(:message => "sheep herding", :recipient => paul)
+
+# Create the graph
+RelaxDB::GraphCreator.create
@@ -31,6 +31,10 @@ class Leaf < RelaxDB::Document
obj = JSON.parse(leaf.to_json)
obj["tree_name"].should == "sapling"
end
+
+ it "should ignore denormalised options for nil properties" do
+ Leaf.new.to_json
+ end
it "should not interfere with normal belongs_to behaviour" do
tree = Tree.new(:name => "sapling", :climate => "tropical").save

0 comments on commit cd63e0d

Please sign in to comment.