Skip to content

Commit

Permalink
Improve Vtodo support / add Maker
Browse files Browse the repository at this point in the history
  • Loading branch information
dpocock committed Jan 31, 2015
1 parent ab68da1 commit 5862e54
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/vpim/icalendar.rb
Expand Up @@ -101,7 +101,14 @@ def add_event(&block) #:yield:event
push Vevent::Maker.make( &block )
end

# TODO add_todo, add_journal
# TODO add_journal

# Add a task/todo to this calendar.
#
# Yields a todo maker, Icalendar::Vtodo::Maker.
def add_todo(&block) #:yield:todo
push Vtodo::Maker.make( &block )
end

=begin
TODO
Expand Down
46 changes: 46 additions & 0 deletions lib/vpim/vtodo.rb
Expand Up @@ -97,6 +97,52 @@ def percent_complete
propinteger 'PERCENT-COMPLETE'
end

# Make a new Vtodo, or make changes to an existing Vtodo.
class Maker
include Vpim::Icalendar::Set::Util #:nodoc:
include Vpim::Icalendar::Set::Common
include Vpim::Icalendar::Set::Location

# The todo that changes are being made to.
attr_reader :todo

def initialize(todo) #:nodoc:
@todo = todo
@comp = todo
end

# Make changes to +todo+. If +todo+ is not specified, creates a new
# todo. Yields a Vtodo::Maker, and returns +todo+.
def self.make(todo = Vpim::Icalendar::Vtodo.create) #:yield:maker
m = self.new(todo)
yield m
m.todo
end

# Set transparency to "OPAQUE" or "TRANSPARENT", see Vpim::Vtodo#transparency.
def transparency(token)
set_token 'TRANSP', ["OPAQUE", "TRANSPARENT"], "OPAQUE", token
end

# Add a RRULE to this todo. The rule can be provided as a pre-build
# RRULE value, or the RRULE maker can be used.
def add_rrule(rule = nil, &block) #:yield: Rrule::Maker
# TODO - should be in Property::Reccurrence::Set
unless rule
rule = Rrule::Maker.new(&block).encode
end
@comp.properties.push(Vpim::DirectoryInfo::Field.create("RRULE", rule))
self
end

# Set the RRULE for this todo. See #add_rrule
def set_rrule(rule = nil, &block) #:yield: Rrule::Maker
rm_all("RRULE")
add_rrule(rule, &block)
end

end

end

end
Expand Down
11 changes: 11 additions & 0 deletions test/test_ical.rb
Expand Up @@ -443,5 +443,16 @@ def test_event_maker_w_rrule
assert_match(/RRULE:FREQ=DAILY/, vc.to_s)
end

def test_todo_maker_w_rrule
vc = Icalendar.create2 do |vc|
vc.add_todo do |m|
m.add_rrule("freq=monthly")
m.set_rrule do |_| _.frequency = "daily" end
end
end
assert_no_match(/RRULE:FREQ=MONTHLY/, vc.to_s)
assert_match(/RRULE:FREQ=DAILY/, vc.to_s)
end

end

0 comments on commit 5862e54

Please sign in to comment.