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



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

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



7
8
9
10
11
12
# File 'lib/icalendar/has_properties.rb', line 7

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



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

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



43
44
45
# File 'lib/icalendar/has_properties.rb', line 43

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

#initialize(*args) ⇒ Object



14
15
16
17
# File 'lib/icalendar/has_properties.rb', line 14

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

#property(property_name) ⇒ Object



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

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)


73
74
75
# File 'lib/icalendar/has_properties.rb', line 73

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

#valid?(strict = false) ⇒ Boolean

Returns:

  • (Boolean)


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

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