module ThumbnailHoverEffect # class Image # image use if such is not specified IMAGE_NOT_FOUND = '/images/no-image-found.jpg' # class attributes attr_accessor :url, # image source url :attributes # data attributes # validating input parameters and using defaults if necessary def initialize(parameters = {}) @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 # rendering image without thumbnail effect def to_s "" end # rendering image with thumbnail effect applied 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 # returns the html template def get_template(effect_number) "
" end end end