Module: ActiveCollection::Serialization

Defined in:
lib/active_collection/serialization.rb

Instance Method Summary collapse

Instance Method Details

#as_data_hashObject



9
10
11
12
13
# File 'lib/active_collection/serialization.rb', line 9

def as_data_hash
  data_hash = { table_name => collection.as_json }
  data_hash["total_entries"] = total_entries
  data_hash
end

#as_json(options = nil) ⇒ Object



30
31
32
# File 'lib/active_collection/serialization.rb', line 30

def as_json(options = nil)
  {"collection" => as_data_hash}.as_json(options)
end

#to_paramObject

Turn the params into a hash suitable such that passing the collection directly to a named path generates the path for the current collection.



5
6
7
# File 'lib/active_collection/serialization.rb', line 5

def to_param
  params.empty?? nil : params.to_param
end

#to_xml(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_collection/serialization.rb', line 15

def to_xml(options = {})
  collect
  options[:indent] ||= 2
  xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
  xml.instruct! unless options[:skip_instruct]
  xml.collection do
    xml.total_entries(total_entries, :type => "integer")
    xml.tag!(table_name, :type => "array") do
      collection.each do |item|
        item.to_xml(:indent => options[:indent], :builder => xml, :skip_instruct => true)
      end
    end
  end
end