dock 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,73 @@
1
+ require 'couchrest'
2
+
3
+ module Dock
4
+ class Couchdb < Base
5
+ def create(attributes = {})
6
+
7
+ end
8
+ def find(id)
9
+
10
+ end
11
+ def find!(id)
12
+
13
+ end
14
+ def all(options = {})
15
+ conditions, order, limit, offset = extract_conditions!(options)
16
+ opts = { :conditions => conditions, :order => order_clause(order) }
17
+ opts = opts.merge({ :limit => limit }) unless limit.nil?
18
+ opts = opts.merge({ :offset => offset }) unless offset.nil?
19
+ model.all opts
20
+ end
21
+ def first(options = {})
22
+ conditions, order = extract_conditions!(options)
23
+ model.first :conditions => conditions, :order => order_clause(order)
24
+ end
25
+ def id()
26
+
27
+ end
28
+ def update(search_key, find_by, update_key, by_value)
29
+ #2 zoo.attributes = { :name => 'The Glue Factory', :inception => Time.now }
30
+ #3 zoo.save
31
+ end
32
+ def destroy(object)
33
+ object.destroy if valid_object?(object)
34
+ end
35
+ def associations()
36
+
37
+ end
38
+ def model_name()
39
+ model.class.name
40
+ end
41
+ def column_names()
42
+ model.properties.map(&:name)
43
+ end
44
+ # Checked
45
+ def count(options = {})
46
+ all(options).count
47
+ end
48
+ def encoding()
49
+
50
+ end
51
+ def belongs_to()
52
+
53
+ end
54
+ def has_many()
55
+
56
+ end
57
+ def scoped?()
58
+
59
+ end
60
+ def embedded?()
61
+
62
+ end
63
+ def cyclic?()
64
+
65
+ end
66
+ def supports_joins?()
67
+ false
68
+ end
69
+ def properties()
70
+
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,88 @@
1
+ require 'dm-core'
2
+
3
+ module Dock
4
+ class DataMapper < Base
5
+ # Checked
6
+ def create(attributes = {})
7
+ model.create(attributes)
8
+ end
9
+ # Checked
10
+ def find(id)
11
+ model.get(id)
12
+ end
13
+ # Checked
14
+ def find!(id)
15
+ model.get!(id)
16
+ end
17
+ # Checked
18
+ def all(options = {})
19
+ conditions, order, limit, offset = extract_conditions!(options)
20
+ opts = { :conditions => conditions, :order => order_clause(order) }
21
+ opts = opts.merge({ :limit => limit }) unless limit.nil?
22
+ opts = opts.merge({ :offset => offset }) unless offset.nil?
23
+ model.all opts
24
+ end
25
+ # Checked
26
+ def first(options = {})
27
+ conditions, order = extract_conditions!(options)
28
+ model.first :conditions => conditions, :order => order_clause(order)
29
+ end
30
+ # Checked
31
+ def id
32
+ # not supported?
33
+ end
34
+ def update(search_key, find_by, update_key, by_value)
35
+ entry = all(search_key find_by)
36
+ entry.attributes = {update_key => by_value}
37
+ entry.save
38
+ end
39
+ # Checked
40
+ def destroy(object)
41
+ object.destroy if valid_object?(object)
42
+ end
43
+ def associations()
44
+
45
+ end
46
+ def model_name
47
+ model.class.name
48
+ end
49
+ # Checked
50
+ def column_names
51
+ model.properties.map(&:name)
52
+ end
53
+ # Checked
54
+ def count(options = {})
55
+ all(options).count
56
+ end
57
+ def encoding
58
+ 'UTF-8'
59
+ end
60
+ def belongs_to()
61
+
62
+ end
63
+ def has_many()
64
+
65
+ end
66
+ def scoped?
67
+ true
68
+ end
69
+ def embedded?
70
+ true
71
+ end
72
+ def cyclic?
73
+ false
74
+ end
75
+ def supports_joins?
76
+ false
77
+ end
78
+
79
+ def properties()
80
+
81
+ end
82
+ protected
83
+
84
+ def order_clause(order)
85
+ order.map {|pair| pair.first.send(pair.last)}
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,98 @@
1
+ require 'mongoid'
2
+
3
+ module Dock
4
+ class Mongoid < Base
5
+ # Checked
6
+ def create(attributes = {})
7
+ model.create!(attributes)
8
+ end
9
+ # Checked
10
+ def find(id)
11
+ model.find(wrap_key(id))
12
+ end
13
+ # Checked
14
+ def find!(id)
15
+ model.where(:_id => wrap_key(id)).first
16
+ end
17
+ # Checked
18
+ def all(options = {})
19
+ conditions, order, limit, offset = extract_conditions!(options)
20
+ model.where(conditions_to_fields(conditions)).order_by(order).limit(limit).offset(offset)
21
+ end
22
+ # Checked
23
+ def first(options = {})
24
+ conditions, order = extract_conditions!(options)
25
+ model.limit(1).where(conditions_to_fields(conditions)).order_by(order).first
26
+ end
27
+ # Checked
28
+ def id
29
+ '_id'
30
+ end
31
+ # Checked
32
+ def update(search_key, find_by, update_key, by_value)
33
+ entry = model.where(search_key, find_by)
34
+ entry.update_attributes!(update_key, by_value)
35
+ end
36
+ # Checked
37
+ def destroy(object)
38
+ object.destroy if valid_object?(object)
39
+ end
40
+ def associations()
41
+
42
+ end
43
+ # Checked
44
+ def model_name
45
+ model.class.name
46
+ end
47
+ # Checked
48
+ def column_names
49
+ model.fields.keys
50
+ end
51
+ # Checked
52
+ def count(options = {})
53
+ all(options).count
54
+ end
55
+ # Checked
56
+ def encoding
57
+ 'UTF-8'
58
+ end
59
+ def belongs_to()
60
+
61
+ end
62
+ def has_many()
63
+
64
+ end
65
+ # Checked
66
+ def scoped?
67
+ model.scoped
68
+ end
69
+ # Checked
70
+ def embedded?
71
+ model.relations.values.detect { |a| a.macro.to_sym == :embedded_in }
72
+ end
73
+ # Checked
74
+ def cyclic?
75
+ model.cyclic?
76
+ end
77
+ # Checked
78
+ def supports_joins
79
+ false
80
+ end
81
+ def properties()
82
+
83
+ end
84
+ protected
85
+ # converts and documents to ids
86
+ def conditions_to_fields(conditions)
87
+ conditions.inject({}) do |fields, (key, value)|
88
+ if value.is_a?(Mongoid::Document) && model.fields.keys.include?("#{key}_id")
89
+ fields.merge("#{key}_id" => value.id)
90
+ elsif key.to_s == 'id'
91
+ fields.merge('_id' => value)
92
+ else
93
+ fields.merge(key => value)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,91 @@
1
+ require 'mongo_mapper'
2
+
3
+ module Dock
4
+ class MongoMapper < Base
5
+ def create(attributes = {})
6
+ model.create!(attributes)
7
+ end
8
+ def find(id)
9
+ model.find!(wrap_key(id))
10
+ end
11
+ def find!(id)
12
+ model.first({ :id => wrap_key(id) })
13
+ end
14
+ def all(conditions = {})
15
+ conditions, order, limit, offset = extract_conditions!(conditions)
16
+ conditions = conditions.merge(:sort => order) unless order.nil?
17
+ conditions = conditions.merge(:limit => limit) unless limit.nil?
18
+ conditions = conditions.merge(:offset => offset) unless limit.nil? || offset.nil?
19
+ model.all(conditions_to_fields(conditions))
20
+ end
21
+ def first(conditions = {})
22
+ conditions, order = extract_conditions!(conditions)
23
+ conditions = conditions.merge(:sort => order) unless order.nil?
24
+ model.first(conditions_to_fields(conditions))
25
+ end
26
+ def id
27
+ '_id'
28
+ end
29
+ def update(search_key, find_by, update_key, by_value)
30
+ entry = model.where(search_key find_by)
31
+ entry.set(update_key => by_value)
32
+ entry.reload
33
+ end
34
+ def destroy(object)
35
+ object.destroy if valid_object?(object)
36
+ end
37
+ def associations()
38
+
39
+ end
40
+ def model_name
41
+ model.class.name
42
+ end
43
+ def column_names
44
+ model.fields.keys
45
+ end
46
+ # Checked
47
+ def count(conditions = {})
48
+ all(options).count
49
+ end
50
+ def encoding
51
+ 'UTF-8'
52
+ end
53
+ def belongs_to()
54
+
55
+ end
56
+ def has_many()
57
+
58
+ end
59
+ # Checked
60
+ def scoped?
61
+ model.scoped
62
+ end
63
+ # Checked
64
+ def embedded?
65
+ model.relations.values.detect { |a| a.macro.to_sym == :embedded_in }
66
+ end
67
+ # Checked
68
+ def cyclic?
69
+ model.cyclic?
70
+ end
71
+ # Checked
72
+ def supports_joins
73
+ false
74
+ end
75
+ def properties()
76
+
77
+ end
78
+ protected
79
+
80
+ # converts and documents to ids
81
+ def conditions_to_fields(conditions)
82
+ conditions.inject({}) do |fields, (key, value)|
83
+ if value.is_a?(MongoMapper::Document) && model.key?("#{key}_id")
84
+ fields.merge("#{key}_id" => value.id)
85
+ else
86
+ fields.merge(key => value)
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,81 @@
1
+ require 'nobrainer'
2
+
3
+ module Dock
4
+ class NoBrainer < Base
5
+ def create(attributes = {})
6
+ model.create!(attributes)
7
+ end
8
+ def find(id)
9
+ model.find(wrap_key(id))
10
+ end
11
+ def find!(id)
12
+ model.find!(wrap_key(id))
13
+ end
14
+ def all(options = {})
15
+ conditions, order, limit, offset = extract_conditions!(options)
16
+ model.where(conditions).order_by(order_to_nql(order)).limit(limit).skip(offset)
17
+ end
18
+ def first(options = {})
19
+ conditions, order = extract_conditions!(options)
20
+ model.where(conditions).order_by(order_to_nql(order)).first
21
+ end
22
+ def id
23
+ 'id'
24
+ end
25
+ def update(search_key, find_by, update_key, by_value)
26
+ entry = model.where(search_key => find_by)
27
+ entry.update_attributes!(update_key => by_value)
28
+ end
29
+ def destroy(object)
30
+ object.destroy if valid_object?(object)
31
+ end
32
+ def associations()
33
+
34
+ end
35
+ def model_name
36
+ model.class.name
37
+ end
38
+ def column_names
39
+ model.fields.keys
40
+ end
41
+ # Checked
42
+ def count(options = {})
43
+ all(options).count
44
+ end
45
+ def encoding
46
+ 'UTF-8'
47
+ end
48
+ def belongs_to()
49
+
50
+ end
51
+ def has_many()
52
+
53
+ end
54
+ def scoped
55
+ true
56
+ end
57
+ def embedded?
58
+ false
59
+ end
60
+ def cyclic?
61
+ false
62
+ end
63
+ def supports_joins?
64
+ false
65
+ end
66
+ def properties()
67
+
68
+ end
69
+
70
+ protected
71
+ # NoBrainer does not accept arrays as an `order_by` argument.
72
+ def order_to_nql(order)
73
+ if order.empty?
74
+ nil
75
+ else
76
+ order.to_h
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,127 @@
1
+ require 'sequel'
2
+
3
+ module Dock
4
+ class Sequel < Base
5
+ def create()
6
+ associated_objects = []
7
+ attrs = {}
8
+
9
+ model.db.transaction do
10
+ attributes.each do |col, value|
11
+ if model.associations.include?(col)
12
+ Array(value).each{|v| associated_objects << [association_method(col), v]}
13
+ else
14
+ attrs.merge!(col => value) # pass it on
15
+ end
16
+ end
17
+
18
+ obj = model.create(attrs) # create main obj
19
+ associated_objects.each do |m,o|
20
+ obj.send(m, o)
21
+ end
22
+ obj.save
23
+ end # transaction
24
+ end
25
+ # Checked
26
+ def find(id)
27
+ model.find(wrap_key(model.primary_key => id))
28
+ end
29
+ # Checked
30
+ def find!(id)
31
+ model[wrap_key(id)] || raise(Error, "#{model.name} not found with #{model.primary_key} of #{wrap_key(id)}")
32
+ end
33
+ # Checked
34
+ def all(options = {})
35
+ conditions, order = extract_conditions!(options)
36
+ model.filter(conditions_to_hash(conditions)).order(*order_clause(order)).all
37
+ end
38
+ # Checked
39
+ def first(options = {})
40
+ conditions, order = extract_conditions!(options)
41
+ model.filter(conditions_to_hash(conditions)).order(*order_clause(order)).first
42
+ end
43
+ # Checked
44
+ def id
45
+ 'primary_key'
46
+ end
47
+ def update(search_key, find_by, update_key, by_value)
48
+ entry = model.where(search_key, find_by)
49
+ entry.update(update_key by_value)
50
+ end
51
+ # Checked
52
+ def destroy(object)
53
+ object.destroy && true if valid_object?(object)
54
+ end
55
+ def associations()
56
+
57
+ end
58
+ # Checked
59
+ def model_name
60
+ model.class.name
61
+ end
62
+ # Checked
63
+ def column_names
64
+ model.columns
65
+ end
66
+ # Checked
67
+ def count(options = {})
68
+ all(options).count
69
+ end
70
+ def encoding
71
+ 'UTF-8'
72
+ end
73
+ def belongs_to()
74
+
75
+ end
76
+ def has_many()
77
+
78
+ end
79
+ def scoped?
80
+ false
81
+ end
82
+ def embedded?
83
+ false
84
+ end
85
+ def cyclic?
86
+ true
87
+ end
88
+ # Checked
89
+ def supports_joins?
90
+ true
91
+ end
92
+ def properties()
93
+
94
+ end
95
+ protected
96
+
97
+ def conditions_to_hash(conditions)
98
+ conditions.inject({}) do |cond_hash, (col, value)|
99
+ # make sure the key is a symbol
100
+ col = col.to_sym
101
+
102
+ if value.is_a?(Sequel::Model)
103
+ # look up the column name for the assoc
104
+ key = model.association_reflection(col)[:key]
105
+ cond_hash.merge(key => value.id)
106
+ else
107
+ cond_hash.merge(col => value)
108
+ end
109
+ end
110
+ end
111
+
112
+ def order_clause(order)
113
+ m = order.map {|pair| pair.first.send(pair.last)}
114
+ m.empty? ? nil : m
115
+ end
116
+
117
+ def association_method(col)
118
+ assoc = model.association_reflection(col)
119
+ case assoc[:type]
120
+ when :one_to_many, :many_to_many
121
+ assoc.add_method
122
+ else # when :many_to_one, :one_to_one
123
+ assoc.setter_method
124
+ end
125
+ end
126
+ end
127
+ end