Class: Placeholder
- Inherits:
-
Object
- Object
- Placeholder
- Defined in:
- lib/placeholder.rb
Instance Attribute Summary collapse
-
#bg_color ⇒ Object
Returns the value of attribute bg_color.
-
#class ⇒ Object
Returns the value of attribute class.
-
#fg_color ⇒ Object
Returns the value of attribute fg_color.
-
#height ⇒ Object
Returns the value of attribute height.
-
#id ⇒ Object
Returns the value of attribute id.
-
#text ⇒ Object
Returns the value of attribute text.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #color_str ⇒ Object
- #css_stuff ⇒ Object
-
#initialize(size, options = {}) ⇒ Placeholder
constructor
A new instance of Placeholder.
- #measure(size) ⇒ Object
- #size_str ⇒ Object
- #text_str ⇒ Object
- #to_s ⇒ Object
- #url ⇒ Object
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, = {}) measure(size) .each{|(k,v)| instance_variable_set("@#{k}", v)} end |
Instance Attribute Details
#bg_color ⇒ Object
Returns the value of attribute bg_color.
3 4 5 |
# File 'lib/placeholder.rb', line 3 def bg_color @bg_color end |
#class ⇒ Object
Returns the value of attribute class.
3 4 5 |
# File 'lib/placeholder.rb', line 3 def class @class end |
#fg_color ⇒ Object
Returns the value of attribute fg_color.
3 4 5 |
# File 'lib/placeholder.rb', line 3 def fg_color @fg_color end |
#height ⇒ Object
Returns the value of attribute height.
3 4 5 |
# File 'lib/placeholder.rb', line 3 def height @height end |
#id ⇒ Object
Returns the value of attribute id.
3 4 5 |
# File 'lib/placeholder.rb', line 3 def id @id end |
#text ⇒ Object
Returns the value of attribute text.
3 4 5 |
# File 'lib/placeholder.rb', line 3 def text @text end |
#width ⇒ Object
Returns the value of attribute width.
3 4 5 |
# File 'lib/placeholder.rb', line 3 def width @width end |
Instance Method Details
#color_str ⇒ Object
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_stuff ⇒ Object
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_str ⇒ Object
27 28 29 |
# File 'lib/placeholder.rb', line 27 def size_str "#{@width}x#{@height}" end |
#text_str ⇒ Object
31 32 33 34 |
# File 'lib/placeholder.rb', line 31 def text_str return "" if @text == nil "&text=" + @text.gsub(" ", "+") end |
#to_s ⇒ Object
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 |
#url ⇒ Object
23 24 25 |
# File 'lib/placeholder.rb', line 23 def url "http://placehold.it/#{size_str}#{color_str}#{text_str}" end |