class Vobject::Parameter

Attributes

multiple[RW]
norm[RW]
param_name[RW]
value[RW]

Public Class Methods

new(key, options) click to toggle source
# File lib/vobject/parameter.rb, line 9
def initialize(key, options)
  self.param_name = key
  if options.class == Array
    self.multiple = []
    options.each do |v|
      multiple << parameter_base_class.new(key, v)
      self.param_name = key
    end
  else
    self.value = options
  end
  norm = nil
  raise_invalid_initialization(key, name) if key != name
end

Public Instance Methods

<=>(another) click to toggle source
# File lib/vobject/parameter.rb, line 5
def <=>(another)
  self.to_norm <=> another.to_norm
end
to_hash() click to toggle source
# File lib/vobject/parameter.rb, line 72
def to_hash
  if multiple
    val = []
    multiple.each do |c|
      val << c.value
    end
    { param_name => val }
  else
    { param_name => value }
  end
end
to_norm() click to toggle source
# File lib/vobject/parameter.rb, line 47
def to_norm
  if norm.nil?
    line = param_name.to_s.tr("_", "-").upcase
    line << "="
    if multiple
      arr = []
      multiple.sort.each { |v| arr << to_norm_line(v.value) }
      line << arr.join(",")
    else
      line << to_norm_line(value)
    end
    norm = line
  end 
  norm
end
to_norm_line(val) click to toggle source
# File lib/vobject/parameter.rb, line 63
def to_norm_line(val)
  # RFC 6868
  val = val.to_s.gsub(/\^/, "^^").gsub(/\n/, "^n").gsub(/"/, "^'")
  #if val =~ /[:;,]/
  val = '"' + val + '"'
  #end
  val
end
to_s() click to toggle source
# File lib/vobject/parameter.rb, line 24
def to_s
  # we made param names have underscore instead of dash as symbols
  line = param_name.to_s.tr("_", "-")
  line << "="
  if multiple
    arr = []
    multiple.each { |v| arr << to_s_line(v.value.to_s) }
    line << arr.join(",")
  else
    line << to_s_line(value.to_s)
  end
  line
end
to_s_line(val) click to toggle source
# File lib/vobject/parameter.rb, line 38
def to_s_line(val)
  # RFC 6868
  val = val.to_s.gsub(/\^/, "^^").gsub(/\n/, "^n").gsub(/"/, "^'")
  if val =~ /[:;,]/
    val = '"' + val + '"'
  end
  val
end

Private Instance Methods

default_value_type() click to toggle source
# File lib/vobject/parameter.rb, line 104
def default_value_type
  "text"
end
name() click to toggle source
# File lib/vobject/parameter.rb, line 86
def name
  param_name
end
parameter_base_class() click to toggle source
# File lib/vobject/parameter.rb, line 108
def parameter_base_class
  Vobject::Parameter
end
parse_text_value(value) click to toggle source
# File lib/vobject/parameter.rb, line 96
def parse_text_value(value)
  value
end
parse_value(value) click to toggle source
# File lib/vobject/parameter.rb, line 90
def parse_value(value)
  parse_method = :"parse_#{value_type}_value"
  parse_method = respond_to?(parse_method, true) ? parse_method : :parse_text_value
  send(parse_method, value)
end
raise_invalid_initialization(key, name) click to toggle source
# File lib/vobject/parameter.rb, line 112
def raise_invalid_initialization(key, name)
  raise "vObject property initialization failed (#{key}, #{name})"
end
value_type() click to toggle source
# File lib/vobject/parameter.rb, line 100
def value_type
  (params || {})[:VALUE] || default_value_type
end