Sha256: 3e94aaf3a5166a5c814836e3d6fa26627f4f6c722d5be1e073a8aa4a94752c35

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

module GreenEggsAndSpam

  class Options < Hash
  
    # Initializes the options hash with the defaults
    def initialize
      super
      default!
    end
    
    # Clears current options and resets to the defaults
    def default!
      clear.merge!(defaults)
    end
    
    # The default options hash
    def defaults
      {
        :key_index => { "1" => "red", "2" => "green", "3" => "blue" },
        :form_options => { :alt => 'AntiSpam Image', :class => 'antispam-image', :path => '/images/antispam', :extension => 'png' },
        :error_message => "Spam Detected! Please make sure you've properly answered the anti spam question."
      }
    end
    
    # Sets and option and converts its key to a symbol
    def []=(key, value)
      key = key.to_sym
      case key
        when :key_index
          raise "Invalid Key Index" unless value.is_a?(Hash) && 1 < value.keys.length
        when :form_options
          raise "Invalid Form Options" unless value.is_a?(Hash)
          value = fetch(:form_options).merge(value)
      end
      super(key, value)
    end
    
    # Turns the key into a symbol and returns the requested value
    def [](key)
      super(key.to_sym)
    end
    
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
green_eggs_and_spam-0.2.1 lib/green_eggs_and_spam/options.rb
green_eggs_and_spam-0.2.0 lib/green_eggs_and_spam/options.rb