Skip to content

Commit

Permalink
Minor changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcarey committed Apr 15, 2010
1 parent 162836c commit 7c7bbca
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 28 deletions.
34 changes: 11 additions & 23 deletions lib/relaxdb/document.rb
Expand Up @@ -8,14 +8,15 @@ class Document
attr_accessor :errors

# A call issued to save_all will save this object and the
# contents of the save_list. This allows secondary object to
# contents of the save_list. This allows secondary objects to
# be saved at the same time as this object.
attr_accessor :save_list

# Attribute symbols added to this list won't be validated on save
attr_accessor :validation_skip_list

# Not part of the public API - should only be used by clients with caution
# Not part of the public API - should only be used by clients with caution.
# The data keys are Strings as this is what JSON.parse gives us.
attr_accessor :data

class_inheritable_accessor :properties, :reader => true
Expand All @@ -33,7 +34,6 @@ class Document
class_inheritable_accessor :references_rels, :reader => true
self.references_rels = {}

# TODO: consider freezing prop names - more upto json - nothing for me to do
def self.property(prop, opts={})
properties << prop

Expand Down Expand Up @@ -134,17 +134,14 @@ def write_derived_props(source)
end
end

# Lots of tests pass in hash with symbols - we want strings only. or maybe not.
def initialize(hash={})
@errors = Errors.new
@save_list = []
@validation_skip_list = []

# If there's no rev, it's a new document
if hash["_rev"].nil?
# If there's no rev, it's a new document. Clients may use symbols
# as keys so convert all to string first.
# The data keys are String as this is what JSON.parse gives us.
# Using symbols as keys is mostly swimming uphill
# Clients may use symbols as keys so convert all to strings first.
@data = hash.map { |k,v| [k.to_s, v] }.to_hash
else
@data = hash
Expand All @@ -155,7 +152,8 @@ def initialize(hash={})
@data["_id"] = UuidGenerator.uuid
end

# Set default properties if this object isn't being loaded from CouchDB
# It's a new doc, set default properties. We only do this after ensuring
# this obj first has an _id.
unless @data["_rev"]
default_methods = methods.select { |m| m =~ /__set_default/ }
default_methods.map! { |m| m.to_sym } if RUBY_VERSION.to_f < 1.9
Expand All @@ -168,40 +166,30 @@ def initialize(hash={})
@data.each do |key, val|
send("#{key}=".to_sym, val)
end

@data["relaxdb_class"] = self.class.name
end
end

def inspect
s = "#<#{self.class}:#{self.object_id}"
properties.each do |prop|
prop_val = instance_variable_get("@#{prop}".to_sym)
prop_val = @data[prop.to_s]
s << ", #{prop}: #{prop_val.inspect}" if prop_val
end
self.class.references_rels.each do |relationship, opts|
id = instance_variable_get("@#{relationship}_id".to_sym)
id = @data["#{relationship}_id"]
s << ", #{relationship}_id: #{id}" if id
end
s << ", errors: #{errors.inspect}" unless errors.empty?
s << ", save_list: #{save_list.map { |o| o.inspect }.join ", " }" unless save_list.empty?
s << ">"

to_json.gsub("\"", "")
end

alias_method :to_s, :inspect

def to_json
# data = {}
# self.class.references_rels.each do |relationship, opts|
# id = instance_variable_get("@#{relationship}_id".to_sym)
# data["#{relationship}_id"] = id if id
# end
# properties.each do |prop|
# prop_val = instance_variable_get("@#{prop}".to_sym)
# data["#{prop}"] = prop_val if prop_val
# end
@data["errors"] = errors unless errors.empty?
@data["relaxdb_class"] = self.class.name
@data.to_json
end

Expand Down
10 changes: 8 additions & 2 deletions lib/relaxdb/relaxdb.rb
Expand Up @@ -124,14 +124,14 @@ def load(ids, atts={})
if ids.is_a? Array
resp = db.post("_all_docs?include_docs=true", {:keys => ids}.to_json)
data = JSON.parse(resp.body)
data["rows"].map { |row| row["doc"] ? create_object(row["doc"]) : nil }
data["rows"].map { |row| row["doc"] ? create_obj_from_doc(row["doc"]) : nil }
else
begin
qs = atts.map{ |k, v| "#{k}=#{v}" }.join("&")
qs = atts.empty? ? ids : "#{ids}?#{qs}"
resp = db.get qs
data = JSON.parse resp.body
create_object data
create_obj_from_doc data
rescue HTTP_404
nil
end
Expand Down Expand Up @@ -299,6 +299,12 @@ def create_object(data)
ViewObject.create data
end
end

def create_obj_from_doc(data)
klass = data["relaxdb_class"]
k = klass.split("::").inject(Object) { |x, y| x.const_get y }
k.new data
end

# Convenience methods - should be in a diffent module?

Expand Down
4 changes: 2 additions & 2 deletions spec/design_doc_spec.rb
Expand Up @@ -15,8 +15,8 @@
describe "#save" do

it "should create a corresponding document in CouchDB" do
RelaxDB::DesignDocument.get("foo").save
RelaxDB.load("_design/foo").should_not be_nil
RelaxDB::DesignDocument.get("foo").save
RelaxDB::DesignDocument.get("foo").should_not be_nil
end

end
Expand Down
2 changes: 1 addition & 1 deletion spec/relaxdb_spec.rb
Expand Up @@ -85,7 +85,7 @@
describe "all-or-nothing" do
it "should save non conflicting and conflicting docs" do
p1, p2 = Primitives.new(:num => 1).save!, Primitives.new(:num => 2).save!
p1d = Primitives.new(:_id => p1._id, :_rev => p1._rev)
p1d = Primitives.new("_id" => p1._id, "_rev" => p1._rev, "relaxdb_class" => "Primitives")
p1d.num = 11
p1d.save!
p1.num = 6
Expand Down

0 comments on commit 7c7bbca

Please sign in to comment.