Class: Placeholder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size, options = {}) ⇒ Placeholder

Returns a new instance of Placeholder.



5
6
7
8
# File 'lib/placeholder.rb', line 5

def initialize(size, options = {})
  measure(size)
  options.each{|(k,v)| instance_variable_set("@#{k}", v)}
end

Instance Attribute Details

#bg_colorObject

Returns the value of attribute bg_color.



3
4
5
# File 'lib/placeholder.rb', line 3

def bg_color
  @bg_color
end

#classObject

Returns the value of attribute class.



3
4
5
# File 'lib/placeholder.rb', line 3

def class
  @class
end

#fg_colorObject

Returns the value of attribute fg_color.



3
4
5
# File 'lib/placeholder.rb', line 3

def fg_color
  @fg_color
end

#heightObject

Returns the value of attribute height.



3
4
5
# File 'lib/placeholder.rb', line 3

def height
  @height
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/placeholder.rb', line 3

def id
  @id
end

#textObject

Returns the value of attribute text.



3
4
5
# File 'lib/placeholder.rb', line 3

def text
  @text
end

#widthObject

Returns the value of attribute width.



3
4
5
# File 'lib/placeholder.rb', line 3

def width
  @width
end

Instance Method Details

#color_strObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/placeholder.rb', line 36

def color_str
  if @bg_color.nil? && @fg_color != nil
    @text = "Set :bg_color when using :fg_color"
  end

  str = ""
  str += "/#{@bg_color}" unless @bg_color.nil?
  str += "/#{@fg_color}" unless @fg_color.nil?
  str
end

#css_stuffObject



47
48
49
50
51
52
# File 'lib/placeholder.rb', line 47

def css_stuff
  str = ""
  str += "class=\"#{@class}\"" unless @class.nil?
  str += "id=\"#{@id}\"" unless @id.nil?
  str
end

#measure(size) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/placeholder.rb', line 10

def measure(size)
  if size =~ /\d+x\d+/
    @width, @height = size.split("x").map{|s| s.to_i}
  else
    @width, @height = size.to_i, size.to_i
  end
end

#size_strObject



27
28
29
# File 'lib/placeholder.rb', line 27

def size_str
  "#{@width}x#{@height}"
end

#text_strObject



31
32
33
34
# File 'lib/placeholder.rb', line 31

def text_str
  return "" if @text == nil
  "&text=" + @text.gsub(" ", "+")
end

#to_sObject



18
19
20
21
# File 'lib/placeholder.rb', line 18

def to_s
  str = "<img src=\"#{url}\" alt=\"placeholder\" #{css_stuff} />"
  str.html_safe rescue str #Rails 3 sanitizes our little string.
end

#urlObject



23
24
25
# File 'lib/placeholder.rb', line 23

def url
  "http://placehold.it/#{size_str}#{color_str}#{text_str}"
end