Class: Redd::Models::MoreComments
- Inherits:
-
BasicModel
- Object
- BasicModel
- Redd::Models::MoreComments
- Defined in:
- lib/redd/models/more_comments.rb
Overview
An object that represents a bunch of comments that need to be expanded.
Instance Attribute Summary
Attributes inherited from BasicModel
Instance Method Summary collapse
-
#expand(link:, sort: nil) ⇒ Listing<Comment, MoreComments>
Expand the object’s children into a listing of Comments and MoreComments.
-
#recursive_expand(link:, sort: nil, lookup: {}, depth: 10) ⇒ Array<Comment, MoreComments>
Keep expanding until all top-level MoreComments are converted to comments.
-
#to_ary ⇒ Array<String>
An array representation of self.
Methods inherited from BasicModel
from_id, #initialize, #inspect, #method_missing, #respond_to_missing?, #to_h
Constructor Details
This class inherits a constructor from Redd::Models::BasicModel
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Redd::Models::BasicModel
Instance Method Details
#expand(link:, sort: nil) ⇒ Listing<Comment, MoreComments>
Expand the object’s children into a listing of Comments and MoreComments.
13 14 15 16 17 18 |
# File 'lib/redd/models/more_comments.rb', line 13 def (link:, sort: nil) params = { link_id: link.name, children: get_attribute(:children).join(',') } params[:sort] = sort if sort params[:sort] = link.sort_order if link.sort_order @client.model(:get, '/api/morechildren', params) end |
#recursive_expand(link:, sort: nil, lookup: {}, depth: 10) ⇒ Array<Comment, MoreComments>
Keep expanding until all top-level MoreComments are converted to comments.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/redd/models/more_comments.rb', line 26 def (link:, sort: nil, lookup: {}, depth: 10) return [self] if depth <= 0 (link: link, sort: sort).each_with_object([]) do |thing, coll| target = (lookup.key?(thing.parent_id) ? lookup[thing.parent_id].replies.children : coll) if thing.is_a?(Comment) # Add the comment to a lookup hash. lookup[thing.name] = thing # If the parent is not in the lookup hash, add it to the root listing. target.push(thing) elsif thing.is_a?(MoreComments) && thing.count > 0 # Get an array of expanded comments from the thing. ary = thing.(link: link, sort: sort, lookup: lookup, depth: depth - 1) target.concat(ary) end end end |
#to_ary ⇒ Array<String>
Returns an array representation of self.
46 47 48 |
# File 'lib/redd/models/more_comments.rb', line 46 def to_ary get_attribute(:children) end |