Class: Virginity::XAbLabel

Inherits:
BaseField show all
Defined in:
lib/virginity/vcard/fields_osx.rb

Constant Summary collapse

ABLABEL =
"X-ABLabel"
SAFE_LABELS =
%w(HOME WORK FAX CELL PREF MAIN PAGER INTERNET VOICE)
STRANGELABEL_MATCHER =
/_\$!<(.*)>!\$_/
STRANGE_LABELS =
{}

Constants inherited from BaseField

BaseField::BEGIN_REGEX, BaseField::END_REGEX, BaseField::PREF, BaseField::TYPE

Constants included from FieldCleaning

FieldCleaning::BOM_BINARY, FieldCleaning::BOM_UTF8, FieldCleaning::CASE_SENSITIVE_TYPES, FieldCleaning::CHARSET, FieldCleaning::ENCODING, FieldCleaning::LIST_NAMES, FieldCleaning::QUOTED_PRINTABLE, FieldCleaning::TYPE, FieldCleaning::X_SYNTHESIS_REF

Constants inherited from ContentLine

ContentLine::COLON_CHAR, ContentLine::GROUP, ContentLine::GROUP_DELIMITER, ContentLine::NAME

Instance Attribute Summary

Attributes inherited from ContentLine

#group, #name, #params, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseField

#<=>, #==, [], #extra_fields_to_xml, #field_as_json, field_register, #group_and_params_as_json, merger, named, #params_to_xml, #params_to_xml!, parse, #pref?, #raw_value=, register_field, register_for, registered?, #to_xml, types, unregister, #value, #value=, #value_to_xml

Methods included from FieldCleaning

#clean!, #clean_base64!, #clean_binary_data!, #clean_charsets!, #clean_quoted_printable_encoding!, #clean_types!, #guess_latin!, #remove_bom!, #remove_encoding_8bit!, #remove_x_synthesis_ref_params!, #uniq_params!

Methods included from Vcard21::Writer

#encode21, #vcard21line

Methods inherited from ContentLine

#<=>, #==, #api_id, #encode, #eql?, #has_name?, #hash, #initialize, line_parts, #merge_with!, merger, #param_values, #params_to_s, parse, #pretty_print, #to_field

Methods included from Encodings

#binary?, #to_ascii, #to_binary, #to_default, #to_default!, #verify_utf8ness

Constructor Details

This class inherits a constructor from Virginity::ContentLine

Class Method Details

.from_field(field) ⇒ Object

returns an array of XAbLables



46
47
48
# File 'lib/virginity/vcard/fields_osx.rb', line 46

def self.from_field(field)
  types_to_convert_to_xablabel(field).map { |t| from_param(t) }
end

.from_param(param, options = {}) ⇒ Object

SAFECHARS = /^(.[^";:,])*$/ ALREADYQOUTED = /^"(.*?)"$/

Raises:

  • (TypeError)


23
24
25
26
# File 'lib/virginity/vcard/fields_osx.rb', line 23

def self.from_param(param, options = {})
  raise TypeError, "expected a Param with key == \"TYPE\"" unless param.key.upcase == "TYPE"
  from_text(param.value, options)
end

.from_text(t, options = {}) ⇒ Object



32
33
34
35
36
37
# File 'lib/virginity/vcard/fields_osx.rb', line 32

def self.from_text(t, options = {})
  new(ABLABEL).tap do |label|
    label.text = t
    label.group = options[:group]
  end
end

.types_to_convert_to_xablabel(field) ⇒ Object



39
40
41
42
43
# File 'lib/virginity/vcard/fields_osx.rb', line 39

def self.types_to_convert_to_xablabel(field)
  field.params('TYPE').reject do |type|
    SAFE_LABELS.include?(type.value)
  end
end

Instance Method Details

#textObject



50
51
52
53
54
55
56
# File 'lib/virginity/vcard/fields_osx.rb', line 50

def text
  if match = STRANGELABEL_MATCHER.match(@value)
    match[1].upcase
  else
    @value
  end
end

#text=(text) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/virginity/vcard/fields_osx.rb', line 58

def text=(text)
  if x = STRANGE_LABELS[text.upcase]
    @value = "_$!<#{x}>!$_"
  else
    @value = text
  end
end

#to_paramObject



28
29
30
# File 'lib/virginity/vcard/fields_osx.rb', line 28

def to_param
  Param.new("TYPE", text)
end