class ContentGenerator

Rubbit Object

Enumerable object that allows a user to iterate over data sets

Public Class Methods

new(source,limit=100,after='') click to toggle source

Description

Initializes a ContentGenerator Object

Attributes

  • source - Required. Link to the .json page for the content

  • limit - Maximum number of entries the ContentGenerator will load. nil for no limit.

  • after - ID of entry to load content after.

# File lib/Rubbit/Rubbit_Objects.rb, line 364
def initialize(source,limit=100,after='')
        @source = source
        @limit = limit
        @count = 0
        @data = []
        @after = after
        @index = 0
        if(@source.include?('?'))
                @limit_param = '&limit='
        else
                @limit_param = '?limit='
        end
end

Public Instance Methods

[](index) click to toggle source

Description

Returns an object in the ContentGenerator at a particular index. Only works after it has been iterated through and loaded.

Attributes

  • index - The index of the object in the ContentGenerator that will be returned.

# File lib/Rubbit/Rubbit_Objects.rb, line 431
def [](index)
        return @data[index]
end
each() { |data| ... } click to toggle source

Description

Iterates over the content from a specific source until there is no content left or until a limit is reached

# File lib/Rubbit/Rubbit_Objects.rb, line 382
def each
        if(@data.length==0)
                if(@limit!=nil)
                        if(@limit-@count>0)
                                listing = Rubbit_Object_Builder.instance.build_listing(@source+@limit_param+[@limit-@count,100].min.to_s+"&after="+@after+"&count="+@count.to_s)
                                @after = listing.after
                                @data += listing.children
                                @count += listing.children.length
                        end
                else
                        listing = Rubbit_Object_Builder.instance.build_listing(@source+@limit_param+100.to_s+"&after="+@after+"&count="+@count.to_s)
                        @after = listing.after
                        @data += listing.children
                        @count+= listing.children.length
                end
        end
        
        while(@index<@data.length)
                yield @data[@index]
                @index+=1
                if(@index==@data.length)
                        if(@after==nil)
                                break
                        end
                        if(@limit!=nil)
                                if(@limit-@count>0)
                                        listing = Rubbit_Object_Builder.instance.build_listing(@source+@limit_param+[@limit-@count,100].min.to_s+"&after="+@after+"&count="+@count.to_s)
                                        @after = listing.after
                                        @data += listing.children
                                        @count += listing.children.length
                                end
                        else
                                listing = Rubbit_Object_Builder.instance.build_listing(@source+@limit_param+100.to_s+"&after="+@after+"&count="+@count.to_s)
                                @after = listing.after
                                @data += listing.children
                                @count += listing.children.length
                        end
                end
        end
end
length() click to toggle source

Description

Returns the length of data already loaded into the ContentGenerator.

# File lib/Rubbit/Rubbit_Objects.rb, line 491
def length
        return @data.length
end
next() click to toggle source

Description

Returns the next object in the ContentGenerator. If none is loaded, it will try to load more, until a limit is reached.

# File lib/Rubbit/Rubbit_Objects.rb, line 439
def next
        if(@index>=@data.length)
                if(@limit!=nil)
                        if(@limit-@count>0)
                                listing = Rubbit_Object_Builder.instance.build_listing(@source+@limit_param+[@limit-@count,100].min.to_s+"&after="+@after+"&count="+@count.to_s)
                                @after = listing.after
                                @data += listing.children
                                @count += listing.children.length
                        end
                else
                        listing = Rubbit_Object_Builder.instance.build_listing(@source+@limit_param+100.to_s+"&after="+@after+"&count="+@count.to_s)
                        @after = listing.after
                        @data += listing.children
                        @count += listing.children.length
                end
        end
        to_return = @data[@index]
        if(to_return!=nil)
                @index+=1
        end
        return to_return
end
prev() click to toggle source

Description

Returns the previous object in the ContentGenerator. If the index is at 0, nil is returned

# File lib/Rubbit/Rubbit_Objects.rb, line 466
def prev
        if(@index>1)
                @index-=1
                return @data[@index-1]
        else
                return nil
        end
end
reset_generator(index=0) click to toggle source

Description

Resets the index of the current entry in ContentGenerator

Attributes

# File lib/Rubbit/Rubbit_Objects.rb, line 483
def reset_generator(index=0)
        @index=index
end