Class: Virginity::ContentLine
- Inherits:
-
Object
- Object
- Virginity::ContentLine
- 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
Constant Summary collapse
- GROUP_DELIMITER =
"."
- COLON_CHAR =
":"
- GROUP =
/#{Bnf::NAME}\./
- NAME =
/#{Bnf::NAME}/
Instance Attribute Summary collapse
-
#group ⇒ Object
Returns the value of attribute group.
-
#name ⇒ Object
Returns the value of attribute name.
-
#params(key = nil) ⇒ Object
if key is given, return only matching parameters.
-
#value ⇒ Object
(also: #raw_value)
Returns the value of attribute value.
Class Method Summary collapse
-
.line_parts(line) ⇒ Object
decode a contentline, returns the four parts [group, name, params, value].
-
.merger(left, right) ⇒ Object
the combination of two content lines This method will raise a MergeError if names, groups or values are conflicting.
-
.parse(line = "X-FOO:bar") ⇒ Object
(also: from_line)
decode a line.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#api_id ⇒ Object
api_id, a SHA1 hash of the whole line.
-
#encode ⇒ Object
(also: #to_s)
(options = {}).
- #eql?(other) ⇒ Boolean
- #has_name?(name) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(name = "X-FOO", value = nil, params = [], group = nil, options = {}) ⇒ ContentLine
constructor
create a ContentLine by specifying all its parts.
-
#merge_with!(other) ⇒ Object
combine with new values from another line.
-
#param_values(key = nil) ⇒ Object
convenience method to grab only the values of the parameters (without the keys).
- #params_to_s ⇒ Object
- #pretty_print(q) ⇒ Object
-
#to_field ⇒ Object
convert to a vcard-field (see Field).
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
27 28 29 30 31 32 |
# File 'lib/virginity/dir_info/content_line.rb', line 27 def initialize(name = "X-FOO", value = nil, params = [], group = nil, = {}) @group = group @name = name.to_s @params = [:no_deep_copy] ? (params) : Param.deep_copy(params) @value = value.to_s end |
Instance Attribute Details
#group ⇒ Object
Returns the value of attribute group.
19 20 21 |
# File 'lib/virginity/dir_info/content_line.rb', line 19 def group @group end |
#name ⇒ Object
Returns the value of attribute name.
19 20 21 |
# File 'lib/virginity/dir_info/content_line.rb', line 19 def name @name end |
#params(key = nil) ⇒ Object
if key is given, return only matching parameters
83 84 85 86 87 88 89 |
# File 'lib/virginity/dir_info/content_line.rb', line 83 def params(key = nil) if key.nil? @params else @params.select { |param| param.has_key?(key) } # case insensitive by design! end end |
#value ⇒ Object Also known as: raw_value
Returns the value of attribute value.
19 20 21 |
# File 'lib/virginity/dir_info/content_line.rb', line 19 def value @value end |
Class Method Details
.line_parts(line) ⇒ Object
decode a contentline, returns the four parts [group, name, params, value]
121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/virginity/dir_info/content_line.rb', line 121 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
47 48 49 50 51 52 |
# File 'lib/virginity/dir_info/content_line.rb', line 47 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
35 36 37 38 39 |
# File 'lib/virginity/dir_info/content_line.rb', line 35 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
103 104 105 |
# File 'lib/virginity/dir_info/content_line.rb', line 103 def <=>(other) str_diff(group, other.group) || str_diff(name, other.name) || (to_s <=> other.to_s) end |
#==(other) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/virginity/dir_info/content_line.rb', line 96 def ==(other) group == other.group && has_name?(other.name) && params == other.params && raw_value == other.raw_value end |
#api_id ⇒ Object
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 |
#encode ⇒ Object Also known as: to_s
(options = {})
67 68 69 70 71 |
# File 'lib/virginity/dir_info/content_line.rb', line 67 def encode #(options = {}) line = "" line << group << GROUP_DELIMITER unless group.nil? line << name << params_to_s << COLON_CHAR << raw_value end |
#eql?(other) ⇒ Boolean
111 112 113 114 115 116 |
# File 'lib/virginity/dir_info/content_line.rb', line 111 def eql?(other) group == other.group && has_name?(other.name) && params == other.params && raw_value == other.raw_value end |
#has_name?(name) ⇒ Boolean
78 79 80 |
# File 'lib/virginity/dir_info/content_line.rb', line 78 def has_name?(name) @name.casecmp(name) == 0 end |
#hash ⇒ Object
107 108 109 |
# File 'lib/virginity/dir_info/content_line.rb', line 107 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
56 57 58 59 60 61 62 63 |
# File 'lib/virginity/dir_info/content_line.rb', line 56 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)
92 93 94 |
# File 'lib/virginity/dir_info/content_line.rb', line 92 def param_values(key = nil) params(key).map { |param| param.value } end |
#params_to_s ⇒ Object
136 137 138 |
# File 'lib/virginity/dir_info/content_line.rb', line 136 def params_to_s Param::params_to_s(params) end |
#pretty_print(q) ⇒ Object
74 75 76 |
# File 'lib/virginity/dir_info/content_line.rb', line 74 def pretty_print(q) q.text({:line => { group: @group, name: @name, params: params_to_s, value: @value }}.to_yaml) end |
#to_field ⇒ Object
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 |