Class: RiCal::PropertyValue::Duration

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

Overview

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:



34
35
36
# File 'lib/ri_cal/property_value/duration.rb', line 34

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

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

:nodoc:



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

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)


62
63
64
# File 'lib/ri_cal/property_value/duration.rb', line 62

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:



8
9
10
# File 'lib/ri_cal/property_value/duration.rb', line 8

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



87
88
89
# File 'lib/ri_cal/property_value/duration.rb', line 87

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.+



102
103
104
# File 'lib/ri_cal/property_value/duration.rb', line 102

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:



66
67
68
# File 'lib/ri_cal/property_value/duration.rb', line 66

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

#hoursObject

:nodoc:



74
75
76
# File 'lib/ri_cal/property_value/duration.rb', line 74

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

#minutesObject

:nodoc:



78
79
80
# File 'lib/ri_cal/property_value/duration.rb', line 78

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

#secondsObject

:nodoc:



82
83
84
# File 'lib/ri_cal/property_value/duration.rb', line 82

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

#subtract_from_date_time_value(date_time_value) ⇒ Object

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



97
98
99
# File 'lib/ri_cal/property_value/duration.rb', line 97

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



92
93
94
# File 'lib/ri_cal/property_value/duration.rb', line 92

def to_ri_cal_duration_value
  self
end

#value=(string) ⇒ Object

:nodoc:



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

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:



70
71
72
# File 'lib/ri_cal/property_value/duration.rb', line 70

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