Skip to content

Improvements and refactorings #60

Merged
merged 6 commits into from Apr 3, 2012
View
89 lib/acts_as_api/api_template.rb
@@ -11,18 +11,11 @@ class ApiTemplate < Hash
# The name of the api template as a Symbol.
attr_accessor :api_template
-
- attr_reader :options
- # Returns a new ApiTemplate with the api template name
- # set to the passed template.
- def self.create(template)
- t = ApiTemplate.new
- t.api_template = template
- return t
- end
+ attr_reader :options
- def initialize
+ def initialize(api_template)
+ self.api_template = api_template
@options ||= {}
end
@@ -67,7 +60,6 @@ def option_for(field, option)
# If a special template name for the passed item is specified
# it will be returned, if not the original api template.
def api_template_for(fieldset, field)
- return api_template unless fieldset.is_a? ActsAsApi::ApiTemplate
fieldset.option_for(field, :template) || api_template
end
@@ -90,59 +82,44 @@ def condition_fulfilled?(model, condition)
when Proc
result = condition.call(model)
end
- !result.nil? && !result.is_a?(FalseClass)
+ !!result
end
# Generates a hash that represents the api response based on this
# template for the passed model instance.
- def to_response_hash(model)
- queue = []
+ def to_response_hash(model, fieldset = self)
api_output = {}
-
- queue << { :output => api_output, :item => self }
-
- until queue.empty? do
- leaf = queue.pop
- fieldset = leaf[:item]
-
- fieldset.each do |field, value|
-
- next unless allowed_to_render?(fieldset, field, model)
-
- case value
- when Symbol
- if model.respond_to?(value)
- out = model.send value
- end
-
- when Proc
- out = value.call(model)
-
- when String
- # go up the call chain
- out = model
- value.split(".").each do |method|
- out = out.send(method.to_sym)
- end
-
- when Hash
- leaf[:output][field] ||= {}
- queue << { :output => leaf[:output][field], :item => value }
- next
- end
-
- if out.respond_to?(:as_api_response)
- sub_template = api_template_for(fieldset, field)
- out = out.send(:as_api_response, sub_template)
- end
-
- leaf[:output][field] = out
+
+ fieldset.each do |field, value|
+ next unless allowed_to_render?(fieldset, field, model)
+
+ out = process_value(model, value)
+
+ if out.respond_to?(:as_api_response)
+ sub_template = api_template_for(fieldset, field)
+ out = out.as_api_response(sub_template)
end
-
+
+ api_output[field] = out
end
-
+
api_output
end
-
+
+ private
+
+ def process_value(model, value)
+ case value
+ when Symbol
+ model.send(value)
+ when Proc
+ value.call(model)
+ when String
+ value.split('.').inject(model) { |result, method| result.send(method) }
+ when Hash
+ to_response_hash(model, value)
+ end
+ end
+
end
end
View
28 lib/acts_as_api/base.rb
@@ -34,7 +34,7 @@ def acts_as_api?#:nodoc:
# be contained in the api responses.
def api_accessible(api_template, options = {}, &block)
- attributes = api_accessible_attributes(api_template) || ApiTemplate.create(api_template)
+ attributes = api_accessible_attributes(api_template) || ApiTemplate.new(api_template)
attributes.merge!(api_accessible_attributes(options[:extend])) if options[:extend]
@@ -59,22 +59,28 @@ module InstanceMethods
def as_api_response(api_template)
api_attributes = self.class.api_accessible_attributes(api_template)
raise ActsAsApi::TemplateNotFoundError.new("acts_as_api template :#{api_template.to_s} was not found for model #{self.class}") if api_attributes.nil?
-
- before_api_response(api_template) if respond_to? :before_api_response
-
- response_hash = if respond_to? :around_api_response
- around_api_response api_template do
- api_attributes.to_response_hash(self)
- end
- else
+
+ before_api_response(api_template)
+ response_hash = around_api_response(api_template) do
api_attributes.to_response_hash(self)
end
-
- after_api_response(api_template) if respond_to? :after_api_response
+ after_api_response(api_template)
response_hash
end
+ protected
+
+ def before_api_response(api_remplate)
+ end
+
+ def after_api_response(api_remplate)
+ end
+
+ def around_api_response(api_remplate)
+ yield
+ end
+
end
end
Something went wrong with that request. Please try again.