Class: DisqusRails::Collection
- Inherits:
-
Object
- Object
- DisqusRails::Collection
- Includes:
- Enumerable
- Defined in:
- lib/disqus_rails/collection.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
Returns the value of attribute items.
Class Method Summary collapse
Instance Method Summary collapse
- #cursor_next ⇒ Object
- #cursor_next! ⇒ Object
- #cursor_prev ⇒ Object
- #cursor_prev! ⇒ Object
- #each(&block) ⇒ Object
- #has_cursor_next? ⇒ Boolean
- #has_cursor_prev? ⇒ Boolean
-
#initialize(api_class_name, method = :list, attributes = {}) ⇒ Collection
constructor
A new instance of Collection.
Constructor Details
#initialize(api_class_name, method = :list, attributes = {}) ⇒ Collection
Returns a new instance of Collection.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/disqus_rails/collection.rb', line 21 def initialize(api_class_name, method = :list, attributes = {}) @api_class_name = api_class_name @method = method @attributes = attributes api_class = Api.const_get api_class_name results = api_class.send method, attributes @cursor = results[:cursor].symbolize_keys @items = [] results[:response].each do |item| @items << DisqusRails.const_get(self.class.name.singularize.split(/::/).last).new(item) end end |
Instance Attribute Details
#items ⇒ Object
Returns the value of attribute items.
6 7 8 |
# File 'lib/disqus_rails/collection.rb', line 6 def items @items end |
Class Method Details
.inherited(subclass) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/disqus_rails/collection.rb', line 8 def self.inherited(subclass) subclass.class_exec do define_method "find_all_#{subclass.name.split(/::/).last.downcase}!".to_sym do while has_cursor_next? previous_items = @items cursor_next! @items = previous_items + @items end self end end end |
Instance Method Details
#cursor_next ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/disqus_rails/collection.rb', line 54 def cursor_next if has_cursor_next? @attributes[:cursor] = @cursor[:next] self.class.new(@api_class_name, @method, @attributes) end self end |
#cursor_next! ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/disqus_rails/collection.rb', line 62 def cursor_next! if has_cursor_next? @attributes[:cursor] = @cursor[:next] initialize(@api_class_name, @method, @attributes) end self end |
#cursor_prev ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/disqus_rails/collection.rb', line 70 def cursor_prev if has_cursor_prev? @attributes[:cursor] = @cursor[:prev] self.class.new(@api_class_name, @method, @attributes) end self end |
#cursor_prev! ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/disqus_rails/collection.rb', line 78 def cursor_prev! if has_cursor_prev? @attributes[:cursor] = @cursor[:prev] initialize(@api_class_name, @method, @attributes) end self end |
#each(&block) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/disqus_rails/collection.rb', line 36 def each(&block) @items.each do |item| if block_given? block.call item else yield item end end end |
#has_cursor_next? ⇒ Boolean
46 47 48 |
# File 'lib/disqus_rails/collection.rb', line 46 def has_cursor_next? @cursor[:hasNext] end |
#has_cursor_prev? ⇒ Boolean
50 51 52 |
# File 'lib/disqus_rails/collection.rb', line 50 def has_cursor_prev? @cursor[:hasPrev] end |