Module: Icalendar::HasProperties

Included in:
Component
Defined in:
lib/icalendar/has_properties.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


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/icalendar/has_properties.rb', line 58

def method_missing(method, *args, &block)
  method_name = method.to_s
  if method_name.start_with? 'x_'
    if method_name.end_with? '='
      append_custom_property method_name.chomp('='), args.first
    else
      custom_property method_name
    end
  else
    super
  end
end

Class Method Details

.included(base) ⇒ Object


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

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

Instance Method Details

#append_custom_property(property_name, value) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/icalendar/has_properties.rb', line 45

def append_custom_property(property_name, value)
  property_name = property_name.downcase
  if self.class.single_properties.include? property_name
    send "#{property_name}=", value
  elsif self.class.multiple_properties.include? property_name
    send "append_#{property_name}", value
  elsif value.is_a? Icalendar::Value
    custom_properties[property_name] << value
  else
    custom_properties[property_name] << Icalendar::Values::Text.new(value)
  end
end

#custom_property(property_name) ⇒ Object


41
42
43
# File 'lib/icalendar/has_properties.rb', line 41

def custom_property(property_name)
  custom_properties[property_name.downcase]
end

#initialize(*args) ⇒ Object


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

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

#property(property_name) ⇒ Object


32
33
34
35
36
37
38
39
# File 'lib/icalendar/has_properties.rb', line 32

def property(property_name)
  property_name = property_name.downcase
  if self.class.properties.include? property_name
    send property_name
  else
    custom_property property_name
  end
end

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

Returns:

  • (Boolean)

71
72
73
# File 'lib/icalendar/has_properties.rb', line 71

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

#valid?(strict = false) ⇒ Boolean

Returns:

  • (Boolean)

17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/icalendar/has_properties.rb', line 17

def valid?(strict = false)
  self.class.required_properties.each_pair do |prop, validator|
    validator.call(self, send(prop)) or return false
  end
  self.class.mutex_properties.each do |mutexprops|
    mutexprops.map { |p| send p }.compact.size > 1 and return false
  end
  if strict
    self.class.suggested_single_properties.each do |single_prop|
      send(single_prop).size > 1 and return false
    end
  end
  true
end