langalex-couch_potato 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. data/CREDITS +3 -0
  2. data/MIT-LICENSE.txt +19 -0
  3. data/init.rb +5 -0
  4. data/lib/core_ext/object.rb +5 -0
  5. data/lib/core_ext/time.rb +14 -0
  6. data/lib/couch_potato/active_record/compatibility.rb +9 -0
  7. data/lib/couch_potato/ordering.rb +88 -0
  8. data/lib/couch_potato/persistence/belongs_to_property.rb +44 -0
  9. data/lib/couch_potato/persistence/bulk_save_queue.rb +47 -0
  10. data/lib/couch_potato/persistence/callbacks.rb +96 -0
  11. data/lib/couch_potato/persistence/collection.rb +51 -0
  12. data/lib/couch_potato/persistence/external_collection.rb +54 -0
  13. data/lib/couch_potato/persistence/external_has_many_property.rb +68 -0
  14. data/lib/couch_potato/persistence/find.rb +21 -0
  15. data/lib/couch_potato/persistence/finder.rb +109 -0
  16. data/lib/couch_potato/persistence/inline_collection.rb +14 -0
  17. data/lib/couch_potato/persistence/inline_has_many_property.rb +39 -0
  18. data/lib/couch_potato/persistence/json.rb +46 -0
  19. data/lib/couch_potato/persistence/properties.rb +40 -0
  20. data/lib/couch_potato/persistence/simple_property.rb +33 -0
  21. data/lib/couch_potato/persistence.rb +186 -0
  22. data/lib/couch_potato/versioning.rb +46 -0
  23. data/lib/couch_potato.rb +20 -0
  24. data/spec/attributes_spec.rb +22 -0
  25. data/spec/belongs_to_spec.rb +37 -0
  26. data/spec/callbacks_spec.rb +229 -0
  27. data/spec/create_spec.rb +68 -0
  28. data/spec/destroy_spec.rb +24 -0
  29. data/spec/find_spec.rb +88 -0
  30. data/spec/finder_spec.rb +115 -0
  31. data/spec/has_many_spec.rb +178 -0
  32. data/spec/inline_collection_spec.rb +15 -0
  33. data/spec/ordering_spec.rb +94 -0
  34. data/spec/property_spec.rb +46 -0
  35. data/spec/reload_spec.rb +46 -0
  36. data/spec/spec.opts +4 -0
  37. data/spec/spec_helper.rb +31 -0
  38. data/spec/update_spec.rb +33 -0
  39. data/spec/versioning_spec.rb +149 -0
  40. metadata +132 -0
data/CREDITS ADDED
@@ -0,0 +1,3 @@
1
+ Parts of this are taken or inspired by Geoffrey Grosenbach's travel_app which is part of his CouchDB screencast.
2
+
3
+ Jan Lehnardt got me started on doing this.
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2007 Bryan Helmkamp, Seth Fitzsimmons
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ # this is for rails only
2
+
3
+ require File.dirname(__FILE__) + '/lib/couch_potato'
4
+
5
+ CouchPotato::Config.database_name = YAML::load(File.read(RAILS_ROOT + '/config/couchdb.yml'))[RAILS_ENV]
@@ -0,0 +1,5 @@
1
+ Object.class_eval do
2
+ def try(method, *args)
3
+ self.send method, *args if self.respond_to?(method)
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ class Time
2
+ def to_json(*a)
3
+ {
4
+ 'json_class' => self.class.name,
5
+ 'data' => self.strftime("%Y/%m/%d %H:%M:%S %z")
6
+ }.to_json(*a)
7
+ end
8
+ end
9
+
10
+ class Time
11
+ def self.json_create(o)
12
+ parse(*o['data'])
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ module CouchPotato
2
+ module Persistence
3
+ alias_method :new_record?, :new_document?
4
+
5
+ module ClassMethods
6
+ alias_method :find, :get
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,88 @@
1
+ module CouchPotato
2
+ module Ordering
3
+ def self.included(base)
4
+ base.class_eval do
5
+ property :position
6
+ cattr_accessor :ordering_scope
7
+
8
+ before_create :set_position
9
+ before_create :update_positions
10
+ before_destroy :update_lower_positions_after_destroy
11
+ before_update :update_positions
12
+
13
+ def self.set_ordering_scope(scope)
14
+ self.ordering_scope = scope
15
+ end
16
+
17
+ def position=(new_position)
18
+ @old_position = position
19
+ @position = new_position
20
+ end
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ MAX = 9999999
27
+
28
+ def set_position
29
+ self.position ||= self.class.count(scope_conditions) + 1
30
+ end
31
+
32
+ def update_positions
33
+ @old_position = MAX if new_document?
34
+ return unless @old_position
35
+ if position < @old_position
36
+ new_lower_items = find_in_positions self.position, @old_position - 1
37
+ move new_lower_items, :down
38
+ elsif position > @old_position
39
+ new_higher_items = find_in_positions @old_position + 1, position
40
+ move new_higher_items, :up
41
+ end
42
+ end
43
+
44
+ def update_lower_positions_after_destroy
45
+ lower_items = find_in_positions self.position + 1, MAX
46
+ move lower_items, :up
47
+ end
48
+
49
+ def find_in_positions(from, to)
50
+ self.class.all scope_conditions.merge(:position => from..to)
51
+ end
52
+
53
+ def scope_conditions
54
+ if ordering_scope
55
+ {ordering_scope => self.send(ordering_scope)}
56
+ else
57
+ {}
58
+ end
59
+ end
60
+
61
+ def move(items, direction)
62
+ items.each do |item|
63
+ if direction == :up
64
+ item.position -= 1
65
+ else
66
+ item.position += 1
67
+ end
68
+ self.bulk_save_queue << item
69
+ end
70
+ end
71
+ end
72
+
73
+ module ExternalCollectionOrderedFindExtension
74
+ def self.included(base)
75
+ base.class_eval do
76
+ def items
77
+ if @item_class.property_names.include?(:position)
78
+ @items ||= CouchPotato::Persistence::Finder.new.find @item_class, @owner_id_attribute_name => owner_id, :position => 1..CouchPotato::Ordering::MAX
79
+ else
80
+ @items ||= CouchPotato::Persistence::Finder.new.find @item_class, @owner_id_attribute_name => owner_id
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ CouchPotato::Persistence::ExternalCollection.send :include, ExternalCollectionOrderedFindExtension
87
+ end
88
+
@@ -0,0 +1,44 @@
1
+ module CouchPotato
2
+ module Persistence
3
+ class BelongsToProperty
4
+ attr_accessor :name
5
+
6
+ def initialize(owner_clazz, name, options = {})
7
+ self.name = name
8
+ accessors = <<-ACCESSORS
9
+ def #{name}
10
+ @#{name} || @#{name}_id ? #{item_class_name}.find(@#{name}_id) : nil
11
+ end
12
+
13
+ def #{name}=(value)
14
+ @#{name} = value
15
+ @#{name}_id = value.id
16
+ end
17
+ ACCESSORS
18
+ owner_clazz.class_eval accessors
19
+ owner_clazz.send :attr_accessor, "#{name}_id"
20
+ end
21
+
22
+ def save(object)
23
+
24
+ end
25
+
26
+ def destroy(object)
27
+
28
+ end
29
+
30
+ def build(object, json)
31
+ object.send "#{name}_id=", json["#{name}_id"]
32
+ end
33
+
34
+ def serialize(json, object)
35
+ json["#{name}_id"] = object.send("#{name}_id") if object.send("#{name}_id")
36
+ end
37
+
38
+ def item_class_name
39
+ @name.to_s.singularize.camelcase
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,47 @@
1
+ module CouchPotato
2
+ module Persistence
3
+ class BulkSaveQueue
4
+ attr_reader :callbacks
5
+
6
+ def initialize
7
+ @other_queues = []
8
+ @callbacks = []
9
+ @instances = []
10
+ end
11
+
12
+ def <<(instance)
13
+ if own?
14
+ @instances << instance
15
+ else
16
+ @other_queues.last << instance
17
+ end
18
+ end
19
+
20
+ def push_queue(queue)
21
+ @other_queues.push queue
22
+ end
23
+
24
+ def pop_queue
25
+ @other_queues.pop
26
+ end
27
+
28
+ def own?
29
+ @other_queues.empty?
30
+ end
31
+
32
+ def save(&callback)
33
+ if own?
34
+ @callbacks << callback if callback
35
+ res = CouchPotato::Persistence.Db.bulk_save @instances
36
+ @instances.clear
37
+ @callbacks.each do |_callback|
38
+ _callback.call res
39
+ end
40
+ @callbacks.clear
41
+ else
42
+ @other_queues.last.callbacks << callback if callback
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,96 @@
1
+ module CouchPotato
2
+ module Persistence
3
+ module Callbacks
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+
7
+ base.class_eval do
8
+ def self.callbacks
9
+ @@callbacks ||= {}
10
+ @@callbacks[self.name] ||= {:before_validation_on_create => [],
11
+ :before_validation_on_update => [], :before_validation_on_save => [], :before_create => [],
12
+ :after_create => [], :before_update => [], :after_update => [],
13
+ :before_save => [], :after_save => [],
14
+ :before_destroy => [], :after_destroy => []}
15
+ end
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def run_callbacks(name)
22
+ self.class.callbacks[name].each do |callback|
23
+ self.send callback
24
+ end
25
+ end
26
+
27
+ module ClassMethods
28
+ def before_validation_on_create(*names)
29
+ names.each do |name|
30
+ callbacks[:before_validation_on_create] << name
31
+ end
32
+ end
33
+
34
+ def before_validation_on_update(*names)
35
+ names.each do |name|
36
+ callbacks[:before_validation_on_update] << name
37
+ end
38
+ end
39
+
40
+ def before_validation_on_save(*names)
41
+ names.each do |name|
42
+ callbacks[:before_validation_on_save] << name
43
+ end
44
+ end
45
+
46
+ def before_create(*names)
47
+ names.each do |name|
48
+ callbacks[:before_create] << name
49
+ end
50
+ end
51
+
52
+ def before_save(*names)
53
+ names.each do |name|
54
+ callbacks[:before_save] << name
55
+ end
56
+ end
57
+
58
+ def before_update(*names)
59
+ names.each do |name|
60
+ callbacks[:before_update] << name
61
+ end
62
+ end
63
+
64
+ def before_destroy(*names)
65
+ names.each do |name|
66
+ callbacks[:before_destroy] << name
67
+ end
68
+ end
69
+
70
+ def after_update(*names)
71
+ names.each do |name|
72
+ callbacks[:after_update] << name
73
+ end
74
+ end
75
+
76
+ def after_save(*names)
77
+ names.each do |name|
78
+ callbacks[:after_save] << name
79
+ end
80
+ end
81
+
82
+ def after_create(*names)
83
+ names.each do |name|
84
+ callbacks[:after_create] << name
85
+ end
86
+ end
87
+
88
+ def after_destroy(*names)
89
+ names.each do |name|
90
+ callbacks[:after_destroy] << name
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,51 @@
1
+ module CouchPotato
2
+ module Persistence
3
+ class Collection
4
+ attr_accessor :items, :item_class
5
+
6
+ def initialize(item_class)
7
+ @item_class = item_class
8
+ @items = []
9
+ end
10
+
11
+ def build(attributes)
12
+ item = @item_class.new(attributes)
13
+ self.<< item
14
+ item
15
+ end
16
+
17
+ def ==(other)
18
+ other.class == self.class && other.items == items && other.item_class == item_class
19
+ end
20
+
21
+ def to_json(*args)
22
+ raise 'implement me in a subclass'
23
+ end
24
+
25
+ def self.json_create(json)
26
+ raise 'implement me in a subclass'
27
+ end
28
+
29
+ delegate :[], :<<, :empty?, :any?, :each, :+, :size, :first, :last, :map, :inject, :join, :clear, :select, :reject, :to => :items
30
+
31
+ end
32
+ end
33
+ end
34
+
35
+ if Object.const_defined? 'WillPaginate'
36
+ module CouchPotato
37
+ module Persistence
38
+ class Collection
39
+ def paginate(options = {})
40
+ page = (options[:page] || 1).to_i
41
+ per_page = options[:per_page] || 20
42
+ collection = WillPaginate::Collection.new page, per_page, self.size
43
+ items[((page - 1) * per_page)..(page * (per_page - 1))].each do |item|
44
+ collection << item
45
+ end
46
+ collection
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,54 @@
1
+ require File.dirname(__FILE__) + '/collection'
2
+ require File.dirname(__FILE__) + '/finder'
3
+
4
+ module CouchPotato
5
+ module Persistence
6
+ class ExternalCollection < Collection
7
+
8
+ attr_accessor :item_ids, :owner_id
9
+
10
+ def initialize(item_class, owner_id_attribute_name)
11
+ super item_class
12
+ @items = nil
13
+ @owner_id_attribute_name = owner_id_attribute_name
14
+ end
15
+
16
+ def build(attributes = {})
17
+ item = @item_class.new(attributes)
18
+ self.<< item
19
+ item.send "#{@owner_id_attribute_name}=", owner_id
20
+ item
21
+ end
22
+
23
+ def create(attributes = {})
24
+ item = build(attributes)
25
+ item.save
26
+ item
27
+ end
28
+
29
+ def create!(attributes = {})
30
+ item = build(attributes)
31
+ item.save!
32
+ item
33
+ end
34
+
35
+ def items
36
+ @items ||= Finder.new.find @item_class, @owner_id_attribute_name => owner_id
37
+ end
38
+
39
+ def save
40
+ items.each do |item|
41
+ item.send "#{@owner_id_attribute_name}=", owner_id
42
+ item.save
43
+ end
44
+ end
45
+
46
+ def destroy
47
+ @items.each do |item|
48
+ item.destroy
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+
@@ -0,0 +1,68 @@
1
+ module CouchPotato
2
+ module Persistence
3
+ class ExternalHasManyProperty
4
+ attr_accessor :name, :dependent
5
+ def initialize(owner_clazz, name, options = {})
6
+ @name, @owner_clazz = name, owner_clazz
7
+ @dependent = options[:dependent] || :nullify
8
+ getter = <<-ACCESORS
9
+ def #{name}
10
+ @#{name} ||= CouchPotato::Persistence::ExternalCollection.new(#{item_class_name}, :#{owner_clazz.name.underscore}_id)
11
+ end
12
+
13
+ def #{name}=(items)
14
+ items.each do |item|
15
+ #{name} << item
16
+ end
17
+ end
18
+ ACCESORS
19
+ owner_clazz.class_eval getter
20
+ end
21
+
22
+ def save(object)
23
+ object.send(name).owner_id = object._id
24
+ object.send(name).each do |item|
25
+ item.send("#{@owner_clazz.name.underscore}_id=", object.id)
26
+ begin
27
+ item.bulk_save_queue.push_queue object.bulk_save_queue
28
+ item.save
29
+ ensure
30
+ item.bulk_save_queue.pop_queue
31
+ end
32
+ end
33
+ end
34
+
35
+ def destroy(object)
36
+ object.send(name).each do |item|
37
+ if dependent == :destroy
38
+ begin
39
+ item.bulk_save_queue.push_queue object.bulk_save_queue
40
+ item.destroy
41
+ ensure
42
+ item.bulk_save_queue.pop_queue
43
+ end
44
+ else
45
+ item.send("#{@owner_clazz.name.underscore}_id=", nil)
46
+ end
47
+ end
48
+ end
49
+
50
+ def build(object, json)
51
+ collection = ExternalCollection.new(item_class_name.constantize, "#{@owner_clazz.name.underscore}_id")
52
+ collection.owner_id = object.id
53
+ object.send("#{name}").clear
54
+ object.send "#{name}=", collection
55
+ end
56
+
57
+ def serialize(json, object)
58
+ nil
59
+ end
60
+
61
+ private
62
+
63
+ def item_class_name
64
+ @name.to_s.singularize.camelcase
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,21 @@
1
+ module CouchPotato
2
+ module Persistence
3
+ module Find
4
+ def first(options = {})
5
+ Finder.new.find(self, options).first
6
+ end
7
+
8
+ def last(options = {})
9
+ Finder.new.find(self, options, :descending => true).first
10
+ end
11
+
12
+ def all(options = {})
13
+ Finder.new.find(self, options)
14
+ end
15
+
16
+ def count(options = {})
17
+ Finder.new.count(self, options)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,109 @@
1
+ require 'uri'
2
+
3
+ module CouchPotato
4
+ module Persistence
5
+ class Finder
6
+ # finds all objects of a given type by the given attribute/value pairs
7
+ # options: attribute_name => value pairs to search for
8
+ # value can also be a range which will do a range search with startkey/endkey
9
+ # WARNING: calling this methods creates a new view in couchdb if it's not present already so don't overuse this
10
+ def find(clazz, conditions = {}, view_options = {})
11
+ params = view_parameters(clazz, conditions, view_options)
12
+ to_instances clazz, query_view!(params)
13
+ end
14
+
15
+ def count(clazz, conditions = {}, view_options = {})
16
+ params = view_parameters(clazz, conditions, view_options)
17
+ query_view!(params, '_count')['rows'].first.try(:[], 'value') || 0
18
+ end
19
+
20
+ private
21
+
22
+ def query_view!(params, view_postfix = nil)
23
+ begin
24
+ query_view params, view_postfix
25
+ rescue RestClient::ResourceNotFound => e
26
+ create_view params
27
+ query_view params, view_postfix
28
+ end
29
+ end
30
+
31
+ def db(name = nil)
32
+ ::CouchPotato::Persistence.Db(name)
33
+ end
34
+
35
+ def create_view(params)
36
+ # in couchdb 0.9 we could use only 1 view and pass reduce=false for find and count with reduce
37
+ design_doc = db.get "_design/#{params[:design_document]}" rescue nil
38
+ db.save({
39
+ "_id" => "_design/#{params[:design_document]}",
40
+ :views => {
41
+ params[:view] => {
42
+ :map => map_function(params)
43
+ },
44
+ params[:view] + '_count' => {
45
+ :map => map_function(params),
46
+ :reduce => "function(keys, values) {
47
+ return values.length;
48
+ }"
49
+ }
50
+ }
51
+ }.merge(design_doc ? {'_rev' => design_doc['_rev']} : {}))
52
+ end
53
+
54
+ def map_function(params)
55
+ "function(doc) {
56
+ if(doc.ruby_class == '#{params[:class]}') {
57
+ emit(
58
+ [#{params[:search_fields].map{|attr| "doc[\"#{attr}\"]"}.join(', ')}], doc
59
+ );
60
+ }
61
+ }"
62
+ end
63
+
64
+ def to_instances(clazz, query_result)
65
+ query_result['rows'].map{|doc| doc['value']}.map{|json| clazz.json_create json}
66
+ end
67
+
68
+ def query_view(params, view_postfix)
69
+ db.view params[:view_url] + view_postfix.to_s, search_keys(params)
70
+ end
71
+
72
+ def search_keys(params)
73
+ if params[:search_values].select{|v| v.is_a?(Range)}.any?
74
+ {:startkey => params[:search_values].map{|v| v.is_a?(Range) ? v.first : v}, :endkey => params[:search_values].map{|v| v.is_a?(Range) ? v.last : v}}.merge(params[:view_options])
75
+ elsif params[:search_values].select{|v| v.is_a?(Array)}.any?
76
+ {:keys => prepare_multi_key_search(params[:search_values])}.merge(params[:view_options])
77
+ else
78
+ {:key => params[:search_values]}.merge(params[:view_options])
79
+ end
80
+ end
81
+
82
+ def prepare_multi_key_search(values)
83
+ array = values.select{|v| v.is_a?(Array)}.first
84
+ index = values.index array
85
+ array.map do |item|
86
+ copy = values.dup
87
+ copy[index] = item
88
+ copy
89
+ end
90
+ end
91
+
92
+ def view_parameters(clazz, conditions, view_options)
93
+ {
94
+ :class => clazz,
95
+ :design_document => clazz.name.underscore,
96
+ :search_fields => conditions.to_a.sort_by{|f| f.first.to_s}.map(&:first),
97
+ :search_values => conditions.to_a.sort_by{|f| f.first.to_s}.map(&:last),
98
+ :view_options => view_options,
99
+ :view => "by_#{view_name(conditions)}",
100
+ :view_url => "#{clazz.name.underscore}/by_#{view_name(conditions)}"
101
+ }
102
+ end
103
+
104
+ def view_name(options)
105
+ options.to_a.sort_by{|f| f.first.to_s}.map(&:first).join('_and_')
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/collection'
2
+
3
+ module CouchPotato
4
+ module Persistence
5
+ class InlineCollection < Collection
6
+
7
+ def to_json(*args)
8
+ @items.to_json(*args)
9
+ end
10
+
11
+ end
12
+ end
13
+ end
14
+