Class: AvatarOMatic::Generator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Generator

Returns a new instance of Generator.



19
20
21
22
23
24
25
26
27
# File 'lib/avatar_o_matic/generator.rb', line 19

def initialize(opts={})
  super()

  opts.keys.select {|k| (Config.properties + [:size]).include? k }.each do |opt|
    self.send :"#{opt}=", opts[opt]
  end

  @size ||= 400
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



17
18
19
# File 'lib/avatar_o_matic/generator.rb', line 17

def image
  @image
end

#sizeObject

Returns the value of attribute size.



16
17
18
# File 'lib/avatar_o_matic/generator.rb', line 16

def size
  @size
end

Instance Method Details

#generate!Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/avatar_o_matic/generator.rb', line 64

def generate!
  @image = MiniMagick::Image.open path_for :background

  Config.properties.select {|p| p != :background }.each do |prop|
    overlay = MiniMagick::Image.open path_for prop
    @image = @image.composite(overlay) do |c|
      c.compose 'Over'
    end
  end

  @image.resize size if image.width != size

  self
end

#save(path) ⇒ Object

Raises:



79
80
81
82
83
# File 'lib/avatar_o_matic/generator.rb', line 79

def save(path)
  raise NoImageYetError.new if @image.nil?
  @image.write path
  path
end

#typeObject



36
37
38
# File 'lib/avatar_o_matic/generator.rb', line 36

def type
  @type ||= Config.types.sample
end

#type=(type) ⇒ Object



29
30
31
32
33
34
# File 'lib/avatar_o_matic/generator.rb', line 29

def type=(type)
  unless Config.types.include? type.to_sym
    raise InvalidPropertyError.new("#{type} is not a recognised type")
  end
  @type = type
end