Class: Mail::ParameterHash

Inherits:
HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/mail/fields/common/parameter_hash.rb

Overview

ParameterHash is an intelligent Hash that allows you to add parameter values including the mime extension paramaters that have the name*0=“blah”, name*1=“bleh” keys, and will just return a single key called name=“blahbleh” and do any required un-encoding to make that happen

Instance Method Summary collapse

Instance Method Details

#[](key_name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mail/fields/common/parameter_hash.rb', line 11

def [](key_name)
  pairs = select { |k,v| k =~ /^#{key_name}\*/ }
  pairs = pairs.to_a if RUBY_VERSION >= '1.9'
  if pairs.empty? # Just dealing with a single value pair
    super(key_name)
  else # Dealing with a multiple value pair or a single encoded value pair
    string = pairs.sort { |a,b| a.first <=> b.first }.map { |v| v.last }.join('')
    if mt = string.match(/([\w\d\-]+)'(\w\w)'(.*)/)
      string = mt[3]
      encoding = mt[1]
    else
      encoding = nil
    end
    Mail::Encodings.param_decode(string, encoding)
  end
end

#encodedObject



28
29
30
31
32
33
34
35
36
# File 'lib/mail/fields/common/parameter_hash.rb', line 28

def encoded
  map.sort { |a,b| a.first <=> b.first }.map do |key_name, value|
    unless value.ascii_only?
      value = Mail::Encodings.param_encode(value)
      key_name = "#{key_name}*"
    end
    %Q{#{key_name}="#{value}"}
  end.join(";\r\n\t")
end