Class: VCardMate::VCard

Inherits:
Object
  • Object
show all
Defined in:
lib/vcard_mate/vcard.rb

Constant Summary collapse

VCARD_PATTERN =
/BEGIN:VCARD\s+(.*?)VERSION:(.+?)\s+(.+?)END:VCARD/m

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version = '4.0') ⇒ VCard

Returns a new instance of VCard.



9
10
11
12
13
# File 'lib/vcard_mate/vcard.rb', line 9

def initialize(version = '4.0')
  @version = version
  @fields = {}
  @group = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vcard_mate/vcard.rb', line 36

def method_missing(method, *args)
  if args.empty?
    # Return the property/properties when no arguments are passed
    get_prop(method)
  else
    # If there's a group, add it
    if @group
      method = "#{@group}.#{method}"
    end

    # Add property to vCard
    property = VCardMate::Property.new(method, *args)
    add_prop(property)
  end
end

Instance Attribute Details

#versionObject

Returns the value of attribute version.



7
8
9
# File 'lib/vcard_mate/vcard.rb', line 7

def version
  @version
end

Instance Method Details

#[](group) ⇒ Object



31
32
33
34
# File 'lib/vcard_mate/vcard.rb', line 31

def [](group)
  @group = group
  self
end

#add_prop(property) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vcard_mate/vcard.rb', line 52

def add_prop(property)
  name = property.instance_variable_get(:@name)

  # Create a field on the fields hash, if not already present, to house
  # the property
  unless @fields.has_key? name
    @fields[name] = []
  end
  
  # Add the property to the field array
  @fields[name].push(property)
end

#fullname(*args) ⇒ Object



104
105
106
# File 'lib/vcard_mate/vcard.rb', line 104

def fullname(*args)
  fn(*args)
end

#get_prop(name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vcard_mate/vcard.rb', line 65

def get_prop(name)
  field = @fields[name.to_s.downcase]
  case field.length
  when 0
    nil
  when 1
    field.first
  else
    field
  end
end

#name(*args) ⇒ Object

Aliases ##########



100
101
102
# File 'lib/vcard_mate/vcard.rb', line 100

def name(*args)
  n(*args)
end

#parse(data) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vcard_mate/vcard.rb', line 15

def parse(data)
  match = VCARD_PATTERN.match(data)
  if match
    # Set version number
    @version = match[2]
    lines = "#{match[1]}#{match[3]}"

    # Add the parsed properties to this vCard
    lines.each_line do |line|
      property = VCardMate::Property.parse(line)
      add_prop(property)
    end
  end
  self
end

#to_sObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vcard_mate/vcard.rb', line 77

def to_s
  # Start vCard
  vcard = VCardMate::Property.new(:begin, 'VCARD').to_s << "\n"

  # Add version
  vcard << VCardMate::Property.new(:version, @version).to_s << "\n"

  # Add the properties
  @fields.each do |field, properties|
    properties.each do |property|
      vcard << property.to_s << "\n"
    end
  end

  # END
  vcard << VCardMate::Property.new(:end, 'VCARD').to_s << "\n"

  # Return vCard
  return vcard
end