Class: RiCal::PropertyValue::Duration

Inherits:
RiCal::PropertyValue show all
Defined in:
lib/ri_cal/property_value/duration.rb

Overview

  • ©2009 Rick DeNatale, All rights reserved. Refer to the file README.txt for the license

RiCal::PropertyValue::CalAddress represents an icalendar Duration property value which is defined in rfc 2445 section 4.3.6 p 37

Instance Attribute Summary

Attributes inherited from RiCal::PropertyValue

#params, #timezone_finder, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RiCal::PropertyValue

#add_date_times_to, date_or_date_time, date_or_date_time_or_period, #default_tzid, #enumerator, #equality_value, #find_timezone, #for_parent, if_valid_string, #initialize, #parms_string, #ruby_value, #to_options_hash, #to_ri_cal_property_value, #to_s, #tz_info_source?, #validate_value, #visible_params

Constructor Details

This class inherits a constructor from RiCal::PropertyValue

Class Method Details

.convert(parent, ruby_object) ⇒ Object

:nodoc:



36
37
38
# File 'lib/ri_cal/property_value/duration.rb', line 36

def self.convert(parent, ruby_object) # :nodoc:
  ruby_object.to_ri_cal_duration_value.for_parent(parent)
end

.from_datetimes(parent, start, finish, sign = '+') ⇒ Object

:nodoc:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ri_cal/property_value/duration.rb', line 14

def self.from_datetimes(parent, start, finish, sign='+') # :nodoc:
  if start > finish
    from_datetimes(self, finish, start, '-')
  else
    diff = finish - start
    days_diff = diff.to_i
    hours = (diff - days_diff) * 24
    hour_diff = hours.to_i
    minutes = (hours - hour_diff) * 60
    min_diff = minutes.to_i
    seconds = (minutes - min_diff) * 60
    sec_diff = seconds.to_i

    day_part = value_part('D',days_diff)
    hour_part = value_part('H', hour_diff)
    min_part = value_part('M', min_diff)
    sec_part = value_part('S', sec_diff)
    t_part = (hour_diff.abs + min_diff.abs + sec_diff.abs) == 0 ? "" : "T"
    new(parent, :value => "#{sign}P#{day_part}#{t_part}#{hour_part}#{min_part}#{sec_part}")        
  end
end

.valid_string?(string) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


64
65
66
# File 'lib/ri_cal/property_value/duration.rb', line 64

def self.valid_string?(string) #:nodoc:
  string =~  /^[+-]?P((\d+D)(T((\d+)[HMS])+)?)|(T((\d+)[HMS])+)|(\d+W)$/
end

.value_part(unit, diff) ⇒ Object

:nodoc:



10
11
12
# File 'lib/ri_cal/property_value/duration.rb', line 10

def self.value_part(unit, diff) # :nodoc:
  (diff == 0) ? nil : "#{diff}#{unit}"
end

Instance Method Details

#==(other) ⇒ Object

Determine whether another object is an equivalent RiCal::PropertyValue::Duration



89
90
91
# File 'lib/ri_cal/property_value/duration.rb', line 89

def ==(other)
  other.kind_of?(PropertyValue::Duration) && value == other.value
end

#add_to_date_time_value(date_time_value) ⇒ Object

Double-dispatch method to support RiCal::PropertyValue::DateTime.+



104
105
106
# File 'lib/ri_cal/property_value/duration.rb', line 104

def add_to_date_time_value(date_time_value)
  date_time_value.advance(:weeks => weeks, :days => days, :hours => hours, :minutes => minutes, :seconds => seconds)
end

#daysObject

:nodoc:



68
69
70
# File 'lib/ri_cal/property_value/duration.rb', line 68

def days # :nodoc:
  @days * @sign
end

#hoursObject

:nodoc:



76
77
78
# File 'lib/ri_cal/property_value/duration.rb', line 76

def hours # :nodoc:
  @hours * @sign
end

#minutesObject

:nodoc:



80
81
82
# File 'lib/ri_cal/property_value/duration.rb', line 80

def minutes # :nodoc:
  @minutes * @sign
end

#secondsObject

:nodoc:



84
85
86
# File 'lib/ri_cal/property_value/duration.rb', line 84

def seconds # :nodoc:
  @seconds * @sign
end

#subtract_from_date_time_value(date_time_value) ⇒ Object

Double-dispatch method to support RiCal::PropertyValue::DateTime.-



99
100
101
# File 'lib/ri_cal/property_value/duration.rb', line 99

def subtract_from_date_time_value(date_time_value)
  date_time_value.advance(:weeks => -weeks, :days => -days, :hours => -hours, :minutes => -minutes, :seconds => -seconds)
end

#to_ri_cal_duration_valueObject

Returns the receiver



94
95
96
# File 'lib/ri_cal/property_value/duration.rb', line 94

def to_ri_cal_duration_value
  self
end

#value=(string) ⇒ Object

:nodoc:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ri_cal/property_value/duration.rb', line 40

def value=(string) # :nodoc:
  super
  match = /([+-])?P(.*)$/.match(string)
  @days = @hours = @minutes = @seconds = @weeks = 0
  if match
    @sign = match[1] == '-' ? -1 : 1
    match[2].scan(/(\d+)([DHMSW])/) do |digits, unit|
      number = digits.to_i
      case unit
      when 'D'
        @days = number
      when 'H'
        @hours = number
      when 'M'
        @minutes = number
      when 'S'
        @seconds = number
      when 'W'
        @weeks = number
      end
    end
  end
end

#weeksObject

:nodoc:



72
73
74
# File 'lib/ri_cal/property_value/duration.rb', line 72

def weeks # :nodoc:
  @weeks * @sign
end