Sha256: 983473d9f5bee34bdff7b3314fc627e057d54433ff77c3ae7bb62c937285a78f

Contents?: true

Size: 1.47 KB

Versions: 7

Compression:

Stored size: 1.47 KB

Contents

module Storefront
  module FormatHelper
    def rendered_text(string)
      string.to_s.
        gsub(/<\/?[^>]*>/, '').
        gsub(/\n/, " ").
        gsub("\r", " ").
        squeeze(" ").
        gsub(/\"/, "&Prime;"). # convert quotes to inches
        gsub(/\'/, "&prime;"). # single quote to feet
        gsub(/\$0?0*\.(\d+)/, "\\1&cent;").html_safe
    end
    
    def format_money(value)
      value.present? ? ("$" + number_with_precision(value.to_s.gsub("$", ""), :precision => 2)) : nil
    end
    
    def format_percent(value)
      value.present? ? (number_with_precision(value.to_s.gsub("%", ""), :precision => 0) + "%") : nil
    end
    
    def sanitize(text)
      text.gsub(/<\/?[^>]*>/, '').gsub(/\n/, " ").gsub("\r", " ").squeeze(" ")
    end
    
    def n(value)
      if value.respond_to?(:nan?) && value.nan?
        0
      else
        value
      end
    end
    
    def s(string, default = "N/A")
      string.present? ? string : default
    end

    def num(num, default = "N/A")
      result = number_with_precision(num, :precision => 1)
      result.blank? ? default : result
    end

    def money(value)
      value ? value.to_s.gsub(/^\$?/, "$") : "$0"
    end
    
    def read_binary_file(path)
      File.open(path, 'rb') {|f| f.read }
    end

    def encoded_contents(path)
      Base64.encode64(read_binary_file(path)).gsub(/\n/, '')
    end

    def font_face_data_uri(path)
      "url(\"data:font/truetype;base64,#{encoded_contents(path)}\")"
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
storefront-0.3.2 lib/storefront/helpers/format_helper.rb
storefront-0.3.1 lib/storefront/helpers/format_helper.rb
storefront-0.3.0 lib/storefront/helpers/format_helper.rb
storefront-0.2.8 lib/storefront/helpers/format_helper.rb
storefront-0.2.7 lib/storefront/helpers/format_helper.rb
storefront-0.2.1 lib/storefront/helpers/format_helper.rb
storefront-0.2.0 lib/storefront/helpers/format_helper.rb