Class: ThumbnailHoverEffect::Image

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

Constant Summary collapse

IMAGE_NOT_FOUND =

image use if such is not specified

'/assets/no-image-found.jpg'
IMAGE_DEMO =
'/assets/demo/demo.jpg'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters = {}) ⇒ Image

validating input parameters and using defaults if necessary



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/thumbnail_hover_effect/image.rb', line 14

def initialize(parameters = {})
  # demo mode
  if parameters.empty?
    parameters = {
        url: IMAGE_DEMO,
        attributes:{likes:'224', dislikes: '2', url:'#'}
    }
  end

  @url = parameters.fetch(:url, IMAGE_NOT_FOUND)
  @attributes = parameters.fetch(:attributes, {})

  @url = IMAGE_NOT_FOUND unless File.extname(@url) =~/^(.png|.gif|.jpg|.jpeg|.bmp)$/
  @attributes = {} unless @attributes.is_a?(Hash)

end

Instance Attribute Details

#attributesObject

class attributes



10
11
12
# File 'lib/thumbnail_hover_effect/image.rb', line 10

def attributes
  @attributes
end

#urlObject

class attributes



10
11
12
# File 'lib/thumbnail_hover_effect/image.rb', line 10

def url
  @url
end

Instance Method Details

#get_template(effect_number) ⇒ Object

returns the html template



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/thumbnail_hover_effect/image.rb', line 53

def get_template(effect_number)
  "
  <div class=\"view-image effect0#{1 unless effect_number}\">
    <div>
    <span class=\"icon-emo-happy\">##title##</span>
    <span class=\"icon-emo-happy\">##description##</span>
    <a href=\"##url##\"><span aria-hidden=\"true\" class=\"icon-emo-happy\"></span></a>
    </div>
    <div class=\"slice\" style=\"background-image: url(##url##);\">
      <span class=\"overlay\"></span>
      <div class=\"slice\" style=\"background-image: url(##url##);\">
        <span class=\"overlay\"></span>
        <div class=\"slice\" style=\"background-image: url(##url##);\">
          <span class=\"overlay\"></span>
          <div class=\"slice\" style=\"background-image: url(##url##);\">
            <span class=\"overlay\"></span>
            <div class=\"slice\" style=\"background-image: url(##url##);\">
              <span class=\"overlay\"></span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  "
end

#render(parameters = {}) ⇒ Object

rendering image with thumbnail effect applied



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/thumbnail_hover_effect/image.rb', line 37

def render(parameters = {})

  has_thumbnail = parameters.fetch(:has_thumbnail, true)
  effect_number = parameters.fetch(:effect_number, false)
  thumbnail_template = self.get_template(effect_number)


  if has_thumbnail
    @attributes.map { |key, value| thumbnail_template["###{key}##"] &&= value }
    thumbnail_template.gsub!('##url##', @url).html_safe
  else
    self.to_s.html_safe
  end
end

#to_sObject

rendering image without thumbnail effect



32
33
34
# File 'lib/thumbnail_hover_effect/image.rb', line 32

def to_s
  "<img src=#{@url} #{@attributes.map{|key,value| "data-#{key}=#{value}" }.join(' ')}/>"
end