Module: NCore::Associations
- Defined in:
- lib/ncore/associations.rb
Instance Method Summary collapse
-
#belongs_to(assoc_name, association_key: nil, class_name: nil) ⇒ Object
assoc_name - singular association name :association_key - key on this resource used to reference the parent association defaults to ‘assoc_name+’_id’‘ :class_name - Module::Class of the parent association, as a string.
-
#has_many(assoc_name, association_key: nil, class_name: nil) ⇒ Object
assoc_name - plural association name :association_key - key used by the association to reference the parent defaults to ‘attrib_name+’_id’‘ :class_name - Module::Class of the child association, as a string.
-
#has_one(assoc_name, association_key: nil, class_name: nil) ⇒ Object
assoc_name - singular association name :association_key - key used by the association to reference the parent defaults to ‘attrib_name+’_id’‘ :class_name - Module::Class of the child association, as a string.
Instance Method Details
#belongs_to(assoc_name, association_key: nil, class_name: nil) ⇒ Object
assoc_name - singular association name :association_key - key on this resource used to reference the parent association
defaults to `assoc_name+'_id'`
:class_name - Module::Class of the parent association, as a string
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 229 230 231 232 233 234 235 236 |
# File 'lib/ncore/associations.rb', line 202 def belongs_to(assoc_name, association_key: nil, class_name: nil) assoc_name = assoc_name.to_s parent_key = association_key&.to_s || "#{assoc_name}_id" klass = class_name || "#{module_name}::#{assoc_name.camelize}" # attr :parent_id # def parent({}) class_eval <<-P1, __FILE__, __LINE__+1 attr :#{parent_key} def #{assoc_name}(params={}) reload = params.delete :reload params = parse_request_params(params).reverse_merge credentials: api_creds if params.except(:credentials, :request).empty? # only cache unfiltered, default api call @attribs[:#{assoc_name}] = nil if reload @attribs[:#{assoc_name}] ||= #{parent_key} && #{klass}.find(#{parent_key}, params) else return nil unless #{parent_key} #{klass}.find(#{parent_key}, params) end end P1 # private def parent_id=(val) # if :parent_key changes, clear memoized association class_eval <<-P2, __FILE__, __LINE__+1 def #{parent_key}=(v) if @attribs[:#{parent_key}] != v and @attribs[:#{assoc_name}]&.id != v @attribs[:#{assoc_name}] = nil end @attribs[:#{parent_key}] = v end private :#{parent_key}= P2 end |
#has_many(assoc_name, association_key: nil, class_name: nil) ⇒ Object
assoc_name - plural association name :association_key - key used by the association to reference the parent
defaults to `attrib_name+'_id'`
:class_name - Module::Class of the child association, as a string
91 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 126 127 128 129 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/ncore/associations.rb', line 91 def has_many(assoc_name, association_key: nil, class_name: nil) assoc_name = assoc_name.to_s parent_key = association_key&.to_s || "#{attrib_name}_id" klass = class_name || "#{module_name}::#{assoc_name.camelize.singularize}" # def items({}) class_eval <<-A1, __FILE__, __LINE__+1 def #{assoc_name}(params={}) return [] unless id reload = params.delete :reload cacheable = params.except(:credentials, :request).empty? params = parse_request_params(params).reverse_merge credentials: api_creds params[:#{parent_key}] = id if cacheable # only cache unfiltered, default api call @attribs[:#{assoc_name}] = (!reload && @attribs[:#{assoc_name}]) || #{klass}.all(params) else #{klass}.all(params) end end A1 # def find_item(id, {}) class_eval <<-F1, __FILE__, __LINE__+1 def find_#{assoc_name.singularize}(aid, params={}) raise UnsavedObjectError unless id params = parse_request_params(params).reverse_merge credentials: api_creds params[:#{parent_key}] = id #{klass}.find(aid, params) end F1 # def retrieve_item(id, {}) class_eval <<-F2, __FILE__, __LINE__+1 def retrieve_#{assoc_name.singularize}(aid, params={}) raise UnsavedObjectError unless id params = parse_request_params(params).reverse_merge credentials: api_creds params[:#{parent_key}] = id #{klass}.retrieve(aid, params) end F2 # def create_item({}) # will always return the object; check .errors? or .valid? to see how it went class_eval <<-C1, __FILE__, __LINE__+1 def create_#{assoc_name.singularize}(params={}) raise UnsavedObjectError unless id params = parse_request_params(params).reverse_merge credentials: api_creds params[:#{parent_key}] = id #{klass}.create(params) end C1 # def create_item!({}) class_eval <<-C2, __FILE__, __LINE__+1 def create_#{assoc_name.singularize}!(params={}) raise UnsavedObjectError unless id params = parse_request_params(params).reverse_merge credentials: api_creds params[:#{parent_key}] = id #{klass}.create!(params) end C2 # def update_item(id, {}) # will always return the object; check .errors? or .valid? to see how it went class_eval <<-U1, __FILE__, __LINE__+1 def update_#{assoc_name.singularize}(aid, params={}) raise UnsavedObjectError unless id params = parse_request_params(params).reverse_merge credentials: api_creds params[:#{parent_key}] = id #{klass}.update(aid, params) end U1 # def update_item!(id, {}) class_eval <<-U2, __FILE__, __LINE__+1 def update_#{assoc_name.singularize}!(aid, params={}) raise UnsavedObjectError unless id params = parse_request_params(params).reverse_merge credentials: api_creds params[:#{parent_key}] = id #{klass}.update!(aid, params) end U2 # def delete_item(id, {}) # will always return the object; check .errors? or .valid? to see how it went class_eval <<-D1, __FILE__, __LINE__+1 def delete_#{assoc_name.singularize}(aid, params={}) raise UnsavedObjectError unless id params = parse_request_params(params).reverse_merge credentials: api_creds params[:#{parent_key}] = id #{klass}.delete(aid, params) end D1 # def delete_item!(id, {}) class_eval <<-D2, __FILE__, __LINE__+1 def delete_#{assoc_name.singularize}!(aid, params={}) raise UnsavedObjectError unless id params = parse_request_params(params).reverse_merge credentials: api_creds params[:#{parent_key}] = id #{klass}.delete!(aid, params) end D2 end |
#has_one(assoc_name, association_key: nil, class_name: nil) ⇒ Object
assoc_name - singular association name :association_key - key used by the association to reference the parent
defaults to `attrib_name+'_id'`
:class_name - Module::Class of the child association, as a string
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ncore/associations.rb', line 8 def has_one(assoc_name, association_key: nil, class_name: nil) assoc_name = assoc_name.to_s parent_key = association_key&.to_s || "#{attrib_name}_id" klass = class_name || "#{module_name}::#{assoc_name.camelize}" # def item({}) class_eval <<-A1, __FILE__, __LINE__+1 def #{assoc_name}(params={}) return nil unless id reload = params.delete :reload cacheable = params.except(:credentials, :request).empty? params = parse_request_params(params).reverse_merge credentials: api_creds params[:#{parent_key}] = id if cacheable # only cache unfiltered, default api call @attribs[:#{assoc_name}] = (!reload && @attribs[:#{assoc_name}]) || #{klass}.first(params) else #{klass}.first(params) end end A1 # def create_item({}) # will always return the object; check .errors? or .valid? to see how it went class_eval <<-C1, __FILE__, __LINE__+1 def create_#{assoc_name}(params={}) raise UnsavedObjectError unless id params = parse_request_params(params).reverse_merge credentials: api_creds params[:#{parent_key}] = id #{klass}.create(params) end C1 # def create_item!({}) class_eval <<-C2, __FILE__, __LINE__+1 def create_#{assoc_name}!(params={}) raise UnsavedObjectError unless id params = parse_request_params(params).reverse_merge credentials: api_creds params[:#{parent_key}] = id #{klass}.create!(params) end C2 # def update_item({}) # will always return the object; check .errors? or .valid? to see how it went class_eval <<-U1, __FILE__, __LINE__+1 def update_#{assoc_name}(params={}) raise UnsavedObjectError unless id #{assoc_name}.update(params) end U1 # def update_item!({}) class_eval <<-U2, __FILE__, __LINE__+1 def update_#{assoc_name}!(params={}) raise UnsavedObjectError unless id #{assoc_name}.update!(params) end U2 # def delete_item({}) # will always return the object; check .errors? or .valid? to see how it went class_eval <<-D1, __FILE__, __LINE__+1 def delete_#{assoc_name}(params={}) raise UnsavedObjectError unless id #{assoc_name}.delete(params) end D1 # def delete_item!({}) class_eval <<-D2, __FILE__, __LINE__+1 def delete_#{assoc_name}!(params={}) raise UnsavedObjectError unless id #{assoc_name}.delete!(params) end D2 end |