Class: HalApi::PagedCollection
- Inherits:
-
Object
- Object
- HalApi::PagedCollection
- Extended by:
- ActiveModel::Naming, Forwardable
- Defined in:
- lib/hal_api/paged_collection.rb
Instance Attribute Summary collapse
-
#facets ⇒ Object
Returns the value of attribute facets.
-
#items ⇒ Object
Returns the value of attribute items.
-
#options ⇒ Object
Returns the value of attribute options.
-
#request ⇒ Object
Returns the value of attribute request.
Class Method Summary collapse
Instance Method Summary collapse
- #cache_key ⇒ Object
- #count ⇒ Object
-
#initialize(items, request = nil, options = nil) ⇒ PagedCollection
constructor
A new instance of PagedCollection.
- #is_root_resource ⇒ Object
- #item_class ⇒ Object
- #item_decorator ⇒ Object
-
#parent ⇒ Object
If this is an embedded collection, the parent will be set here for use in urls.
- #persisted? ⇒ Boolean
- #request_stub ⇒ Object
- #show_curies ⇒ Object
- #to_model ⇒ Object
-
#url ⇒ Object
url to use for the self:href, can be a string or proc.
Constructor Details
#initialize(items, request = nil, options = nil) ⇒ PagedCollection
Returns a new instance of PagedCollection.
30 31 32 33 34 35 |
# File 'lib/hal_api/paged_collection.rb', line 30 def initialize(items, request=nil, =nil) self.items = items self.request = request || request_stub self. = || {} self.[:is_root_resource] = true unless (self.[:is_root_resource] == false) end |
Instance Attribute Details
#facets ⇒ Object
Returns the value of attribute facets.
9 10 11 |
# File 'lib/hal_api/paged_collection.rb', line 9 def facets @facets end |
#items ⇒ Object
Returns the value of attribute items.
9 10 11 |
# File 'lib/hal_api/paged_collection.rb', line 9 def items @items end |
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/hal_api/paged_collection.rb', line 9 def @options end |
#request ⇒ Object
Returns the value of attribute request.
9 10 11 |
# File 'lib/hal_api/paged_collection.rb', line 9 def request @request end |
Class Method Details
.representer ⇒ Object
18 19 20 |
# File 'lib/hal_api/paged_collection.rb', line 18 def self.representer representer_class || HalApi::PagedCollectionRepresenter end |
Instance Method Details
#cache_key ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/hal_api/paged_collection.rb', line 37 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.(key_components) end |
#count ⇒ Object
81 82 83 |
# File 'lib/hal_api/paged_collection.rb', line 81 def count items.length end |
#is_root_resource ⇒ Object
51 52 53 |
# File 'lib/hal_api/paged_collection.rb', line 51 def is_root_resource !!self.[:is_root_resource] end |
#item_class ⇒ Object
59 60 61 |
# File 'lib/hal_api/paged_collection.rb', line 59 def item_class [:item_class] || self.items.first.try(:item_class) || self.items.first.class end |
#item_decorator ⇒ Object
63 64 65 |
# File 'lib/hal_api/paged_collection.rb', line 63 def item_decorator [:item_decorator] || "Api::#{item_class.name}Representer".constantize end |
#parent ⇒ Object
If this is an embedded collection, the parent will be set here for use in urls
73 74 75 76 77 78 79 |
# File 'lib/hal_api/paged_collection.rb', line 73 def parent rep = [: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
26 27 28 |
# File 'lib/hal_api/paged_collection.rb', line 26 def persisted? false end |
#request_stub ⇒ Object
47 48 49 |
# File 'lib/hal_api/paged_collection.rb', line 47 def request_stub OpenStruct.new(params: {}) end |
#show_curies ⇒ Object
55 56 57 |
# File 'lib/hal_api/paged_collection.rb', line 55 def show_curies is_root_resource && ![:no_curies] end |
#to_model ⇒ Object
22 23 24 |
# File 'lib/hal_api/paged_collection.rb', line 22 def to_model self end |
#url ⇒ Object
url to use for the self:href, can be a string or proc
68 69 70 |
# File 'lib/hal_api/paged_collection.rb', line 68 def url [:url] end |