Class: Virginity::ContentLine

Inherits:
Object
  • Object
show all
Extended by:
Encodings
Includes:
Encodings
Defined in:
lib/virginity/vcard/field.rb,
lib/virginity/api_extensions.rb,
lib/virginity/dir_info/content_line.rb

Overview

Type names and parameter names are case insensitive (e.g., the type name “fn” is the same as “FN” and “Fn”). Parameter values MAY be case sensitive or case insensitive, depending on their definition.

Direct Known Subclasses

BaseField

Constant Summary collapse

GROUP_DELIMITER =
"."
COLON_CHAR =
":"
GROUP =
/#{Bnf::NAME}\./
NAME =
/#{Bnf::NAME}/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Encodings

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

Constructor Details

#initialize(name = "X-FOO", value = nil, params = [], group = nil, options = {}) ⇒ ContentLine

create a ContentLine by specifying all its parts



28
29
30
31
32
33
# File 'lib/virginity/dir_info/content_line.rb', line 28

def initialize(name = "X-FOO", value = nil, params = [], group = nil, options = {})
  @group = group
  @name = name.to_s
  @params = options[:no_deep_copy] ? (params) : Param.deep_copy(params)
  @value = value.to_s
end

Instance Attribute Details

#groupObject

Returns the value of attribute group.



20
21
22
# File 'lib/virginity/dir_info/content_line.rb', line 20

def group
  @group
end

#nameObject

Returns the value of attribute name.



20
21
22
# File 'lib/virginity/dir_info/content_line.rb', line 20

def name
  @name
end

#params(key = nil) ⇒ Object

if key is given, return only matching parameters



84
85
86
87
88
89
90
# File 'lib/virginity/dir_info/content_line.rb', line 84

def params(key = nil)
  if key.nil?
    @params
  else
    @params.select { |param| param.has_key?(key) } # case insensitive by design!
  end
end

#valueObject Also known as: raw_value

Returns the value of attribute value.



20
21
22
# File 'lib/virginity/dir_info/content_line.rb', line 20

def value
  @value
end

Class Method Details

.line_parts(line) ⇒ Object

decode a contentline, returns the four parts [group, name, params, value]



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/virginity/dir_info/content_line.rb', line 122

def self.line_parts(line)
  scanner = StringScanner.new(line)
  if group = scanner.scan(GROUP)
    group.chomp!(".")
    group
  end
  name = scanner.scan(NAME)
  name.upcase! # FIXME: names should be case insensitive when compared... only when compared
  [group, name, Param::scan_params(scanner), scanner.rest]
rescue InvalidEncoding
  raise
rescue => e
  raise InvalidEncoding, "#{scanner.string.inspect}, at pos #{scanner.pos} (original error: #{e})"
end

.merger(left, right) ⇒ Object

the combination of two content lines This method will raise a MergeError if names, groups or values are conflicting

Raises:



48
49
50
51
52
53
# File 'lib/virginity/dir_info/content_line.rb', line 48

def self.merger(left, right)
  raise MergeError, "group, #{left.group} != #{right.group}" unless left.group == right.group or left.group.nil? or left.group.nil?
  raise MergeError, "name, #{left.name} != #{right.name}" unless left.has_name?(right.name)
  raise MergeError, "value, #{left.raw_value} != #{right.raw_value}" unless left.raw_value == right.raw_value
  ContentLine.new(left.name, left.raw_value, (left.params + right.params).uniq, left.group || right.group)
end

.parse(line = "X-FOO:bar") ⇒ Object Also known as: from_line

decode a line



36
37
38
39
40
# File 'lib/virginity/dir_info/content_line.rb', line 36

def self.parse(line = "X-FOO:bar")
  group, name, params, value = line_parts(line.to_s)
  # optimization: since params is deep_copied, constructing many objects and we know for certain that params can safely be used without copying, we put it in later.
  new(name, value, params, group, :no_deep_copy => true)
end

Instance Method Details

#<=>(other) ⇒ Object



104
105
106
# File 'lib/virginity/dir_info/content_line.rb', line 104

def <=>(other)
  str_diff(group, other.group) || str_diff(name, other.name) || (to_s <=> other.to_s)
end

#==(other) ⇒ Object



97
98
99
100
101
102
# File 'lib/virginity/dir_info/content_line.rb', line 97

def ==(other)
  group == other.group &&
    has_name?(other.name) &&
    params == other.params &&
    raw_value == other.raw_value
end

#api_idObject

api_id, a SHA1 hash of the whole line



10
11
12
# File 'lib/virginity/api_extensions.rb', line 10

def api_id
  Digest::SHA1.hexdigest(to_s)
end

#encodeObject Also known as: to_s

(options = {})



68
69
70
71
72
# File 'lib/virginity/dir_info/content_line.rb', line 68

def encode #(options = {})
  line = ""
  line << group << GROUP_DELIMITER unless group.nil?
  line << name << params_to_s << COLON_CHAR << raw_value
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
# File 'lib/virginity/dir_info/content_line.rb', line 112

def eql?(other)
  group == other.group &&
    has_name?(other.name) &&
    params == other.params &&
    raw_value ==  other.raw_value
end

#has_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/virginity/dir_info/content_line.rb', line 79

def has_name?(name)
  @name.casecmp(name) == 0
end

#hashObject



108
109
110
# File 'lib/virginity/dir_info/content_line.rb', line 108

def hash
  [group, name, params, raw_value].hash
end

#merge_with!(other) ⇒ Object

combine with new values from another line. This method will raise a MergeError if names, groups or values are conflicting

Raises:



57
58
59
60
61
62
63
64
# File 'lib/virginity/dir_info/content_line.rb', line 57

def merge_with!(other)
  raise MergeError, "group, #{group} != #{other.group}" unless group == other.group or group.nil? or other.group.nil?
  raise MergeError, "name, #{name} != #{other.name}" unless has_name?(other.name)
  raise MergeError, "value, #{raw_value} != #{other.raw_value}" unless raw_value == other.raw_value
  self.group = group || other.group
  self.params = (params + other.params).uniq
  self
end

#param_values(key = nil) ⇒ Object

convenience method to grab only the values of the parameters (without the keys)



93
94
95
# File 'lib/virginity/dir_info/content_line.rb', line 93

def param_values(key = nil)
  params(key).map { |param| param.value }
end

#params_to_sObject



137
138
139
# File 'lib/virginity/dir_info/content_line.rb', line 137

def params_to_s
  Param::params_to_s(params)
end

#pretty_print(q) ⇒ Object



75
76
77
# File 'lib/virginity/dir_info/content_line.rb', line 75

def pretty_print(q)
  q.text({:line => { group: @group, name: @name, params: params_to_s, value: @value }}.to_yaml)
end

#to_fieldObject

convert to a vcard-field (see Field)



16
17
18
# File 'lib/virginity/vcard/field.rb', line 16

def to_field
  Field.parse(self)
end