Class: Almodovar::ResourcePresenter::Collection
- Inherits:
-
Object
- Object
- Almodovar::ResourcePresenter::Collection
- Defined in:
- lib/almodovar-server/resource_presenter/collection.rb
Instance Attribute Summary collapse
-
#resource_class ⇒ Object
readonly
Returns the value of attribute resource_class.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
-
#initialize(resource_class, resources_args = [], options = {}) ⇒ Collection
constructor
A new instance of Collection.
- #next_link ⇒ Object
- #prev_link ⇒ Object
- #resource_type ⇒ Object
- #to_html(options = {}) ⇒ Object
- #to_json(options = {}) ⇒ Object
- #to_xml(options = {}) ⇒ Object
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 = [], = {}) @resource_class = resource_class @resources = resources_args.map { |arg| @resource_class.new(arg) } @total = [:total_entries] @next = [:next_link] @prev = [:prev_link] end |
Instance Attribute Details
#resource_class ⇒ Object (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( = {}) = ActiveSupport::OrderedHash.new.tap do || [:total_entries] = @total if @total .merge! prev_link.as_json if prev_link .merge! next_link.as_json if next_link [:entries] = @resources.map { |resource| resource.as_json() } end end |
#next_link ⇒ Object
59 60 61 |
# File 'lib/almodovar-server/resource_presenter/collection.rb', line 59 def next_link Link.new(:next, @next) if @next end |
#prev_link ⇒ Object
63 64 65 |
# File 'lib/almodovar-server/resource_presenter/collection.rb', line 63 def prev_link Link.new(:prev, @prev) if @prev end |
#resource_type ⇒ Object
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( = {}) HtmlSerializer.new(self, ).to_html end |
#to_json(options = {}) ⇒ Object
46 47 48 49 |
# File 'lib/almodovar-server/resource_presenter/collection.rb', line 46 def to_json( = {}) require 'yajl' Yajl::Encoder.encode(as_json(), :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( = {}) # 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) = .dup [:indent] ||= 2 [:builder] ||= ::Builder::XmlMarkup.new(:indent => [:indent]) xml = [:builder] xml.instruct! unless [: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(.merge(:root => resource_type.singularize, :skip_instruct => true)) } end end |