Class: ContentGenerator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/Rubbit/Rubbit_Objects.rb

Overview

Rubbit Object

Enumerable object that allows a user to iterate over data sets

Instance Method Summary collapse

Constructor Details

#initialize(source, limit = 100, after = '') ⇒ ContentGenerator

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.



364
365
366
367
368
369
370
371
372
373
374
375
376
# 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

Instance Method Details

#[](index) ⇒ Object

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.



431
432
433
# File 'lib/Rubbit/Rubbit_Objects.rb', line 431

def [](index)
  return @data[index]
end

#eachObject

Description

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



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# 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

#lengthObject

Description

Returns the length of data already loaded into the ContentGenerator.



491
492
493
# File 'lib/Rubbit/Rubbit_Objects.rb', line 491

def length
  return @data.length
end

#nextObject

Description

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



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# 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

#prevObject

Description

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



466
467
468
469
470
471
472
473
# 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) ⇒ Object

Description

Resets the index of the current entry in ContentGenerator

Attributes

  • index - The index that the ContentGenerator will be set to. Defaults to 0.



483
484
485
# File 'lib/Rubbit/Rubbit_Objects.rb', line 483

def reset_generator(index=0)
  @index=index
end