Class: LeMeme::MemeLib
- Inherits:
-
Object
- Object
- LeMeme::MemeLib
- Defined in:
- lib/le_meme/meme_lib.rb
Overview
Utility for easily generating memes based off templates
Instance Attribute Summary collapse
-
#memes ⇒ Object
readonly
Returns the value of attribute memes.
Class Method Summary collapse
-
.new_with_default_memes ⇒ MemeLib
Creates a meme library, preloaded with the included templates.
Instance Method Summary collapse
-
#initialize(*dirs) ⇒ MemeLib
constructor
Creates a meme library.
-
#load_directory!(dir) ⇒ Hash
Loads a directory into the MemeLib, for template consumption Clobbers any existing templates.
-
#meme(template: nil, top: nil, bottom: nil, watermark: nil) ⇒ LeMeme::Meme
Create a meme from a template.
Constructor Details
#initialize(*dirs) ⇒ MemeLib
Creates a meme library
9 10 11 12 |
# File 'lib/le_meme/meme_lib.rb', line 9 def initialize(*dirs) @memes = {} dirs.each(&method(:load_directory!)) end |
Instance Attribute Details
#memes ⇒ Object (readonly)
Returns the value of attribute memes.
4 5 6 |
# File 'lib/le_meme/meme_lib.rb', line 4 def memes @memes end |
Class Method Details
.new_with_default_memes ⇒ MemeLib
Creates a meme library, preloaded with the included templates
17 18 19 20 |
# File 'lib/le_meme/meme_lib.rb', line 17 def self.new_with_default_memes path = File.join(File.dirname(File.(__FILE__)), '..', '..', 'memes', '*') new(path) end |
Instance Method Details
#load_directory!(dir) ⇒ Hash
Loads a directory into the MemeLib, for template consumption Clobbers any existing templates
27 28 29 30 31 32 33 34 |
# 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.(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) ⇒ LeMeme::Meme
Create a meme from a template
43 44 45 46 47 |
# 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 |