module AbsorbApi::Relations
Public Instance Methods
create(attributes = []) { |object| ... }
click to toggle source
# File lib/absorb_api/relations.rb, line 45 def create(attributes = [], &block) object = to_s.classify.constantize.new(attributes) yield object if block_given? attrs = JSON.parse(object.to_json) attrs.keys.each { |k| attrs[ k.camelize ] = attrs.delete(k) } response = Base.api.post("#{to_s.demodulize.pluralize}", attrs) if response.status == 500 raise ValidationError elsif response.status == 405 raise RouteNotFound else object.id = response.body["Id"] object end end
has_many(rel_name, klass = nil)
click to toggle source
# File lib/absorb_api/relations.rb, line 6 def has_many(rel_name, klass = nil) klass ||= rel_name define_method "#{rel_name.to_s}" do |**conditions| AbsorbApi.api.get("#{self.class.to_s.demodulize.pluralize}/#{id}/#{rel_name.to_s}", conditions).body.map! do |attributes| "AbsorbApi::#{klass.to_s.classify}".constantize.new(attributes) end end define_method "find_#{rel_name.to_s.singularize}" do |child_id| response = AbsorbApi.api.get("#{self.class.to_s.demodulize.pluralize}/#{id}/#{rel_name.to_s}/#{child_id}") if response.status == 400 raise ResourceNotFound else "AbsorbApi::#{klass.to_s.classify}".constantize.new(response.body) end end define_method "#{rel_name.to_s.singularize}_ids" do AbsorbApi.api.get("#{self.class.to_s.demodulize.pluralize}/#{id}/#{rel_name.to_s}").body.map! do |attributes| "AbsorbApi::#{klass.to_s.classify}".constantize.new(attributes) end.map(&:id) end end
has_one(rel_name, klass = nil)
click to toggle source
# File lib/absorb_api/relations.rb, line 31 def has_one(rel_name, klass = nil) klass ||= rel_name define_method "#{rel_name.to_s}" do "AbsorbApi::#{klass.to_s.classify}".constantize.new(AbsorbApi.api.get("#{klass.to_s.pluralize}/"+ send(rel_name.to_s + "_id")).body) end end
where(**conditions)
click to toggle source
# File lib/absorb_api/relations.rb, line 39 def where(**conditions) Collection.new( AbsorbApi.api.get("#{to_s.demodulize.pluralize}", conditions).body.map! do |attributes| new(attributes) end, {klass: to_s.demodulize } ) end