Class: Restfully::Collection
- Includes:
- Enumerable
- Defined in:
- lib/restfully/collection.rb
Overview
This includes the Enumerable
module, but does not have all the methods that you could expect from an Array
. Remember that this class inherits from a Restfully::Resource
and as such, the #[]
method gives access to Resource properties, and not to an item in the collection. If you want to operate on the array of items, you MUST call #to_a
first (or #items
) on the Restfully::Collection.
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Attributes inherited from Resource
#executed_requests, #links, #properties, #session, #title, #uri
Instance Method Summary collapse
-
#[](property) ⇒ Object
if property is a Symbol, it tries to find the corresponding item whose uid.to_sym is matching the property else, returns the result of calling
[]
on its superclass. -
#each(*args, &block) ⇒ Object
Iterates over the collection of items.
-
#initialize(uri, session, options = {}) ⇒ Collection
constructor
See Resource#new.
- #inspect ⇒ Object
-
#length ⇒ Object
(also: #size)
Returns the current number of items (not the total number) in the collection.
- #populate_object(key, value) ⇒ Object
- #pretty_print(pp) ⇒ Object
-
#reset ⇒ Object
See Resource#reset.
Methods inherited from Resource
#delete, #http_methods, #load, #method_missing, #reload, #respond_to?, #stale!, #stale?, #submit, #uri_for
Constructor Details
#initialize(uri, session, options = {}) ⇒ Collection
See Resource#new
15 16 17 |
# File 'lib/restfully/collection.rb', line 15 def initialize(uri, session, = {}) super(uri, session, ) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Restfully::Resource
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
12 13 14 |
# File 'lib/restfully/collection.rb', line 12 def items @items end |
Instance Method Details
#[](property) ⇒ Object
if property is a Symbol, it tries to find the corresponding item whose uid.to_sym is matching the property else, returns the result of calling []
on its superclass.
46 47 48 49 50 51 52 53 54 |
# File 'lib/restfully/collection.rb', line 46 def [](property) if property.kind_of?(Symbol) item = find{|i| i['uid'] == property.to_s} || find{|i| i['uid'] == property.to_s.to_i} || Resource.new([uri, property].join("/"), session).load rescue nil else super(property) end end |
#each(*args, &block) ⇒ Object
Iterates over the collection of items
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/restfully/collection.rb', line 28 def each(*args, &block) @items.each_index{ |i| block.call(@items[i]) if i == @items.length-1 && @items.length+self["offset"] < self["total"] # load next page = executed_requests['GET']['options'][:query] || {} [:offset] = self["offset"]+@items.length [:limit] ||= 200 next_page = Collection.new(uri, session).load(:query => ) rescue nil if next_page && next_page['offset'] != self["offset"] @items.push(*next_page.to_a) end end } end |
#inspect ⇒ Object
88 89 90 |
# File 'lib/restfully/collection.rb', line 88 def inspect @items.inspect end |
#length ⇒ Object Also known as: size
Returns the current number of items (not the total number) in the collection.
58 59 60 |
# File 'lib/restfully/collection.rb', line 58 def length @items.length end |
#populate_object(key, value) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/restfully/collection.rb', line 62 def populate_object(key, value) case key when "links" value.each{|link| define_link(Link.new(link))} when "items" value.each do |item| self_link = (item['links'] || []). map{|link| Link.new(link)}.detect{|link| link.self?} if self_link && self_link.valid? @items.push Resource.new(uri_for(self_link.href), session).load(:body => item) else session.logger.warn "Resource #{key} does not have a 'self' link. skipped." end end else case value when Hash @properties.store(key, SpecialHash.new.replace(value)) unless @links.has_key?(key) when Array @properties.store(key, SpecialArray.new(value)) else @properties.store(key, value) end end end |
#pretty_print(pp) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/restfully/collection.rb', line 93 def pretty_print(pp) super(pp) do |inner_pp| if @items.length > 0 inner_pp.breakable inner_pp.text "ITEMS (#{self["offset"]}..#{self["offset"]+@items.length})/#{self["total"]}" inner_pp.nest 2 do @items.each_with_index do |item, i| inner_pp.breakable inner_pp.text "#<#{item.class}:0x#{item.object_id.to_s(16)} uid=#{item['uid'].inspect}>" inner_pp.text "," if i < @items.length-1 end end end end end |