Class: Tilia::VObject::Document

Inherits:
Component show all
Defined in:
lib/tilia/v_object/document.rb

Overview

Document.

A document is just like a component, except that it’s also the top level element.

Both a VCALENDAR and a VCARD are considered documents.

This class also provides a registry for document types.

Direct Known Subclasses

Component::VCalendar, Component::VCard

Constant Summary collapse

UNKNOWN =

Unknown document type.

1
VCALENDAR10 =

vCalendar 1.0.

2
ICALENDAR20 =

iCalendar 2.0.

3
VCARD21 =

vCard 2.1.

4
VCARD30 =

vCard 3.0.

5
VCARD40 =

vCard 4.0.

6

Constants inherited from Node

Node::PROFILE_CALDAV, Node::PROFILE_CARDDAV, Node::REPAIR

Class Attribute Summary collapse

Attributes inherited from Component

#name

Attributes inherited from Node

#iterator, #parent

Instance Method Summary collapse

Methods inherited from Component

#[], #[]=, #add, #children, #components, #delete, #destroy, #initialize_copy, #json_serialize, #key?, #remove, #select, #serialize, #to_s, #validate, #validation_rules, #xml_serialize

Methods inherited from Node

#==, #[], #[]=, #delete, #destroy, #each, #json_serialize, #key?, #serialize, #size, #validate, #xml_serialize

Constructor Details

#initialize(*args) ⇒ void

Creates a new document.

We’re changing the default behavior slightly here. First, we don’t want to have to specify a name (we already know it), and we want to allow children to be specified in the first argument.

But, the default behavior also works.

So the two sigs:

new Document(array children = [], defaults = true) new Document(string name, array children = [], defaults = true)



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tilia/v_object/document.rb', line 73

def initialize(*args)
  if args.size == 0 || args[0].is_a?(Hash) || args[0].is_a?(Array)
    args.unshift(self.class.default_name)
    args.unshift(self)

    super(*args)
  else
    args.unshift(self)
    super(*args)
  end
end

Class Attribute Details

.component_mapObject

Returns the value of attribute component_map.



55
56
57
# File 'lib/tilia/v_object/document.rb', line 55

def component_map
  @component_map
end

.default_nameObject

Returns the value of attribute default_name.



53
54
55
# File 'lib/tilia/v_object/document.rb', line 53

def default_name
  @default_name
end

.property_mapObject

Returns the value of attribute property_map.



54
55
56
# File 'lib/tilia/v_object/document.rb', line 54

def property_map
  @property_map
end

.value_mapObject

Returns the value of attribute value_map.



56
57
58
# File 'lib/tilia/v_object/document.rb', line 56

def value_map
  @value_map
end

Instance Method Details

#class_name_for_property_name(property_name) ⇒ String

Returns the default class for a property name.

Parameters:

  • property_name (String)

Returns:

  • (String)


209
210
211
212
213
214
215
# File 'lib/tilia/v_object/document.rb', line 209

def class_name_for_property_name(property_name)
  if self.class.property_map.key?(property_name)
    self.class.property_map[property_name]
  else
    Property::Unknown
  end
end

#class_name_for_property_value(value_param) ⇒ void

This method returns an undefined value.

This method returns a full class-name for a value parameter.

For instance, DTSTART may have VALUE=DATE. In that case we will look in our valueMap table and return the appropriate class name.

This method returns null if we don’t have a specialized class.

Parameters:

  • value_param (String)


196
197
198
199
200
201
202
# File 'lib/tilia/v_object/document.rb', line 196

def class_name_for_property_value(value_param)
  value_param = value_param.upcase

  return self.class.value_map[value_param] if self.class.value_map.key?(value_param)

  nil
end

#create(name, *args) ⇒ mixed

Creates a new component or property.

If it’s a known component, we will automatically call createComponent. otherwise, we’ll assume it’s a property and call createProperty instead.

Parameters:

  • name (String)
  • arg1,... (String)

    Unlimited number of args

Returns:

  • (mixed)


101
102
103
104
105
106
107
# File 'lib/tilia/v_object/document.rb', line 101

def create(name, *args)
  if self.class.component_map.key?(name.upcase)
    create_component(name, *args)
  else
    create_property(name, *args)
  end
end

#create_component(name, children = nil, defaults = true) ⇒ Component

Creates a new component.

This method automatically searches for the correct component class, based on its name.

You can specify the children either in key=>value syntax, in which case properties will automatically be created, or you can just pass a list of Component and Property object.

By default, a set of sensible values will be added to the component. For an iCalendar object, this may be something like CALSCALE:GREGORIAN. To ensure that this does not happen, set defaults to false.

Parameters:

  • name (String)
  • children (array) (defaults to: nil)
  • defaults (Boolean) (defaults to: true)

Returns:



127
128
129
130
131
132
133
134
135
136
# File 'lib/tilia/v_object/document.rb', line 127

def create_component(name, children = nil, defaults = true)
  name = name.upcase

  klass = Component

  klass = self.class.component_map[name] if self.class.component_map.key?(name)

  children = [] unless children
  klass.new(self, name, children, defaults)
end

#create_property(name, value = nil, parameters = nil, value_type = nil) ⇒ Property

Factory method for creating new properties.

This method automatically searches for the correct property class, based on its name.

You can specify the parameters either in key=>value syntax, in which case parameters will automatically be created, or you can just pass a list of Parameter objects.

Parameters:

  • name (String)
  • value (defaults to: nil)
  • parameters (array) (defaults to: nil)
  • value_type (String) (defaults to: nil)

    Force a specific valuetype, such as URI or TEXT

Returns:



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/tilia/v_object/document.rb', line 153

def create_property(name, value = nil, parameters = nil, value_type = nil)
  parameters = {} unless parameters

  # If there's a . in the name, it means it's prefixed by a groupname.
  i = name.index('.')
  if i
    group = name[0...i]
    name = name[i + 1..-1].upcase
  else
    name = name.upcase
    group = nil
  end

  klass = nil

  if value_type
    # The valueType argument comes first to figure out the correct
    # class.
    klass = class_name_for_property_value(value_type)
  end

  unless klass
    # If a VALUE parameter is supplied, we should use that.
    if parameters.key?('VALUE')
      klass = class_name_for_property_value(parameters['VALUE'])
    else
      klass = class_name_for_property_name(name)
    end
  end

  klass.new(self, name, value, parameters, group)
end

#document_typeFixnum

Returns the current document type.

Returns:

  • (Fixnum)


88
89
90
# File 'lib/tilia/v_object/document.rb', line 88

def document_type
  UNKNOWN
end