Class: Cyrax::Presenters::BaseCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/cyrax/presenters/base_collection.rb

Direct Known Subclasses

DecoratedCollection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, options = {}) ⇒ BaseCollection

Returns a new instance of BaseCollection.



5
6
7
8
# File 'lib/cyrax/presenters/base_collection.rb', line 5

def initialize(collection, options = {})
  @collection = collection
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



27
28
29
30
# File 'lib/cyrax/presenters/base_collection.rb', line 27

def method_missing(method, *args, &block)
  return super unless collection.respond_to?(method)
  collection.send(method, *args, &block)
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



3
4
5
# File 'lib/cyrax/presenters/base_collection.rb', line 3

def collection
  @collection
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/cyrax/presenters/base_collection.rb', line 3

def options
  @options
end

Instance Method Details

#as_json(*args) ⇒ Object



23
24
25
# File 'lib/cyrax/presenters/base_collection.rb', line 23

def as_json(*args)
  presented_collection.as_json(*args)
end

#presented_collectionObject



13
14
15
16
17
18
19
20
21
# File 'lib/cyrax/presenters/base_collection.rb', line 13

def presented_collection
  if collection.is_a?(ActiveRecord::Relation)
    collection.to_a
  elsif collection.respond_to?(:all)
    collection.all.to_a
  else
    Array.wrap collection
  end
end