Class: Vobject::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/vobject/component.rb

Defined Under Namespace

Classes: Vcalendar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, cs, err) ⇒ Component

Returns a new instance of Component.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vobject/component.rb', line 19

def initialize(key, cs, err)
  self.comp_name = key
  raise_invalid_initialization if key != name
  self.children = []
  if cs.nil?
  else
    cs.each_key do |k|
      val = cs[k]
      # iteration of array || hash values is making the value a key!
      next if k.class == Array
      next if k.class == Hash
      cc = child_class(k, val)
      if val.is_a?(Hash) && val.has_key?(:component)
        val[:component].each do |x|
          children << cc.new(k, x, [])
        end
      else
        children << cc.new(k, val)
      end
    end
  end
  self.errors = err.select { |e| !e.nil? }
  self.norm = nil
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



7
8
9
# File 'lib/vobject/component.rb', line 7

def children
  @children
end

#comp_nameObject

Returns the value of attribute comp_name.



7
8
9
# File 'lib/vobject/component.rb', line 7

def comp_name
  @comp_name
end

#errorsObject

Returns the value of attribute errors.



7
8
9
# File 'lib/vobject/component.rb', line 7

def errors
  @errors
end

#multiple_componentsObject

Returns the value of attribute multiple_components.



7
8
9
# File 'lib/vobject/component.rb', line 7

def multiple_components
  @multiple_components
end

#normObject

Returns the value of attribute norm.



7
8
9
# File 'lib/vobject/component.rb', line 7

def norm
  @norm
end

Instance Method Details

#<=>(another) ⇒ Object



9
10
11
12
13
# File 'lib/vobject/component.rb', line 9

def <=>(another)
  me = self.to_norm
  o = another.to_norm
  me <=> o
end

#blank(version) ⇒ Object



15
16
17
# File 'lib/vobject/component.rb', line 15

def blank(version)
  ingest VOBJECT: { VERSION: { value: version } }
end

#child_class(key, val) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vobject/component.rb', line 48

def child_class(key, val)
  base_class = if val.is_a?(Hash) && val.has_key?(:component)
                 component_base_class
               elsif !(val.is_a?(Hash) && !val.has_key?(:value))
                 property_base_class
               else
                 component_base_class
               end
  return base_class if [:CLASS, :OBJECT, :METHOD].include? key
  camelized_key = key.to_s.downcase.split("_").map(&:capitalize).join("")
  base_class.const_get(camelized_key) rescue base_class
end

#get_errorsObject



44
45
46
# File 'lib/vobject/component.rb', line 44

def get_errors
  errors
end

#nameObject



105
106
107
# File 'lib/vobject/component.rb', line 105

def name
  comp_name
end

#to_hashObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/vobject/component.rb', line 87

def to_hash
  a = {}
  children.each do |c|
    if c.is_a?(Vobject::Component)
      a = a.merge(c.to_hash) { |_, old, new| [old, new].flatten }
    elsif c.is_a?(Vobject::Property)
      a = a.merge(c.to_hash) { |_, old, new| [old, new].flatten }
    else
      a[c.name] = c.to_hash
    end
  end
  { comp_name => a }
end

#to_jsonObject



101
102
103
# File 'lib/vobject/component.rb', line 101

def to_json
  to_hash.to_json
end

#to_normObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/vobject/component.rb', line 70

def to_norm
  if norm.nil?
    s = "BEGIN:#{name.upcase}\n"
    properties = children.select { |c| c.is_a? Vobject::Property }
    components = children.select { |c| not c.is_a? Vobject::Property }
    # create to_norm in advance
    properties.each { |p| p.to_norm }
    properties.sort.each do |p|
      s << p.to_norm 
    end
    components.sort.each { |p| s << p.to_norm }
    s << "END:#{name.upcase}\n"
    norm = s
  end
  norm
end

#to_sObject



61
62
63
64
65
66
67
68
# File 'lib/vobject/component.rb', line 61

def to_s
  s = "BEGIN:#{name}\n"
  children.each do |c|
    s << c.to_s
  end
  s << "END:#{name}\n"
  s
end