Class: Datasource::Base
- Inherits:
-
Object
- Object
- Datasource::Base
- Defined in:
- lib/datasource/base.rb,
lib/datasource/attributes/loaded.rb,
lib/datasource/attributes/query_attribute.rb,
lib/datasource/attributes/computed_attribute.rb
Class Attribute Summary collapse
-
._associations ⇒ Object
Returns the value of attribute _associations.
-
._attributes ⇒ Object
Returns the value of attribute _attributes.
-
._collection_context ⇒ Object
Returns the value of attribute _collection_context.
-
._loader_order ⇒ Object
Returns the value of attribute _loader_order.
-
._loaders ⇒ Object
Returns the value of attribute _loaders.
-
._update_scope ⇒ Object
Returns the value of attribute _update_scope.
-
.default_consumer_adapter ⇒ Object
Should be set by consumer adapter library (e.g. for ActiveModelSerializers).
- .orm_klass ⇒ Object
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#expose_associations ⇒ Object
readonly
Returns the value of attribute expose_associations.
-
#expose_attributes ⇒ Object
readonly
Returns the value of attribute expose_attributes.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Class Method Summary collapse
- ._column_attribute_names ⇒ Object
- .collection(&block) ⇒ Object
- .default_adapter ⇒ Object
- .inherited(base) ⇒ Object
- .primary_key ⇒ Object
- .reflection_select(reflection, parent_select, assoc_select) ⇒ Object
Instance Method Summary collapse
- #attribute_exposed?(name) ⇒ Boolean
-
#can_upgrade?(records) ⇒ Boolean
assume records have all attributes selected (default ORM record).
- #fail_missing_attributes(names) ⇒ Object
- #get_collection_context(rows) ⇒ Object
- #get_exposed_loaders ⇒ Object
- #get_select_values ⇒ Object
-
#initialize(scope, adapter = nil) ⇒ Base
constructor
A new instance of Base.
- #params(*args) ⇒ Object
- #results(rows = nil) ⇒ Object
- #run_loaders(rows) ⇒ Object
- #select(*names) ⇒ Object
- #select_all ⇒ Object
- #select_all_columns ⇒ Object
- #set_row_loaded_values(collection_context, row) ⇒ Object
- #update_dependencies(names) ⇒ Object
- #upgrade_records(records) ⇒ Object
Constructor Details
#initialize(scope, adapter = nil) ⇒ Base
Returns a new instance of Base.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/datasource/base.rb', line 86 def initialize(scope, adapter = nil) @adapter = adapter || self.class.default_adapter @scope = if self.class._update_scope self.class._update_scope.call(scope) else scope end @expose_attributes = [] @expose_associations = {} @select_all_columns = false @params = {} end |
Class Attribute Details
._associations ⇒ Object
Returns the value of attribute _associations.
4 5 6 |
# File 'lib/datasource/base.rb', line 4 def _associations @_associations end |
._attributes ⇒ Object
Returns the value of attribute _attributes.
4 5 6 |
# File 'lib/datasource/base.rb', line 4 def _attributes @_attributes end |
._collection_context ⇒ Object
Returns the value of attribute _collection_context.
4 5 6 |
# File 'lib/datasource/base.rb', line 4 def _collection_context @_collection_context end |
._loader_order ⇒ Object
Returns the value of attribute _loader_order.
4 5 6 |
# File 'lib/datasource/base.rb', line 4 def _loader_order @_loader_order end |
._loaders ⇒ Object
Returns the value of attribute _loaders.
4 5 6 |
# File 'lib/datasource/base.rb', line 4 def _loaders @_loaders end |
._update_scope ⇒ Object
Returns the value of attribute _update_scope.
4 5 6 |
# File 'lib/datasource/base.rb', line 4 def _update_scope @_update_scope end |
.default_consumer_adapter ⇒ Object
Should be set by consumer adapter library (e.g. for ActiveModelSerializers)
7 8 9 |
# File 'lib/datasource/base.rb', line 7 def default_consumer_adapter @default_consumer_adapter end |
.orm_klass ⇒ Object
23 24 25 |
# File 'lib/datasource/base.rb', line 23 def orm_klass fail Datasource::Error, "Model class not set for #{name}. You should define it:\nclass YourDatasource\n @orm_klass = MyModelClass\nend" end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
84 85 86 |
# File 'lib/datasource/base.rb', line 84 def adapter @adapter end |
#expose_associations ⇒ Object (readonly)
Returns the value of attribute expose_associations.
84 85 86 |
# File 'lib/datasource/base.rb', line 84 def expose_associations @expose_associations end |
#expose_attributes ⇒ Object (readonly)
Returns the value of attribute expose_attributes.
84 85 86 |
# File 'lib/datasource/base.rb', line 84 def expose_attributes @expose_attributes end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
84 85 86 |
# File 'lib/datasource/base.rb', line 84 def scope @scope end |
Class Method Details
._column_attribute_names ⇒ Object
46 47 48 49 50 |
# File 'lib/datasource/base.rb', line 46 def _column_attribute_names column_attributes = _attributes.values.select { |att| att[:klass].nil? }.map { |att| att[:name] } end |
.collection(&block) ⇒ Object
42 43 44 |
# File 'lib/datasource/base.rb', line 42 def collection(&block) _collection_context.class_exec(&block) end |
.default_adapter ⇒ Object
17 18 19 20 21 |
# File 'lib/datasource/base.rb', line 17 def default_adapter @adapter ||= begin Datasource::Adapters.const_get(Datasource::Adapters.constants.first) end end |
.inherited(base) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/datasource/base.rb', line 9 def inherited(base) base._attributes = (_attributes || {}).dup base._associations = (_associations || {}).dup base._loaders = (_loaders || {}).dup base._loader_order = (_loader_order || []).dup base._collection_context = Class.new(_collection_context || CollectionContext) end |
.primary_key ⇒ Object
27 28 29 |
# File 'lib/datasource/base.rb', line 27 def primary_key :id end |
.reflection_select(reflection, parent_select, assoc_select) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/datasource/base.rb', line 31 def reflection_select(reflection, parent_select, assoc_select) # append foreign key depending on assoication if reflection[:macro] == :belongs_to parent_select.push(reflection[:foreign_key]) elsif [:has_many, :has_one].include?(reflection[:macro]) assoc_select.push(reflection[:foreign_key]) else fail Datasource::Error, "unsupported association type #{reflection[:macro]} - TODO" end end |
Instance Method Details
#attribute_exposed?(name) ⇒ Boolean
230 231 232 |
# File 'lib/datasource/base.rb', line 230 def attribute_exposed?(name) @expose_attributes.include?(name.to_s) end |
#can_upgrade?(records) ⇒ Boolean
assume records have all attributes selected (default ORM record)
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/datasource/base.rb', line 235 def can_upgrade?(records) query_attributes = @expose_attributes.select do |name| klass = self.class._attributes[name][:klass] if klass klass.ancestors.include?(Attributes::QueryAttribute) end end return true if query_attributes.empty? Array(records).all? do |record| query_attributes.all? do |name| adapter.has_attribute?(record, name) end end end |
#fail_missing_attributes(names) ⇒ Object
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/datasource/base.rb', line 164 def fail_missing_attributes(names) = if names.size > 1 "attributes or associations #{names.join(', ')} don't exist " else "attribute or association #{names.first} doesn't exist " end += "for #{self.class.orm_klass.name}, " += "did you forget to call \"computed :#{names.first}, <dependencies>\" in your datasource_module?" fail Datasource::Error, end |
#get_collection_context(rows) ⇒ Object
255 256 257 |
# File 'lib/datasource/base.rb', line 255 def get_collection_context(rows) self.class._collection_context.new(@scope, rows, self, @params) end |
#get_exposed_loaders ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/datasource/base.rb', line 259 def get_exposed_loaders @expose_attributes .map { |name| self.class._attributes[name] }.select { |att| att[:klass] && att[:klass].ancestors.include?(Attributes::ComputedAttribute) }.flat_map { |att| att[:klass]._loader_depends }.uniq .sort_by { |loader_name| self.class._loader_order.index(loader_name) } end |
#get_select_values ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/datasource/base.rb', line 199 def get_select_values scope_table = adapter.primary_scope_table(self) select_values = Set.new if @select_all_columns select_values.add("#{scope_table}.*") self.class._attributes.values.each do |att| if att[:klass] && attribute_exposed?(att[:name]) if att[:klass].ancestors.include?(Attributes::QueryAttribute) select_values.add("(#{att[:klass].select_value}) as #{att[:name]}") end end end else select_values.add("#{scope_table}.#{self.class.primary_key}") self.class._attributes.values.each do |att| if attribute_exposed?(att[:name]) if att[:klass] == nil select_values.add("#{scope_table}.#{att[:name]}") elsif att[:klass].ancestors.include?(Attributes::QueryAttribute) select_values.add("(#{att[:klass].select_value}) as #{att[:name]}") end end end end select_values.to_a end |
#params(*args) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/datasource/base.rb', line 100 def params(*args) args.each do |arg| if arg.kind_of?(Hash) @params.deep_merge!(arg.symbolize_keys) elsif arg.is_a?(Symbol) @params.merge!(arg => true) else fail Datasource::Error, "unknown parameter type #{arg.class}" end end @params end |
#results(rows = nil) ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/datasource/base.rb', line 320 def results(rows = nil) rows ||= adapter.get_rows(self) collection_context = run_loaders(rows) rows.each do |row| row._datasource_instance = self set_row_loaded_values(collection_context, row) if collection_context end rows end |
#run_loaders(rows) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/datasource/base.rb', line 273 def run_loaders(rows) return if rows.empty? # check if loaders have already been ran if rows.first._datasource_loaded # not frequent, so we can afford to check all rows check_list = get_exposed_loaders run_list = [] rows.each do |row| if row._datasource_loaded check_list.delete_if do |name| if !row._datasource_loaded.key?(name) run_list << name true end end break if check_list.empty? else run_list.concat(check_list) break end end else # most frequent case - loaders haven't been ran run_list = get_exposed_loaders end get_collection_context(rows).tap do |collection_context| run_list.each do |loader_name| loader = self.class._loaders[loader_name] or fail Datasource::Error, "loader with name :#{loader_name} could not be found" Datasource.logger.info { "Running loader #{loader_name} for #{rows.first.try!(:class)}" } collection_context.loaded_values[loader_name] = loader.load(collection_context) end end end |
#select(*names) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/datasource/base.rb', line 130 def select(*names) newly_exposed_attributes = [] missing_attributes = [] names.each do |name| if name.kind_of?(Hash) name.each_pair do |assoc_name, assoc_select| assoc_name = assoc_name.to_s if self.class._associations.key?(assoc_name) @expose_associations[assoc_name] ||= [] @expose_associations[assoc_name].concat(Array(assoc_select)) @expose_associations[assoc_name].uniq! else missing_attributes << assoc_name end end else name = name.to_s if name == "*" select_all_columns elsif self.class._attributes.key?(name) unless @expose_attributes.include?(name) @expose_attributes.push(name) newly_exposed_attributes.push(name) end else missing_attributes << name end end end update_dependencies(newly_exposed_attributes) unless newly_exposed_attributes.empty? fail_missing_attributes(missing_attributes) if Datasource.config.raise_error_on_unknown_attribute_select && !missing_attributes.empty? self end |
#select_all ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/datasource/base.rb', line 122 def select_all attributes = self.class._attributes.keys select(*attributes) @select_all_columns = true attributes end |
#select_all_columns ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/datasource/base.rb', line 114 def select_all_columns columns = self.class._column_attribute_names select(*columns) @select_all_columns = true columns end |
#set_row_loaded_values(collection_context, row) ⇒ Object
311 312 313 314 315 316 317 318 |
# File 'lib/datasource/base.rb', line 311 def set_row_loaded_values(collection_context, row) row._datasource_loaded ||= {} primary_key = row.send(self.class.primary_key) collection_context.loaded_values.each_pair do |name, values| row._datasource_loaded[name] = values[primary_key] end end |
#update_dependencies(names) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/datasource/base.rb', line 175 def update_dependencies(names) scope_table = adapter.primary_scope_table(self) self.class._attributes.values.each do |att| next unless names.include?(att[:name]) next unless att[:klass] if att[:klass].ancestors.include?(Attributes::ComputedAttribute) att[:klass]._depends.each_pair do |key, value| if key.to_s == scope_table select(*value) else select(key => value) end end elsif att[:klass].ancestors.include?(Attributes::QueryAttribute) att[:klass]._depends.each do |name| next if name == scope_table adapter.ensure_table_join!(self, name, att) end end end end |
#upgrade_records(records) ⇒ Object
251 252 253 |
# File 'lib/datasource/base.rb', line 251 def upgrade_records(records) adapter.upgrade_records(self, Array(records)) end |