Skip to content

Commit

Permalink
Removed has_one, has_n and references_many. Renamed belongs_to to ref…
Browse files Browse the repository at this point in the history
…erences.

All specs passing.
  • Loading branch information
paulcarey committed Apr 15, 2010
1 parent e3cb89b commit 6a79a2c
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 1,040 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require 'spec/rake/spectask'

PLUGIN = "relaxdb"
NAME = "relaxdb"
GEM_VERSION = "0.4"
GEM_VERSION = "0.5"
AUTHOR = "Paul Carey"
EMAIL = "paul.p.carey@gmail.com"
HOMEPAGE = "http://github.com/paulcarey/relaxdb/"
Expand Down
6 changes: 3 additions & 3 deletions docs/spec_results.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ <h1>RSpec Results</h1>
</div>
<div class="example_group">
<dl>
<dt id="example_group_2">RelaxDB::BelongsToProxy belongs_to</dt>
<dt id="example_group_2">RelaxDB::BelongsToProxy references</dt>
<script type="text/javascript">moveProgressBar('0.4');</script>
<dd class="spec passed"><span class="passed_spec_name">should return nil when accessed before assignment</span></dd>
<script type="text/javascript">moveProgressBar('0.9');</script>
Expand All @@ -208,7 +208,7 @@ <h1>RSpec Results</h1>
</div>
<div class="example_group">
<dl>
<dt id="example_group_3">RelaxDB::BelongsToProxy belongs_to validator</dt>
<dt id="example_group_3">RelaxDB::BelongsToProxy references validator</dt>
<script type="text/javascript">moveProgressBar('5.0');</script>
<dd class="spec passed"><span class="passed_spec_name">should be passed the _id and object</span></dd>
<script type="text/javascript">moveProgressBar('5.5');</script>
Expand Down Expand Up @@ -620,7 +620,7 @@ <h1>RSpec Results</h1>
<dl>
<dt id="example_group_42">RelaxDB::HasManyProxy has_many#delete</dt>
<script type="text/javascript">moveProgressBar('51.8');</script>
<dd class="spec passed"><span class="passed_spec_name">should nullify the belongs_to relationship</span></dd>
<dd class="spec passed"><span class="passed_spec_name">should nullify the references relationship</span></dd>
</dl>
</div>
<div class="example_group">
Expand Down
2 changes: 1 addition & 1 deletion lib/more/grapher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def self.create

dot << %Q%#{doc._id} [ label ="#{atts}"];\n%

doc.class.belongs_to_rels.each do |relationship, opts|
doc.class.references_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
Expand Down
5 changes: 1 addition & 4 deletions lib/relaxdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@
end

require 'relaxdb/all_delegator'
require 'relaxdb/belongs_to_proxy'
require 'relaxdb/design_doc'
require 'relaxdb/document'
require 'relaxdb/extlib'
require 'relaxdb/has_many_proxy'
require 'relaxdb/has_one_proxy'
require 'relaxdb/migration'
require 'relaxdb/paginate_params'
require 'relaxdb/paginator'
require 'relaxdb/query'
require 'relaxdb/references_many_proxy'
require 'relaxdb/references_proxy'
require 'relaxdb/relaxdb'
require 'relaxdb/server'
require 'relaxdb/uuid_generator'
Expand Down
150 changes: 20 additions & 130 deletions lib/relaxdb/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class Document
class_inheritable_accessor :__view_by_list__
self.__view_by_list__ = []

class_inheritable_accessor :belongs_to_rels, :reader => true
self.belongs_to_rels = {}
class_inheritable_accessor :references_rels, :reader => true
self.references_rels = {}

def self.property(prop, opts={})
properties << prop
Expand Down Expand Up @@ -161,6 +161,7 @@ def initialize(hash={})
end

unless @data["_id"]
# TODO - use uuids from CouchDB (sequential) and convert to base 36
@data["_id"] = UuidGenerator.uuid
end

Expand All @@ -176,14 +177,15 @@ def initialize(hash={})

# This may become redundant as all items are served only
# from the underlying hash - if we ever set, we always want to derive
@set_derived_props = true
# @set_derived_props = true

@data.each do |key, val|
send("#{key}=".to_sym, val)
end
end
end

# TODO - delete
def set_attributes(data)
data.each do |key, val|
# Only set instance variables on creation - object references are resolved on demand
Expand All @@ -207,7 +209,7 @@ def inspect
prop_val = instance_variable_get("@#{prop}".to_sym)
s << ", #{prop}: #{prop_val.inspect}" if prop_val
end
self.class.belongs_to_rels.each do |relationship, opts|
self.class.references_rels.each do |relationship, opts|
id = instance_variable_get("@#{relationship}_id".to_sym)
s << ", #{relationship}_id: #{id}" if id
end
Expand All @@ -222,7 +224,7 @@ def inspect

def to_json
# data = {}
# self.class.belongs_to_rels.each do |relationship, opts|
# self.class.references_rels.each do |relationship, opts|
# id = instance_variable_get("@#{relationship}_id".to_sym)
# data["#{relationship}_id"] = id if id
# end
Expand Down Expand Up @@ -303,8 +305,8 @@ def validates?
props = properties - validation_skip_list
prop_vals = props.map { |prop| @data[prop.to_s] }

rels = self.class.belongs_to_rels.keys - validation_skip_list
rel_vals = rels.map { |rel| @data[rel.to_s] }
rels = self.class.references_rels.keys - validation_skip_list
rel_vals = rels.map { |rel| @data["#{rel}_id"] }

att_names = props + rels
att_vals = prop_vals + rel_vals
Expand Down Expand Up @@ -385,117 +387,28 @@ def create_or_get_proxy(klass, relationship, opts=nil)
def ==(other)
other && _id == other._id
end

# If you're using this method, read the specs and make sure you understand
# how it can be used and how it shouldn't be used
def self.references_many(relationship, opts={})
# Treat the representation as a standard property
properties << relationship

# Keep track of the relationship so peers can be disassociated on destroy
@references_many_rels ||= []
@references_many_rels << relationship

id_arr_sym = "@#{relationship}".to_sym

if RelaxDB.create_views?
target_class = opts[:class]
relationship_as_viewed_by_target = opts[:known_as].to_s
ViewCreator.references_many(self.name, relationship, target_class, relationship_as_viewed_by_target).add_to_design_doc
end

define_method(relationship) do
instance_variable_set(id_arr_sym, []) unless instance_variable_defined? id_arr_sym
create_or_get_proxy(ReferencesManyProxy, relationship, opts)
end

define_method("#{relationship}_ids") do
instance_variable_set(id_arr_sym, []) unless instance_variable_defined? id_arr_sym
instance_variable_get(id_arr_sym)
end

define_method("#{relationship}=") do |val|
# Don't invoke this method unless you know what you're doing
instance_variable_set(id_arr_sym, val)
end
end

def self.references_many_rels
@references_many_rels ||= []
end

def self.has_many(relationship, opts={})
@has_many_rels ||= []
@has_many_rels << relationship

if RelaxDB.create_views?
target_class = opts[:class] || relationship.to_s.singularize.camel_case
relationship_as_viewed_by_target = (opts[:known_as] || self.name.snake_case).to_s
ViewCreator.has_n(self.name, relationship, target_class, relationship_as_viewed_by_target).add_to_design_doc
end

define_method(relationship) do
create_or_get_proxy(HasManyProxy, relationship, opts)
end

define_method("#{relationship}=") do |children|
create_or_get_proxy(HasManyProxy, relationship, opts).children = children
write_derived_props(relationship) if @set_derived_props
children
end
end

def self.has_many_rels
# Don't force clients to check its instantiated
@has_many_rels ||= []
end

def self.has_one(relationship)
@has_one_rels ||= []
@has_one_rels << relationship

if RelaxDB.create_views?
target_class = relationship.to_s.camel_case
relationship_as_viewed_by_target = self.name.snake_case
ViewCreator.has_n(self.name, relationship, target_class, relationship_as_viewed_by_target).add_to_design_doc
end

define_method(relationship) do
create_or_get_proxy(HasOneProxy, relationship).target
end

define_method("#{relationship}=") do |new_target|
create_or_get_proxy(HasOneProxy, relationship).target = new_target
write_derived_props(relationship) if @set_derived_props
new_target
end
end

def self.has_one_rels
@has_one_rels ||= []
end

def self.belongs_to(relationship, opts={})
belongs_to_rels[relationship] = opts

def self.references(relationship, opts={})
references_rels[relationship] = opts

define_method(relationship) do
create_or_get_proxy(BelongsToProxy, relationship).target
create_or_get_proxy(ReferencesProxy, relationship).target
end

define_method("#{relationship}=") do |new_target|
create_or_get_proxy(BelongsToProxy, relationship).target = new_target
write_derived_props(relationship) if @set_derived_props
create_or_get_proxy(ReferencesProxy, relationship).target = new_target
write_derived_props(relationship) # if @set_derived_props
end

# Allows all writers to be invoked from the hash passed to initialize
define_method("#{relationship}_id=") do |id|
instance_variable_set("@#{relationship}_id".to_sym, id)
write_derived_props(relationship) if @set_derived_props
@data["#{relationship}_id"] = id
write_derived_props(relationship) # if @set_derived_props
id
end

define_method("#{relationship}_id") do
instance_variable_get("@#{relationship}_id")
@data["#{relationship}_id"]
end

create_validator(relationship, opts[:validator]) if opts[:validator]
Expand All @@ -504,39 +417,16 @@ def self.belongs_to(relationship, opts={})
create_validation_msg(relationship, opts[:validation_msg]) if opts[:validation_msg]
end

class << self
alias_method :references, :belongs_to
end

self.belongs_to_rels = {}

# TODO : check for other refs and delete
def self.all_relationships
belongs_to_rels + has_one_rels + has_many_rels + references_many_rels
references_rels + has_one_rels + has_many_rels + references_many_rels
end

def self.all params = {}
AllDelegator.new self.name, params
end

# destroy! nullifies all relationships with peers and children before deleting
# itself in CouchDB
# The nullification and deletion are not performed in a transaction
#
# TODO: Current implemention may be inappropriate - causing CouchDB to try to JSON
# encode undefined. Ensure nil is serialized? See has_many_spec#should nullify its child relationships
def destroy!
self.class.references_many_rels.each do |rel|
send(rel).clear
end

self.class.has_many_rels.each do |rel|
send(rel).clear
end

self.class.has_one_rels.each do |rel|
send("#{rel}=".to_sym, nil)
end

# Implicitly prevent the object from being resaved by failing to update its revision
RelaxDB.db.delete("#{_id}?rev=#{_rev}")
self
Expand Down
101 changes: 0 additions & 101 deletions lib/relaxdb/has_many_proxy.rb

This file was deleted.

Loading

0 comments on commit 6a79a2c

Please sign in to comment.