Module: Datasource::Adapters::Sequel
- Defined in:
- lib/datasource/adapters/sequel.rb
Defined Under Namespace
Modules: DatasourceGenerator, Model, ScopeExtensions
Class Method Summary
collapse
Class Method Details
.association_reflection(klass, name) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/datasource/adapters/sequel.rb', line 79
def association_reflection(klass, name)
reflection = klass.association_reflections[name]
macro = case reflection[:type]
when :many_to_one then :belongs_to
when :one_to_many then :has_many
when :one_to_one then :has_one
else
fail Datasource::Error, "unimplemented association type #{reflection[:type]} - TODO"
end
{
klass: reflection[:cache][:class] || reflection[:class_name].constantize,
macro: macro,
foreign_key: reflection[:key].try!(:to_s)
}
end
|
.ensure_table_join!(ds, name, att) ⇒ Object
168
169
170
171
172
173
|
# File 'lib/datasource/adapters/sequel.rb', line 168
def ensure_table_join!(ds, name, att)
join_value = Hash(ds.scope.opts[:join]).find do |value|
(value.table_alias || value.table).to_s == att[:name]
end
fail Datasource::Error, "given scope does not join on #{name}, but it is required by #{att[:name]}" unless join_value
end
|
.get_assoc_eager_options(klass, name, assoc_select, append_select) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/datasource/adapters/sequel.rb', line 116
def get_assoc_eager_options(klass, name, assoc_select, append_select)
if reflection = Adapters::Sequel.association_reflection(klass, name)
self_append_select = []
Datasource::Base.reflection_select(reflection, append_select, self_append_select)
assoc_class = reflection[:klass]
datasource_class = assoc_class.default_datasource
{
name => ->(ds) {
ds.with_datasource(datasource_class)
.datasource_select(*(self_append_select + assoc_select))
}
}
else
{}
end
end
|
.get_rows(ds) ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/datasource/adapters/sequel.rb', line 147
def get_rows(ds)
eager = {}
append_select = []
ds.expose_associations.each_pair do |assoc_name, assoc_select|
eager.merge!(
get_assoc_eager_options(ds.class.orm_klass, assoc_name.to_sym, assoc_select, append_select))
end
scope = select_scope(ds)
if scope.respond_to?(:use_datasource)
scope = scope.clone.use_datasource(nil)
end
scope
.select_append(*get_sequel_select_values(append_select.map { |v| primary_scope_table(ds) + ".#{v}" }))
.eager(eager).all
end
|
.get_sequel_select_values(values = nil) ⇒ Object
135
136
137
|
# File 'lib/datasource/adapters/sequel.rb', line 135
def get_sequel_select_values(values = nil)
values.map { |str| ::Sequel.lit(str) }
end
|
.get_table_name(klass) ⇒ Object
96
97
98
|
# File 'lib/datasource/adapters/sequel.rb', line 96
def get_table_name(klass)
klass.table_name
end
|
.is_scope?(obj) ⇒ Boolean
100
101
102
|
# File 'lib/datasource/adapters/sequel.rb', line 100
def is_scope?(obj)
obj.kind_of?(::Sequel::Dataset)
end
|
.primary_scope_table(ds) ⇒ Object
164
165
166
|
# File 'lib/datasource/adapters/sequel.rb', line 164
def primary_scope_table(ds)
ds.scope.first_source_alias.to_s
end
|
.scope_loaded?(scope) ⇒ Boolean
112
113
114
|
# File 'lib/datasource/adapters/sequel.rb', line 112
def scope_loaded?(scope)
false
end
|
.scope_to_class(scope) ⇒ Object
104
105
106
107
108
109
110
|
# File 'lib/datasource/adapters/sequel.rb', line 104
def scope_to_class(scope)
if scope.row_proc && scope.row_proc.ancestors.include?(::Sequel::Model)
scope.row_proc
else
fail Datasource::Error, "unable to determine model for scope"
end
end
|
.select_scope(ds) ⇒ Object
143
144
145
|
# File 'lib/datasource/adapters/sequel.rb', line 143
def select_scope(ds)
ds.scope.select(*get_sequel_select_values(ds.get_select_values))
end
|
.to_query(ds) ⇒ Object
139
140
141
|
# File 'lib/datasource/adapters/sequel.rb', line 139
def to_query(ds)
ds.scope.sql
end
|