Sha256: 8ac9baa2c8b9f28b0dde5f5e7ea62739ee14af68a3228a8065eac16716321acf

Contents?: true

Size: 1.37 KB

Versions: 8

Compression:

Stored size: 1.37 KB

Contents

require 'chunky_png'

module Facades
  module Helpers
    ##
    # Misc helpers for front-end development.
    # 
    module Frontend
      
      def placeholder_image(*args)
        @_placeholder_image_cache ||= ImageCache.new
        options = args.extract_options!
        image_tag(@_placeholder_image_cache.get(args), options)
      end
      
      ##
      # Caches placeholder image data based on 
      # a md5 hash. 
      # 
      class ImageCache
        
        def get(args)
          hash    = ::Digest::MD5.hexdigest("#{args.join('|')}")
          return cache[hash] if cache[hash].present?
          
          width   = args.shift
          height  = (args.shift || width)
          color   = (args.shift || '#cccccc')
          text    = args.shift
          
          cache[hash] = generate_png(width, height, color, text)
        end
        
        
        private
        
        def cache
          @_cache ||= {}
        end
        
        ##
        # Generates an image based on the provided parameters.
        # 
        # 
        def generate_png(width, height, color, text = nil)
          color   = color.sub(/^#/, '')
          color   = "#{color}#{color}" if color.size <= 3
          color   = ChunkyPNG::Color.from_hex("##{color}")
          image   = ChunkyPNG::Image.new(width, height, color)
          image.to_data_url
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
facades-1.1.0 lib/facades/helpers/frontend.rb
facades-1.0.9 lib/facades/helpers/frontend.rb
facades-1.0.8 lib/facades/helpers/frontend.rb
facades-1.0.6 lib/facades/helpers/frontend.rb
facades-1.0.4 lib/facades/helpers/frontend.rb
facades-1.0.3 lib/facades/helpers/frontend.rb
facades-1.0.2 lib/facades/helpers/frontend.rb
facades-1.0.1 lib/facades/helpers/frontend.rb