Module: Icalendar::HasComponents

Included in:
Component
Defined in:
lib/icalendar/has_components.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/icalendar/has_components.rb', line 24

def method_missing(method, *args, &block)
  method_name = method.to_s
  if method_name =~ /^add_(x_\w+)$/
    component_name = $1
    custom = args.first || Component.new(component_name, component_name.upcase)
    custom_components[component_name] << custom
    yield custom if block_given?
    custom
  else
    super
  end
end

Class Method Details

.included(base) ⇒ Object


5
6
7
8
9
10
# File 'lib/icalendar/has_components.rb', line 5

def self.included(base)
  base.extend ClassMethods
  base.class_eval do
    attr_reader :custom_components
  end
end

Instance Method Details

#add_component(c) {|c| ... } ⇒ Object

Yields:

  • (c)

17
18
19
20
21
22
# File 'lib/icalendar/has_components.rb', line 17

def add_component(c)
  c.parent = self
  yield c if block_given?
  send("#{c.name.downcase}s") << c
  c
end

#initialize(*args) ⇒ Object


12
13
14
15
# File 'lib/icalendar/has_components.rb', line 12

def initialize(*args)
  @custom_components = Hash.new { |h, k| h[k] = [] }
  super
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)

37
38
39
# File 'lib/icalendar/has_components.rb', line 37

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.start_with?('add_x_') || super
end