lib/roar/rails/responder.rb in roar-rails-0.0.3 vs lib/roar/rails/responder.rb in roar-rails-0.0.4

- old
+ new

@@ -1,23 +1,31 @@ module Roar::Rails - module Responder - def extend_with_representer!(resource, representer=nil) - representer ||= representer_for_resource(resource) - resource.extend(representer) + module ModelMethods + # DISCUSS: move this into a generic namespace as we could need that in Sinatra as well. + def extend_with_representer!(model, representer=nil) + representer ||= representer_for_model(model) + model.extend(representer) end - def display(resource, given_options={}) - if resource.respond_to?(:map!) - resource.map! do |r| + + private + def representer_for_model(model) + (model.class.name + "Representer").constantize + end + end + + module Responder + include ModelMethods + + def display(model, given_options={}) + # TODO: remove the [] semantics, this should be done with a Collection representer. + if model.respond_to?(:map!) + model.map! do |r| extend_with_representer!(r) r.to_hash end else - extend_with_representer!(resource, options.delete(:with_representer)) + extend_with_representer!(model, options.delete(:with_representer)) end super - end - private - def representer_for_resource(resource) - (resource.class.name + "Representer").constantize end end end