Class: DataMapper::Collection

### Home Classes Methods

### In Files

- lib/dm-serializer/to_csv.rb
- lib/dm-serializer/to_json.rb
- lib/dm-serializer/to_xml.rb
- lib/dm-serializer/to_yaml.rb

### Parent

Object

### Methods

- #to_csv
- #to_json
- #to_xml
- #to_xml_document

### Included Modules

- DataMapper::Serializer::Collection::ToYaml

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- DataMapper
- DataMapper::Collection
- DataMapper::Serializer
- DataMapper::Serializer::Collection
- DataMapper::Serializer::Collection::ToYaml
- DataMapper::Serializer::ValidationErrors
- DataMapper::Serializer::ValidationErrors::ToCsv
- DataMapper::Serializer::ValidationErrors::ToJson
- DataMapper::Serializer::ValidationErrors::ToXml
- DataMapper::Serializer::ValidationErrors::ToYaml
- DataMapper::Serializer::XML
- DataMapper::Serializer::XML::LibXML
- DataMapper::Serializer::XML::Nokogiri
- DataMapper::Serializer::XML::REXML
- DataMapper::Validations
- DataMapper::Validations::ValidationErrors
- Object

No matching classes.

# DataMapper::Collection

### Public Instance Methods

to_csv(*args) click to toggle source

    # File lib/dm-serializer/to_csv.rb, line 55
    def to_csv(*args)
      result = ''
      each do |item|
        result << item.to_csv(args.first) + "\n"
      end
      result
    end

to_json(*args) click to toggle source

    # File lib/dm-serializer/to_json.rb, line 80
    def to_json(*args)
      options = args.first
      options = {} unless options.kind_of?(Hash)

      resource_options = options.merge(:to_json => false)
      collection = map { |resource| resource.to_json(resource_options) }

      # default to making JSON
      if options.fetch(:to_json, true)
        MultiJson.encode(collection)
      else
        collection
      end
    end

to_xml(opts = {}) click to toggle source

    # File lib/dm-serializer/to_xml.rb, line 93
    def to_xml(opts = {})
      to_xml_document(opts).to_s
    end

to_xml_document(opts = {}) click to toggle source

    # File lib/dm-serializer/to_xml.rb, line 97
    def to_xml_document(opts = {})
      xml = DataMapper::Serializer::XML.serializer
      doc = xml.new_document

      default_collection_element_name = lambda {
        DataMapper::Inflector.pluralize(DataMapper::Inflector.underscore(self.model.to_s)).tr("/", "-")
      }

      root = xml.root_node(
        doc,
        opts[:collection_element_name] || default_collection_element_name[],
        {'type' => 'array'}
      )

      self.each do |item|
        item.to_xml_document(opts, doc)
      end

      doc
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.