Class: FakePicture::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/fake_picture.rb

Direct Known Subclasses

Avatar, Blog, Company, People

Class Method Summary collapse

Class Method Details

.initialize_methods(*method_names, new_random_method_name: nil) ⇒ Object

end



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fake_picture.rb', line 18

def self.initialize_methods(*method_names, new_random_method_name: nil) # from line 17 to 24 make code better
  path_to_pack = "#{__dir__}/fake_picture/#{self.name.split('::').last.downcase}/pack"

  self.superclass.check_pack_directory_readiness(path_to_pack)

  [*method_names, :random].each do |name|
    selector = name == :random ? '/*' : "/#{name}-*"
    name = new_random_method_name if name == :random && !new_random_method_name.nil?

    define_singleton_method :"#{name}" do
      self.superclass.random_file("#{path_to_pack +  selector}")
    end
  end

  define_singleton_method :file do |method_name|
    File.new(self.send(method_name))
  end

  define_singleton_method :base64 do |method_name|
    Base64.encode64(File.read(self.send(method_name)))
  end
end

.random_file(path) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fake_picture.rb', line 6

def self.random_file(path)
  path_to_file = Dir[path].select { |f| File.file?(f) }.sample

  return false if path_to_file.nil?

  path_to_file
end