Class: Vobject::Parameter

Inherits:
Object
  • Object
show all
Defined in:
lib/vobject/parameter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, options) ⇒ Parameter

Returns a new instance of Parameter.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#multipleObject

Returns the value of attribute multiple.



3
4
5
# File 'lib/vobject/parameter.rb', line 3

def multiple
  @multiple
end

#normObject

Returns the value of attribute norm.



3
4
5
# File 'lib/vobject/parameter.rb', line 3

def norm
  @norm
end

#param_nameObject

Returns the value of attribute param_name.



3
4
5
# File 'lib/vobject/parameter.rb', line 3

def param_name
  @param_name
end

#valueObject

Returns the value of attribute value.



3
4
5
# File 'lib/vobject/parameter.rb', line 3

def value
  @value
end

Instance Method Details

#<=>(another) ⇒ Object



5
6
7
# File 'lib/vobject/parameter.rb', line 5

def <=>(another)
  self.to_norm <=> another.to_norm
end

#to_hashObject



72
73
74
75
76
77
78
79
80
81
82
# 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_normObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



63
64
65
66
67
68
69
70
# 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_sObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



38
39
40
41
42
43
44
45
# 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