class Pixitar::Avatar

Attributes

assets_path[R]
gender[R]
image[R]
image_extention[R]

Public Class Methods

new(image_klass = Pixitar::Image.new, opts = {}) click to toggle source
# File lib/pixitar.rb, line 33
def initialize(image_klass = Pixitar::Image.new, opts = {})
  @image = image_klass
  @assets_path = opts.fetch(:assets_path, "images")
  @image_extention = opts.fetch(:image_extention, "png")
end

Public Instance Methods

face_parts() click to toggle source
# File lib/pixitar.rb, line 39
def face_parts
  [
    :background,
    :clothes,
    :face,
    :hair,
    :mouth,
    :eye
  ]
end
female_avatar() click to toggle source
# File lib/pixitar.rb, line 65
def female_avatar
  generate_avatar(:female)
end
genders() click to toggle source
# File lib/pixitar.rb, line 50
def genders
  [
    :male,
    :female
  ]
end
generate_avatar(gender = random_gender, filename = "avatar.png") click to toggle source
# File lib/pixitar.rb, line 69
def generate_avatar(gender = random_gender, filename = "avatar.png")
  @gender = gender
  raise InvalidGenderError unless genders.include?(gender)

  create_avatar_image(filename)
end
male_avatar() click to toggle source
# File lib/pixitar.rb, line 61
def male_avatar
  generate_avatar(:male)
end
random_gender() click to toggle source
# File lib/pixitar.rb, line 57
def random_gender
  genders.sample
end

Private Instance Methods

assets() click to toggle source
# File lib/pixitar.rb, line 92
def assets
  @assets ||= Dir.glob(path)
end
create_avatar_image(filename) click to toggle source
# File lib/pixitar.rb, line 78
def create_avatar_image(filename)
  face_parts.map do |face_part|
    asset = random_asset_for(face_part)
    image.compose(asset)
  end
  image.save(filename)
end
path() click to toggle source
# File lib/pixitar.rb, line 96
def path
  File.join(assets_path, gender.to_s, "*.#{image_extention}")
end
random_asset_for(face_part) click to toggle source
# File lib/pixitar.rb, line 86
def random_asset_for(face_part)
  parts = assets.grep(/#{face_part}/)
  raise MissingImageFilesError, "Missing #{face_part} image files for #{gender} avatars" if parts.empty?
  parts.sample
end