Module: Icalendar::HasComponents::ClassMethods

Defined in:
lib/icalendar/has_components.rb

Instance Method Summary collapse

Instance Method Details

#component(singular_name, find_by = :uid, klass = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/icalendar/has_components.rb', line 46

def component(singular_name, find_by = :uid, klass = nil)
  components = "#{singular_name}s"
  self.components << components
  component_var = "@#{components}"

  define_method components do
    if instance_variable_defined? component_var
      instance_variable_get component_var
    else
      instance_variable_set component_var, []
    end
  end

  define_method singular_name do |c = nil, &block|
    if c.nil?
      c = begin
        klass ||= Icalendar.const_get singular_name.capitalize
        klass.new
      rescue NameError => ne
        Icalendar.logger.warn ne.message
        Component.new singular_name
      end
    end

    add_component c, &block
  end

  define_method "find_#{singular_name}" do |id|
    send(components).find { |c| c.send(find_by) == id }
  end if find_by

  define_method "add_#{singular_name}" do |c|
    send singular_name, c
  end

  define_method "has_#{singular_name}?" do
    !send(components).empty?
  end
end

#componentsObject



42
43
44
# File 'lib/icalendar/has_components.rb', line 42

def components
  @components ||= []
end