class Moodwall::Wallpaper

Attributes

mood_id[R]
path[R]
weight[R]

Public Class Methods

list() click to toggle source
# File lib/moodwall/wallpaper.rb, line 30
def list
  all.map(&:path)
end
new(options) click to toggle source
# File lib/moodwall/wallpaper.rb, line 11
def initialize(options)
  @path    = options.fetch(:path) { raise(MissingPathError, "Wallpaper path is required") }
  @mood_id = options.fetch(:mood_id) { Moodwall::Mood.current }
  @weight  = options.fetch(:weight, 0)
end
sample() click to toggle source
# File lib/moodwall/wallpaper.rb, line 18
def sample
  record = within_current_mood.shuffle.sort.first
  raise(WallpaperNotFoundError, "Can't find wallpaper.") if record.nil?
  record.increment_weight!
  record
end
within_current_mood() click to toggle source
# File lib/moodwall/wallpaper.rb, line 25
def within_current_mood
  mood = Moodwall::Mood.current
  mood.nil? ? all : all.select { |w| w.mood_id == mood.id }
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/moodwall/wallpaper.rb, line 40
def <=>(other)
  other_weight = other.weight
  if other_weight != weight
    -(other_weight <=> weight)
  else
    [-1, 0, 1].sample
  end
end
increment_weight!() click to toggle source
# File lib/moodwall/wallpaper.rb, line 49
def increment_weight!
  @weight += 1
  save
end
save() click to toggle source
Calls superclass method Moodwall::Record#save
# File lib/moodwall/wallpaper.rb, line 35
def save
  check_file
  super
end

Private Instance Methods

check_file() click to toggle source
# File lib/moodwall/wallpaper.rb, line 56
def check_file
  raise(MissingFileError, "Can't find the `#{ path }`") unless File.exist?(path)
end