Class: HalApi::PagedCollection

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, Forwardable
Defined in:
lib/hal_api/paged_collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items, request = nil, options = nil) ⇒ PagedCollection

Returns a new instance of PagedCollection.



31
32
33
34
35
36
# File 'lib/hal_api/paged_collection.rb', line 31

def initialize(items, request=nil, options=nil)
  self.items   = items
  self.request = request || request_stub
  self.options = options || {}
  self.options[:is_root_resource] = true unless (self.options[:is_root_resource] == false)
end

Instance Attribute Details

#facetsObject

Returns the value of attribute facets.



10
11
12
# File 'lib/hal_api/paged_collection.rb', line 10

def facets
  @facets
end

#itemsObject

Returns the value of attribute items.



10
11
12
# File 'lib/hal_api/paged_collection.rb', line 10

def items
  @items
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/hal_api/paged_collection.rb', line 10

def options
  @options
end

#requestObject

Returns the value of attribute request.



10
11
12
# File 'lib/hal_api/paged_collection.rb', line 10

def request
  @request
end

Class Method Details

.representerObject



19
20
21
# File 'lib/hal_api/paged_collection.rb', line 19

def self.representer
  representer_class || HalApi::PagedCollectionRepresenter
end

Instance Method Details

#cache_keyObject



38
39
40
41
42
43
44
45
46
# File 'lib/hal_api/paged_collection.rb', line 38

def cache_key
  item_keys = items.inject([]) do |keys, i|
    keys << i.try(:id)
    keys << i.try(:updated_at).try(:utc).to_i
  end
  key_components = ['c', item_class.model_name.cache_key]
  key_components << OpenSSL::Digest::MD5.hexdigest(item_keys.join)
  ActiveSupport::Cache.expand_cache_key(key_components)
end

#countObject



82
83
84
# File 'lib/hal_api/paged_collection.rb', line 82

def count
  items.length
end

#is_root_resourceObject



52
53
54
# File 'lib/hal_api/paged_collection.rb', line 52

def is_root_resource
  !!self.options[:is_root_resource]
end

#item_classObject



60
61
62
# File 'lib/hal_api/paged_collection.rb', line 60

def item_class
  options[:item_class] || self.items.first.try(:item_class) || self.items.first.class
end

#item_decoratorObject



64
65
66
# File 'lib/hal_api/paged_collection.rb', line 64

def item_decorator
  options[:item_decorator] || "Api::#{item_class.name}Representer".constantize
end

#parentObject

If this is an embedded collection, the parent will be set here for use in urls



74
75
76
77
78
79
80
# File 'lib/hal_api/paged_collection.rb', line 74

def parent
  rep = options[:parent]
  return rep unless rep.respond_to?(:becomes, true)

  klass = rep.class.try(:base_class)
  klass && (klass != rep.class) ? rep.becomes(klass) : rep
end

#persisted?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/hal_api/paged_collection.rb', line 27

def persisted?
  false
end

#request_stubObject



48
49
50
# File 'lib/hal_api/paged_collection.rb', line 48

def request_stub
  OpenStruct.new(params: {})
end

#show_curiesObject



56
57
58
# File 'lib/hal_api/paged_collection.rb', line 56

def show_curies
  is_root_resource && !options[:no_curies]
end

#to_modelObject



23
24
25
# File 'lib/hal_api/paged_collection.rb', line 23

def to_model
  self
end

#urlObject

url to use for the self:href, can be a string or proc



69
70
71
# File 'lib/hal_api/paged_collection.rb', line 69

def url
  options[:url]
end