lib/vobject/property.rb in vobject-0.1.0 vs lib/vobject/property.rb in vobject-1.0.2

- old
+ new

@@ -1,83 +1,162 @@ -module Vobject +require "vobject" +require "vobject/parameter" - class Property +class Vobject::Property + attr_accessor :group, :prop_name, :params, :value, :multiple, :norm - MAX_LINE_WIDTH = 75 + def <=>(another) + if self.prop_name =~ /^VERSION$/i + -1 + elsif another.prop_name =~ /^VERSION$/i + 1 + else + self.to_norm <=> another.to_norm + end + end - attr_accessor :group, :prop_name, :params, :value - - def initialize key, options - self.group = options[:group] + def initialize(key, options) + if options.class == Array + self.multiple = [] + options.each do |v| + multiple << property_base_class.new(key, v) + self.prop_name = key + end + else self.prop_name = key - self.params = options[:params] - self.value = parse_value(options[:value]) + if options.nil? || options.empty? + self.group = nil + self.params = [] + self.value = nil + else + self.group = options[:group] + self.prop_name = key + unless options[:params].nil? || options[:params].empty? + self.params = [] + options[:params].each do |k, v| + params << parameter_base_class.new(k, v) + end + end + # self.value = parse_value(options[:value]) + self.value = options[:value] + end + end + self.norm = nil + raise_invalid_initialization if key != name + end - raise_invalid_initialization if key != name + def to_s + if multiple.nil? || multiple.empty? + ret = to_s_line + else + arr = [] + multiple.each do |x| + arr << x.to_s_line + end + ret = arr.join("") end + ret + end - def to_s - line = group ? "#{group}." : "" - line << "#{name}" + def to_s_line + line = group ? "#{group}." : "" + line << name.to_s.tr("_", "-") - (params || {}).each do |pname, pvalue| - pvalue.to_s.gsub!(/\n/, '\n') + (params || {}).each do |p| + line << ";#{p}" + end - line << ";#{pname}=#{pvalue}" - end + line << ":#{value}" - line << ":#{value}" + line = Vobject::fold_line(line) << "\n" - line = fold_line(line) << "\n" + line + end - line + def to_norm + if @norm.nil? + if multiple.nil? || multiple.empty? + ret = to_norm_line + else + arr = [] + multiple.sort.each do |x| + arr << x.to_norm_line + end + ret = arr.join("") + end + @norm = ret end + @norm + end - private + def to_norm_line + line = group ? "#{group}." : "" + line << name.to_s.tr("_", "-").upcase - def name - prop_name + (params || {}).sort.each do |p| + line << ";#{p.to_norm}" end - 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 + line << ":#{value.to_norm}" - def parse_text_value value - value - end + line = Vobject::fold_line(line) << "\n" - def value_type - (params || {})[:VALUE] || default_value_type - end + line + end - def default_value_type - "text" - end - def raise_invalid_initialization - raise "vObject property initialization failed" + def to_hash + ret = {} + if multiple + ret[prop_name] = [] + multiple.each do |c| + ret[prop_name] = ret[prop_name] << c.to_hash[prop_name] + end + else + ret = {prop_name => { value: value.to_hash } } + ret[prop_name][:group] = group unless group.nil? + if params + ret[prop_name][:params] = {} + params.each do |p| + ret[prop_name][:params] = ret[prop_name][:params].merge p.to_hash + end + end end + ret + end - # This implements the line folding as specified in - # http://tools.ietf.org/html/rfc6350#section-3.2 - # - # NOTE: the "line" here is not including the trailing \n - def fold_line(line) - folded_line = line[0, MAX_LINE_WIDTH] - remainder_line = line[MAX_LINE_WIDTH, line.length - MAX_LINE_WIDTH] || '' + private - max_width = MAX_LINE_WIDTH - 1 + def name + prop_name + end - for i in 0..((remainder_line.length - 1) / max_width) - folded_line << "\n " - folded_line << remainder_line[i * max_width, max_width] - end + 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 - folded_line - end + def parse_text_value(value) + value + end + def value_type + params ? params[0].value : default_value_type end + def default_value_type + "text" + end + + def property_base_class + Vobject::Property + end + + def parameter_base_class + Vobject::Parameter + end + + def raise_invalid_initialization + raise "vObject property initialization failed" + end end