class LeMeme::MemeLib

Utility for easily generating memes based off templates

Attributes

memes[R]

Public Class Methods

new(*dirs) click to toggle source

Creates a meme library

@param [String] *dirs Directory glob patterns to meme templates @return [MemeLib]

# File lib/le_meme/meme_lib.rb, line 9
def initialize(*dirs)
  @memes = {}
  dirs.each(&method(:load_directory!))
end
new_with_default_memes() click to toggle source

Creates a meme library, preloaded with the included templates

@return [MemeLib]

# File lib/le_meme/meme_lib.rb, line 17
def self.new_with_default_memes
  path = File.join(File.dirname(File.expand_path(__FILE__)), '..', '..', 'memes', '*')
  new(path)
end

Public Instance Methods

load_directory!(dir) click to toggle source

Loads a directory into the MemeLib, for template consumption Clobbers any existing templates

@param [String] dir Directory glob pattern to meme templates @return [Hash] Hash of all templates and their filepaths

# File lib/le_meme/meme_lib.rb, line 27
def load_directory!(dir)
  paths = Dir.glob(dir).grep LeMeme::IMAGE_EXTENSIONS
  @memes.merge!(paths.reduce({}) do |images, path|
    path = File.expand_path(path)
    name = path.split.last.sub(LeMeme::IMAGE_EXTENSIONS, '').to_s
    images.merge(name => path)
  end)
end
meme(template: nil, top: nil, bottom: nil, watermark: nil) click to toggle source

Create a meme from a template

@param [String] template: nil The template to use. Omit for random template @param [String] top: nil @param [String] bottom: nil @param [String] watermark: nil @return [LeMeme::Meme]

# File lib/le_meme/meme_lib.rb, line 43
def meme(template: nil, top: nil, bottom: nil, watermark: nil)
  path = template.nil? ? @memes.values.sample : @memes[template]

  Meme.new(path, top: top, bottom: bottom, watermark: watermark)
end