Class: VCardigan::Property
- Inherits:
-
Object
- Object
- VCardigan::Property
- Defined in:
- lib/vcardigan/property.rb
Direct Known Subclasses
Constant Summary collapse
- APPLE_LABEL =
/_\$!<(.*?)>!\$_/
Instance Attribute Summary collapse
-
#group ⇒ Object
Returns the value of attribute group.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(vcard, name, *args) ⇒ Property
constructor
A new instance of Property.
- #param(name) ⇒ Object
- #to_s ⇒ Object
- #value(idx = 0) ⇒ Object
Constructor Details
#initialize(vcard, name, *args) ⇒ Property
Returns a new instance of Property.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/vcardigan/property.rb', line 12 def initialize(vcard, name, *args) @vcard = vcard @params = {} @values = [] @group = nil setup if respond_to? :setup # Determine whether this property name has a group name_parts = name.to_s.split('.', 2) # If it has a group, set it if name_parts.length > 1 @group = name_parts.first end # Set the name @name = name_parts.last.downcase # Build out the values/params from the passed arguments valueIdx = 0 args.each do |arg| if arg.is_a? Hash arg.each do |param, value| param = param_name(param.to_s.downcase, value) value = param_value(param, value) add_param(param, value) end else value = parse_value(arg.to_s) add_value(value, valueIdx) valueIdx += 1 end end end |
Instance Attribute Details
#group ⇒ Object
Returns the value of attribute group.
7 8 9 |
# File 'lib/vcardigan/property.rb', line 7 def group @group end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/vcardigan/property.rb', line 8 def name @name end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
9 10 11 |
# File 'lib/vcardigan/property.rb', line 9 def params @params end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
10 11 12 |
# File 'lib/vcardigan/property.rb', line 10 def values @values end |
Class Method Details
.create(vcard, name, *args) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/vcardigan/property.rb', line 48 def self.create(vcard, name, *args) name = name.to_s.downcase classname = '' case name when 'n' className = 'NameProperty' else className = 'Property' end cls = Module.const_get('VCardigan').const_get(className) cls.new(vcard, name, *args) end |
.parse(vcard, data) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/vcardigan/property.rb', line 63 def self.parse(vcard, data) # Gather the parts data = data.strip parts = data.split(':', 2) values = parts.last.split(';') params = parts.first.split(';') name = params.shift # Create argument array args = [vcard, name] # Add values to array args.concat(values) # Add params to array params.each do |param| keyval = param.split('=') hash = Hash[keyval.first, keyval.last] args.push(hash) end # Instantiate a new class with the argument array self.create(*args) end |
Instance Method Details
#param(name) ⇒ Object
92 93 94 |
# File 'lib/vcardigan/property.rb', line 92 def param(name) name ? @params[name.to_s.downcase] : nil end |
#to_s ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/vcardigan/property.rb', line 96 def to_s # Name/Group name = @name.upcase property = @group ? "#{@group}.#{name}" : name.upcase # Params @params.each do |param, value| str = param_to_s(param, value) property << ';' << str if str end # Split with colon property << ':' # Values @values.each_with_index do |value, idx| property << ';' unless idx === 0 property << value end line_fold(property) end |
#value(idx = 0) ⇒ Object
88 89 90 |
# File 'lib/vcardigan/property.rb', line 88 def value(idx = 0) @values[idx] end |