Class: Almodovar::ResourcePresenter::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/almodovar-server/resource_presenter/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_class, resources_args = [], options = {}) ⇒ Collection

Returns a new instance of Collection.



7
8
9
10
11
12
13
14
# File 'lib/almodovar-server/resource_presenter/collection.rb', line 7

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

Instance Attribute Details

#resource_classObject (readonly)

Returns the value of attribute resource_class.



5
6
7
# File 'lib/almodovar-server/resource_presenter/collection.rb', line 5

def resource_class
  @resource_class
end

Instance Method Details

#as_json(options = {}) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/almodovar-server/resource_presenter/collection.rb', line 37

def as_json(options = {})
  message = ActiveSupport::OrderedHash.new.tap do |message|
    message[:total_entries] = @total if @total
    message.merge! prev_link.as_json if prev_link
    message.merge! next_link.as_json if next_link 
    message[:entries] = @resources.map { |resource| resource.as_json(options) }
  end
end


59
60
61
# File 'lib/almodovar-server/resource_presenter/collection.rb', line 59

def next_link
  Link.new(:next, @next) if @next
end


63
64
65
# File 'lib/almodovar-server/resource_presenter/collection.rb', line 63

def prev_link
  Link.new(:prev, @prev) if @prev
end

#resource_typeObject



55
56
57
# File 'lib/almodovar-server/resource_presenter/collection.rb', line 55

def resource_type
  @resource_class.resource_type
end

#to_html(options = {}) ⇒ Object



51
52
53
# File 'lib/almodovar-server/resource_presenter/collection.rb', line 51

def to_html(options = {})
  HtmlSerializer.new(self, options).to_html
end

#to_json(options = {}) ⇒ Object



46
47
48
49
# File 'lib/almodovar-server/resource_presenter/collection.rb', line 46

def to_json(options = {})
  require 'yajl'
  Yajl::Encoder.encode(as_json(options), :pretty => true) + "\n"
end

#to_xml(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/almodovar-server/resource_presenter/collection.rb', line 16

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
    prev_link.to_xml(:builder => xml) if prev_link
    next_link.to_xml(:builder => xml) if next_link
    @resources.each { |value| value.to_xml(options.merge(:root => resource_type.singularize, :skip_instruct => true)) }
  end
end