class Marmot::OptionsSanitizer

Constants

FALSE_CHECKBOXES

{:fix_vertical_metrics=>false, :fix_gasp=>false, :remove_kerning=>false, :add_spaces=>false, :add_hyphens=>false, :webonly=>false, :base64=>false, :style_link=>false, :agreement=>false}

NON_EXPERT_OPTIONS
OPTIONS
SUBSET_OPTIONS

Public Class Methods

sanitize(options, custom_options={}) click to toggle source

Sanitize options

# File lib/marmot/options_sanitizer.rb, line 46
def self.sanitize options, custom_options={}
  options = options.clone
  custom_options ||= {}
  result = {}
  OPTIONS.each_pair do |key,array|
    type = array[0]
    allowed_values = array[1..999]
    user_value = options[key].nil? ? options[key.to_s] : options[key]
    
    if !user_value.nil? && OPTIONS.has_key?(key) && !NON_EXPERT_OPTIONS.include?(key)
      options[:mode] = result[:mode] = "expert" if options[:mode].nil?
    end


    if !user_value.nil? && OPTIONS.has_key?(key) && SUBSET_OPTIONS.include?(key)
      options[:options_subset] = result[:options_subset] = "advanced" if options[:options_subset].nil?
    end

    case type
    when :array
      if !user_value.kind_of?(Array)
        if user_value.kind_of?(String)
          user_value = user_value.split(",").map{|v| v.strip}
        else
          user_value = [user_value]
        end
      end

      user_value = user_value.delete_if {|val| !allowed_values.include? val }

      result[key] = user_value unless user_value.empty?
    when :number
      result[key] = (user_value || allowed_values[0]).to_i
    when :string
      result[key] = user_value || allowed_values[0]
    when :checkbox
      if user_value == true
        result[key] = allowed_values.last
      elsif user_value == false
        #nil
      else
        result[key] = allowed_values.include?(user_value) ? (user_value) : (allowed_values[0])
      end
    when :radio
      result[key] = allowed_values.include?(user_value) ? (user_value) : (allowed_values[0])
    end
  end

  result[:formats] = ["ttf", "woff", "svg", "eotz"] if result[:formats].nil?
  result[:dirname] = [result[:id]]
  result.merge!(custom_options).delete_if { |k, v| v.nil? }
end