class Rrant::Local

Public: Contains local storage handling methods, operates on top of store object.

Public Class Methods

new(store) click to toggle source
# File lib/rrant/local.rb, line 10
def initialize(store)
  raise Error::InvalidStore unless store.is_a?(Store)

  @store = store
  @unseen = false
end

Public Instance Methods

random() click to toggle source

Public: Returns random rant from the store. Returns placeholder if there are no available rants. Updates rant's 'viewed_at' parameter.

# File lib/rrant/local.rb, line 19
def random
  return placeholder if @store.empty?
  rant = pick_random
  return placeholder unless rant

  rant.tap { |r| @store.touch(r['id']) }
end
unseen(set) click to toggle source

Public: Sets 'unseen' instance variable and returns self. With 'unseen' set to true, we fetch only rants with 'viewed_at' set to nil.

# File lib/rrant/local.rb, line 29
def unseen(set)
  @unseen = set
  self
end

Private Instance Methods

pick_random() click to toggle source

Private: Grabs random rant from the store according to 'unseen' instance variable.

# File lib/rrant/local.rb, line 38
def pick_random
  return @store.entities.sample unless @unseen

  @store.entities.reject { |rant| !rant['viewed_at'].nil? }.sample
end
placeholder() click to toggle source
# File lib/rrant/local.rb, line 44
def placeholder
  { 'text' => 'No rants available :/',
    'image' => "#{files_path}/devrant.png" }
end