module Icalendar::HasComponents

def self.included(base)

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

def add_component(c)

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

def add_custom_component(component_name, c)

def add_custom_component(component_name, c)
  c.parent = self
  yield c if block_given?
  (custom_components[component_name.downcase.gsub("-", "_")] ||= []) << c
  c
end

def custom_component(component_name)

def custom_component(component_name)
  custom_components[component_name.downcase.gsub("-", "_")] || []
end

def initialize(*args)

def initialize(*args)
  @custom_components = Hash.new
  super
end

def method_missing(method, *args, &block)

def method_missing(method, *args, &block)
  method_name = method.to_s
  if method_name =~ METHOD_MISSING_ADD_REGEX
    component_name = $1
    custom = args.first || Component.new(component_name, component_name.upcase)
    add_custom_component(component_name, custom, &block)
  elsif method_name =~ METHOD_MISSING_X_FLAG_REGEX && custom_component(method_name).size > 0
    custom_component method_name
  else
    super
  end
end

def respond_to_missing?(method_name, include_private = false)

def respond_to_missing?(method_name, include_private = false)
  string_method = method_name.to_s
  string_method.start_with?('add_x_') || custom_component(string_method).size > 0 || super
end