lib/milk_cap/rtm/resources.rb in milk_cap-0.5.1 vs lib/milk_cap/rtm/resources.rb in milk_cap-0.5.2
- old
+ new
@@ -31,26 +31,12 @@
class MilkResource
def initialize (hsh)
@hsh = hsh
- @operations = []
end
- # Saves the instance back to RTM.
- #
- def save!
-
- # TODO : compact !
-
- @operations.reverse.each do |method_name, args|
-
- self.class.execute method_name, args
- end
- @operations = []
- end
-
protected
# a class method for listing attributes that can be found
# in the hash reply coming from RTM...
#
@@ -92,30 +78,26 @@
# Returns the current timeline (fetches one if none has yet
# been prepared).
#
def self.timeline
- @timeline ||= MilkCap::RTM.get_timeline
+ @@timeline ||= MilkCap::RTM.get_timeline
end
-
- def queue_operation (method_name, args)
-
- @operations << [ method_name, args ]
- end
end
#
# The RTM Task class.
#
class Task < MilkResource
+ include DataNormalization
def self.task_attr (*att_names) #:nodoc:
att_names.each do |att_name|
class_eval %{
def #{att_name}
- @hsh['task']['#{att_name}']
+ @task_hash['#{att_name}']
end
}
end
end
@@ -143,23 +125,33 @@
:deleted,
:has_due_time,
:estimate,
:due
- def initialize (list_id, h)
+ # Task series may have multiple tasks. task_hash used to specify which task
+ # in series.
+ def initialize (list_id, task_series_hash, task_hash)
- super(h)
+ super(task_series_hash)
+ @task_hash = task_hash
- t = h['task']
-
@list_id = list_id
- @taskseries_id = h['id']
- @task_id = t['id']
+ @taskseries_id = task_series_hash['id']
+ @task_id = @task_hash['id']
- @tags = TagArray.new(self, h['tags'])
+ # Normalize the RTM structure and put it in TagArray
+ tags = normalize_rtm_tags_hash( task_series_hash['tags'] )
+ @tags = TagArray.new(self, tags)
end
+ def save!
+ if self.tags.dirty?
+ args = prepare_api_args.merge( tags: self.tags.join(",") )
+ self.class.execute('setTags', args)
+ end
+ end
+
# Deletes the task.
#
def delete!
self.class.execute('delete', prepare_api_args)
@@ -173,16 +165,12 @@
end
# Sets the tags for the task.
#
def tags= (tags)
-
- tags = tags.split(',') if tags.is_a?(String)
-
- @tags = TagArray.new(list_id, tags)
-
- queue_operation('setTasks', tags.join(','))
+ @tags = TagArray.new(self, normalize_tags_array(tags))
+ @tags.dirty!
end
def self.find (params={})
parse_tasks(execute('getList', params))
@@ -194,11 +182,11 @@
opts = { list_id: nil, parse: true }.merge(opts)
args = {}
args[:name] = name
args[:list_id] = opts[:list_id] if opts[:list_id]
- args[:timeline] = MilkCap::RTM.get_timeline
+ args[:timeline] = timeline
args[:parse] = 1 unless !opts[:parse]
h = execute('add', args)
parse_tasks(h)[0]
@@ -235,13 +223,19 @@
r
end
end
def self.parse_taskseries (list_id, o)
-
o = [ o ] unless o.is_a?(Array)
- o.collect { |s| self.new(list_id, s) }
+
+ # o is an array of taskseries. Collect flattened array of tasks out of
+ # all taskseries
+ o.inject([]) do |m,s|
+ tasks = s['task']
+ tasks = [ tasks ] unless tasks.is_a?(Array)
+ m + tasks.collect { |t| self.new(list_id, s, t) }
+ end
end
end
class List < MilkResource
@@ -269,57 +263,45 @@
# An array of tasks.
#
class TagArray #:nodoc:
include Enumerable
+ # TODO introduce method to return state change between clean & dirty tags
+ # in order to use addTags or removeTags instead of just setTags.
+
def initialize (task, tags)
+ @dirty = false
+ @prev = @tags = tags
+ end
- @task = task
-
- @tags = if tags.is_a?(Array)
- tags
- else
- tags['tag']
- end
+ def dirty=(dirty_state)
+ @dirty = dirty_state
+ @prev = @tags if !dirty_state
end
+ def dirty!; self.dirty=true; end
+ def dirty?; @dirty; end
def << (tag)
-
@tags << tag
-
- args = prepare_api_args
- args[:tags] = tag
-
- @task.queue_operation('addTags', args)
+ self.dirty!
end
def delete (tag)
-
@tags.delete tag
-
- args = prepare_api_args
- args[:tags] = tag
-
- @task.queue_operation('removeTags', args)
+ self.dirty!
end
def clear
@tags.clear
-
- args = prepare_api_args
- args[:tags] = ''
-
- @task.queue_operation('setTags', args)
+ self.dirty!
end
def join (s)
-
@tags.join(s)
end
def each
-
@tags.each { |e| yield e }
end
end
end