lib/fake_picture.rb in fake_picture-0.2.0 vs lib/fake_picture.rb in fake_picture-0.2.2
- old
+ new
@@ -1,46 +1,34 @@
# frozen_string_literal: true
module FakePicture
class Base
- PACK_PATH = '/pack'
-
- def self.random_file(path, file: false)
+ def self.random_file(path)
path_to_file = Dir[path].select { |f| File.file?(f) }.sample
return false if path_to_file.nil?
- file ? File.new(path_to_file) : path_to_file
+ path_to_file
end
# def download(query)
#TODO: make downloading pictures to gem
# end
- def self.define_methods(path_to_directory, *method_names)
- method_names.each do |name|
- define_singleton_method :"#{name}" do
- self.superclass.check_pack_directory_readiness(path_to_directory + PACK_PATH)
+ def self.initialize_fake_picture_methods(*method_names)
+ path_to_pack = "#{__dir__}/fake_picture/#{self.name.split('::').last.downcase}/pack"
- self.superclass.random_file("#{path_to_directory + PACK_PATH}/#{name}-*")
- end
+ self.superclass.check_pack_directory_readiness(path_to_pack)
- define_singleton_method :"#{name}_file" do
- self.superclass.check_pack_directory_readiness(path_to_directory + PACK_PATH)
+ [*method_names, :random].each do |name|
+ selector = name == :random ? '/*' : "/#{name}-*"
- self.superclass.random_file("#{path_to_directory + PACK_PATH}/#{name}-*", file: true)
+ define_singleton_method :"#{name}" do
+ self.superclass.random_file("#{path_to_pack + selector}")
end
- define_singleton_method :random do
- self.superclass.check_pack_directory_readiness(path_to_directory + PACK_PATH)
-
- self.superclass.random_file("#{path_to_directory + self::PACK_PATH}/*")
- end
-
- define_singleton_method :random_file do
- self.superclass.check_pack_directory_readiness(path_to_directory + PACK_PATH)
-
- self.superclass.random_file("#{path_to_directory + self::PACK_PATH}/*", file: true)
+ define_singleton_method :"#{name}_file" do
+ File.new(self.send(name))
end
end
end
private