Sha256: 32ffaca4c2c80c9d899229feb46dbe7f07a656429d294d859c879dd8ab035119

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

# Introspective methods to assist in the conversion of collections in other formats.
class Array
  def to_atom(options={}, &block)
    raise "Not all elements respond to to_atom" unless all? { |e| e.respond_to? :to_atom }
    options = options.dup
    
    if options.delete :only_self_link
      options[:skip_associations_links] = true
      options[:skip_attributes]         = true      
    end
    
    Atom::Feed.new do |feed|
      # TODO: Define better feed attributes
      # Array#to_s can return a very long string
      feed.title   = "Collection of #{map {|i| i.class.name }.uniq.to_sentence}"
      # TODO: this id does not comply with Rest standards yet
      feed.id      = hash
      feed.updated = updated_at
    
      each do |element|
        feed.entries << element.to_atom(options)
      end
      
      yield feed if block_given?
    end
  end
  
  # Return max update date for items in collection, for it uses the updated_at of items.
  # 
  #==Example:
  #
  # Find max updated at in ActiveRecord objects 
  #
  #   albums = Albums.find(:all)
  #   albums.updated_at
  # 
  # Using a custom field to check the max date
  #
  #   albums = Albums.find(:all)
  #   albums.updated_at(:created_at)
  #
  def updated_at(field = :updated_at)
    map { |item| item.send(field) if item.respond_to?(field) }.compact.max || Time.now
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
restfulie-0.7.2 lib/restfulie/server/core_ext/array.rb
restfulie-0.7.1 lib/restfulie/server/core_ext/array.rb
restfulie-0.7.0 lib/restfulie/server/core_ext/array.rb