-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored properties into their own classes, belongs_to property wor…
…king, changed generated json to look much nicer
- Loading branch information
Alexander Lang
committed
Oct 5, 2008
1 parent
d5d59e1
commit 45cd49a
Showing
19 changed files
with
291 additions
and
82 deletions.
There are no files selected for viewing
Empty file.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module CouchPotatoe | ||
module Persistence | ||
class BelongsToProperty | ||
attr_accessor :name | ||
|
||
def initialize(owner_clazz, name) | ||
self.name = name | ||
accessors = <<-ACCESSORS | ||
def #{name} | ||
@#{name} || @#{name}_id ? #{item_class_name}.find(@#{name}_id) : nil | ||
end | ||
def #{name}=(value) | ||
@#{name} = value | ||
@#{name}_id = value.id | ||
end | ||
ACCESSORS | ||
owner_clazz.class_eval accessors | ||
owner_clazz.send :attr_accessor, "#{name}_id" | ||
end | ||
|
||
def save(object) | ||
|
||
end | ||
|
||
def build(object, json) | ||
object.send "#{name}_id=", json["#{name}_id"] | ||
end | ||
|
||
def serialize(json, object) | ||
json["#{name}_id"] = object.send("#{name}_id") if object.send("#{name}_id") | ||
end | ||
|
||
def item_class_name | ||
@name.to_s.singularize.capitalize | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require File.dirname(__FILE__) + '/collection' | ||
require File.dirname(__FILE__) + '/finder' | ||
|
||
module CouchPotatoe | ||
module Persistence | ||
class ExternalCollection < Collection | ||
|
||
attr_accessor :item_ids, :owner_id | ||
|
||
def initialize(item_class, owner_id_attribute_name) | ||
super item_class | ||
@items = nil | ||
@owner_id_attribute_name = owner_id_attribute_name | ||
end | ||
|
||
def build(attributes) | ||
item = @item_class.new(attributes) | ||
self.<< item | ||
item.position = self.size if item.respond_to?(:position=) | ||
item.send "#{@owner_id_attribute_name}=", owner_id | ||
item | ||
end | ||
|
||
def items | ||
if @items.nil? | ||
@items = Finder.new.find @item_class, @owner_id_attribute_name => owner_id | ||
end | ||
@items | ||
end | ||
|
||
def save | ||
items.each do |item| | ||
item.send "#{@owner_id_attribute_name}=", owner_id | ||
item.save | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
|
45 changes: 45 additions & 0 deletions
45
lib/couch_potatoe/persistence/external_has_many_property.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module CouchPotatoe | ||
module Persistence | ||
class ExternalHasManyProperty | ||
attr_accessor :name | ||
def initialize(owner_clazz, name) | ||
@name, @owner_clazz = name, owner_clazz | ||
getter = <<-ACCESORS | ||
def #{name} | ||
@#{name} ||= CouchPotatoe::Persistence::ExternalCollection.new(#{item_class_name}, :#{owner_clazz.name.underscore}_id) | ||
end | ||
def #{name}=(items) | ||
items.each do |item| | ||
#{name} << item | ||
end | ||
end | ||
ACCESORS | ||
owner_clazz.class_eval getter | ||
end | ||
|
||
def save(object) | ||
object.send(name).each do |item| | ||
item.send("#{@owner_clazz.name.underscore}_id=", object.id) | ||
item.save | ||
end | ||
end | ||
|
||
def build(object, json) | ||
collection = ExternalCollection.new(item_class_name.constantize, "#{@owner_clazz.name.underscore}_id") | ||
collection.owner_id = object.id | ||
object.send "#{name}=", collection | ||
end | ||
|
||
def serialize(json, object) | ||
nil | ||
end | ||
|
||
private | ||
|
||
def item_class_name | ||
@name.to_s.singularize.capitalize | ||
end | ||
end | ||
end | ||
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class InlineHasManyProperty | ||
attr_accessor :name | ||
|
||
def initialize(owner_clazz, name) | ||
@name = name | ||
getter = <<-GETTER | ||
def #{name} | ||
@#{name} ||= CouchPotatoe::Persistence::InlineCollection.new(#{item_class_name}) | ||
end | ||
GETTER | ||
owner_clazz.class_eval getter | ||
end | ||
|
||
def build(object, json) | ||
json[name.to_s].each do |item| | ||
item.delete 'ruby_class' | ||
object.send("#{name}").build item | ||
end | ||
end | ||
|
||
def save(object) | ||
|
||
end | ||
|
||
def serialize(json, object) | ||
json[name.to_s] = object.send(name) | ||
end | ||
|
||
private | ||
|
||
def item_class_name | ||
@name.to_s.singularize.capitalize | ||
end | ||
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
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
Oops, something went wrong.