Sha256: 166b8604bd31642c24c15ae4532bc09e608c58ae8f728a7f0f74282d4d6f6af9

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

module Almodovar
  
  class ResourcePresenter::Collection
    
    def initialize(resource_class, resources_args = [], options = {})
      @resource_class = resource_class
      @resources = resources_args.map { |arg| @resource_class.new(arg) }
      
      @total = options[:total_entries]
      @next = options[:next_link]
      @prev = options[:prev_link]
    end
    
    def to_xml(options = {})
      # Most of the following code is borrowed from ActiveSupport's Array#to_xml.
      # We cannot use Array#to_xml because we need to add a few custom tags for
      # pagination.
      require 'active_support/builder' unless defined?(Builder)

      options = options.dup
      options[:indent]  ||= 2
      options[:builder] ||= ::Builder::XmlMarkup.new(:indent => options[:indent])

      xml = options[:builder]

      xml.instruct! unless options[:skip_instruct]
      xml.tag! resource_type.pluralize.dasherize, :type => 'array' do
        xml.tag!('total-entries', @total) if @total
        xml.link :rel => 'next', :href => @next if @next
        xml.link :rel => 'prev', :href => @prev if @prev
        @resources.each { |value| value.to_xml(options.merge(:root => resource_type.singularize, :skip_instruct => true)) }
      end
    end
    
    def as_json(options = {})
      ActiveSupport::OrderedHash.new.tap do |message|
        message[:total_entries] = @total if @total
        message[:next_link] = @next if @next
        message[:prev_link] = @prev if @prev
        message[:entries] = @resources.map { |resource| resource.as_json(options) }
      end
    end
    
    def to_json(options = {})
      require 'yajl'
      Yajl::Encoder.encode(as_json(options), :pretty => true) + "\n"
    end
    
    def resource_type
      @resource_class.resource_type
    end

  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
almodovar-0.7.0 lib/almodovar/resource_presenter/collection.rb
almodovar-0.6.2 lib/almodovar/resource_presenter/collection.rb
almodovar-0.6.1 lib/almodovar/resource_presenter/collection.rb