Class: Datasource::Base
- Inherits:
-
Object
- Object
- Datasource::Base
- Defined in:
- lib/datasource/base.rb,
lib/datasource/attributes/loader.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.
-
._loaders ⇒ Object
Returns the value of attribute _loaders.
-
._update_scope ⇒ Object
Returns the value of attribute _update_scope.
- .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
- .consumer_adapter ⇒ 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_select_values ⇒ Object
-
#initialize(scope, adapter = nil) ⇒ Base
constructor
A new instance of Base.
- #results(rows = nil) ⇒ Object
- #select(*names) ⇒ Object
- #select_all ⇒ Object
- #select_all_columns ⇒ Object
- #update_dependencies(names) ⇒ Object
- #upgrade_records(records) ⇒ Object
Constructor Details
#initialize(scope, adapter = nil) ⇒ Base
Returns a new instance of Base.
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/datasource/base.rb', line 76 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 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 |
._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 |
.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.
74 75 76 |
# File 'lib/datasource/base.rb', line 74 def adapter @adapter end |
#expose_associations ⇒ Object (readonly)
Returns the value of attribute expose_associations.
74 75 76 |
# File 'lib/datasource/base.rb', line 74 def expose_associations @expose_associations end |
#expose_attributes ⇒ Object (readonly)
Returns the value of attribute expose_attributes.
74 75 76 |
# File 'lib/datasource/base.rb', line 74 def expose_attributes @expose_attributes end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
74 75 76 |
# File 'lib/datasource/base.rb', line 74 def scope @scope end |
Class Method Details
.consumer_adapter ⇒ Object
19 20 21 |
# File 'lib/datasource/base.rb', line 19 def consumer_adapter @consumer_adapter = Datasource::ConsumerAdapters::ActiveModelSerializers end |
.default_adapter ⇒ Object
13 14 15 16 17 |
# File 'lib/datasource/base.rb', line 13 def default_adapter @adapter ||= begin Datasource::Adapters.const_get(Datasource::Adapters.constants.first) end end |
.inherited(base) ⇒ Object
7 8 9 10 11 |
# File 'lib/datasource/base.rb', line 7 def inherited(base) base._attributes = (_attributes || {}).dup base._associations = (_associations || {}).dup base._loaders = (_loaders || {}).dup 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
208 209 210 |
# File 'lib/datasource/base.rb', line 208 def attribute_exposed?(name) @expose_attributes.include?(name.to_s) end |
#can_upgrade?(records) ⇒ Boolean
assume records have all attributes selected (default ORM record)
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/datasource/base.rb', line 213 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
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/datasource/base.rb', line 142 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_select_values ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/datasource/base.rb', line 177 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 |
#results(rows = nil) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/datasource/base.rb', line 233 def results(rows = nil) rows ||= adapter.get_rows(self) unless rows.empty? @expose_attributes.each do |name| att = self.class._attributes[name] klass = att[:klass] next unless klass if att[:klass].ancestors.include?(Attributes::ComputedAttribute) att[:klass]._loader_depends.each do |name| if loader = self.class._loaders[name] if loaded_values = loader.load(rows.map(&self.class.primary_key), rows, @scope) unless rows.first.loaded_values rows.each do |row| row.loaded_values = {} end end rows.each do |row| key = row.send(self.class.primary_key) if loaded_values.key?(key) row.loaded_values[name] = loaded_values[key] elsif loader.default_value row.loaded_values[name] = loader.default_value end end end else raise Datasource::Error, "loader with name :#{name} could not be found" end end end end end rows end |
#select(*names) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/datasource/base.rb', line 108 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) unless Datasource.config.simple_mode || missing_attributes.empty? self end |
#select_all ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/datasource/base.rb', line 100 def select_all attributes = self.class._attributes.keys select(*attributes) @select_all_columns = true attributes end |
#select_all_columns ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/datasource/base.rb', line 89 def select_all_columns column_attributes = self.class._attributes.values.select do |att| att[:klass].nil? end columns = column_attributes.map { |att| att[:name] } select(*columns) @select_all_columns = true columns end |
#update_dependencies(names) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/datasource/base.rb', line 153 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
229 230 231 |
# File 'lib/datasource/base.rb', line 229 def upgrade_records(records) adapter.upgrade_records(self, Array(records)) end |