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
- .computed(name, *_deps) ⇒ Object
- .consumer_adapter ⇒ Object
- .default_adapter ⇒ Object
- .inherited(base) ⇒ Object
- .loaded(name, _options = {}, &block) ⇒ Object
- .loader(name, _options = {}, &block) ⇒ Object
- .primary_key ⇒ Object
- .query(name, deps = nil, value = nil, &block) ⇒ 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
- #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 |
# 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 = {} 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
.computed(name, *_deps) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/datasource/attributes/computed_attribute.rb', line 29 def self.computed(name, *_deps) deps = _deps.select { |dep| dep.kind_of?(Hash) } _deps.reject! { |dep| dep.kind_of?(Hash) } unless _deps.empty? self_key = default_adapter.get_table_name(orm_klass) deps.push(self_key => _deps) end klass = Class.new(Attributes::ComputedAttribute) do depends *deps end attribute name, klass end |
.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 |
.loaded(name, _options = {}, &block) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/datasource/attributes/loader.rb', line 69 def self.loaded(name, = {}, &block) loader(name, , &block) renamed_existing_method = :"#{name}_without_datasource" orm_klass.class_eval do alias_method renamed_existing_method, name if method_defined?(name) fail "#{name} already defined on #{to_s}, would be overridden by datasource loaded method" if method_defined?(name) define_method name do |*args, &block| if loaded_values || !method_defined?(renamed_existing_method) loaded_values[name.to_sym] else send(renamed_existing_method, *args, &block) end end end computed name, loader: name end |
.loader(name, _options = {}, &block) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/datasource/attributes/loader.rb', line 60 def self.loader(name, = {}, &block) klass = Class.new(Attributes::Loader) do # depends deps () self._load_proc = block end @_loaders[name.to_sym] = klass end |
.primary_key ⇒ Object
27 28 29 |
# File 'lib/datasource/base.rb', line 27 def primary_key :id end |
.query(name, deps = nil, value = nil, &block) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/datasource/attributes/query_attribute.rb', line 20 def self.query(name, deps = nil, value = nil, &block) klass = Class.new(Attributes::QueryAttribute) do depends deps if deps if block define_singleton_method(:select_value, &block) else define_singleton_method(:select_value) { value } end end attribute name, klass 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
182 183 184 |
# File 'lib/datasource/base.rb', line 182 def attribute_exposed?(name) @expose_attributes.include?(name.to_s) end |
#can_upgrade?(records) ⇒ Boolean
assume records have all attributes selected (default ORM record)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/datasource/base.rb', line 187 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
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/datasource/base.rb', line 127 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 :#{name}, <dependencies>\" in your datasource_module?" fail Datasource::Error, end |
#get_select_values ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/datasource/base.rb', line 162 def get_select_values scope_table = adapter.primary_scope_table(self) # SQL select values select_values = Set.new 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 select_values.to_a end |
#results(rows = nil) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/datasource/base.rb', line 207 def results(rows = nil) rows ||= adapter.get_rows(self) @expose_attributes.each do |name| att = self.class._attributes[name] fail Datasource::Error, "attribute #{name} doesn't exist for #{self.class.orm_klass.name}, did you forget to call \"computed :#{name}, :db_column_dependency\" in your datasource_module? See https://github.com/mrbrdo/datasource#model-methods--virtual-attributes" unless att klass = att[:klass] next unless klass next if rows.empty? 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| row.loaded_values[name] = loaded_values[row.send(self.class.primary_key)] || loader.default_value end end else raise Datasource::Error, "loader with name :#{name} could not be found" end end end end rows end |
#select(*names) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/datasource/base.rb', line 92 def select(*names) failure = ->(name) { fail Datasource::Error, "attribute or association #{name} doesn't exist for #{self.class.orm_klass.name}, did you forget to call \"computed :#{name}, <dependencies>\" in your datasource_module?" } 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] += Array(assoc_select) @expose_associations[assoc_name].uniq! else missing_attributes << assoc_name failure.call(assoc_name) end end else name = name.to_s if self.class._attributes.key?(name) unless @expose_attributes.include?(name) @expose_attributes.push(name) newly_exposed_attributes.push(name) end else missing_attributes << name failure.call(name) end end end update_dependencies(newly_exposed_attributes) unless newly_exposed_attributes.empty? fail_missing_attributes(missing_attributes) unless missing_attributes.blank? self end |
#select_all ⇒ Object
88 89 90 |
# File 'lib/datasource/base.rb', line 88 def select_all @expose_attributes = self.class._attributes.keys.dup end |
#update_dependencies(names) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/datasource/base.rb', line 138 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
203 204 205 |
# File 'lib/datasource/base.rb', line 203 def upgrade_records(records) adapter.upgrade_records(self, Array(records)) end |