module SurveyGizmo::Resource
Public Class Methods
descendants()
click to toggle source
# File lib/survey_gizmo/resource.rb, line 16 def self.descendants @descendants ||= Set.new end
Public Instance Methods
destroy()
click to toggle source
Delete the Resource
from Survey Gizmo
# File lib/survey_gizmo/resource.rb, line 151 def destroy fail "No id; can't delete #{self.inspect}!" unless id Connection.delete(create_route(:delete)) end
inspect()
click to toggle source
# File lib/survey_gizmo/resource.rb, line 156 def inspect attribute_strings = self.class.attribute_set.map do |attrib| value = self.send(attrib.name) value = value.is_a?(Hash) ? value.inspect : value.to_s " \"#{attrib.name}\" => \"#{value}\"\n" unless value.strip.blank? end.compact "#<#{self.class.name}:#{self.object_id}>\n#{attribute_strings.join}" end
reload()
click to toggle source
Repopulate the attributes based on what is on SurveyGizmo's servers
# File lib/survey_gizmo/resource.rb, line 145 def reload self.attributes = Connection.get(create_route(:get)).body['data'] self end
save()
click to toggle source
If we have an id, it's an update because we already know the surveygizmo assigned id Returns itself if successfully saved, but with attributes (like id) added by SurveyGizmo
# File lib/survey_gizmo/resource.rb, line 138 def save method, path = id ? [:post, :update] : [:put, :create] self.attributes = Connection.send(method, create_route(path), attributes_without_blanks).body['data'] self end
Private Instance Methods
attributes_without_blanks()
click to toggle source
# File lib/survey_gizmo/resource.rb, line 168 def attributes_without_blanks attributes.reject { |k, v| v.blank? } end
children_params()
click to toggle source
Attributes that should be passed down the object hierarchy - e.g. a Question should have a survey_id Also used for loading member objects, e.g. loading Options for a given Question.
# File lib/survey_gizmo/resource.rb, line 188 def children_params klass_id = self.class.name.split('::').last.downcase + '_id' route_params.merge(klass_id.to_sym => id).reject { |k, v| k == :id } end
create_route(method)
click to toggle source
# File lib/survey_gizmo/resource.rb, line 193 def create_route(method) self.class.create_route(method, route_params) end
route_params()
click to toggle source
Extract attributes required for API
calls about this object
# File lib/survey_gizmo/resource.rb, line 173 def route_params params = { id: id } self.class.routes.values.each do |route| route.gsub(/:(\w+)/) do |m| m = m.delete(':').to_sym params[m] = self.send(m) end end params end