lib/mastodon.rb in mastodon-0.1.0 vs lib/mastodon.rb in mastodon-0.3.1

- old
+ new

@@ -1,10 +1,10 @@ require 'set' -require 'pp' +require 'core_ext/string/pull_regex' + require 'mastodon/todo' -require 'mastodon/version' class Mastodon # A context is: An at-sign (@) followed by one or more non-whitespace characters. Context_Regex = /(?<!\+)\B@(\S+)/ # A project is: An plus sign (+) followed by one or more non-whitespace characters. @@ -14,49 +14,43 @@ attr_reader :contexts, :projects, :todos # +todo+: An array of strings (each being an individual todo item) def initialize(todos) - parse! todos.map{|line| line.strip} - end - - def parse!(todos) @contexts = Set.new @projects = Set.new @todos = [] + parse! todos.map{|line| line.strip} + + @contexts = @contexts.to_a + @projects = @projects.to_a + end + + def parse!(todos) todos.each do |todo| # Looping through the string, find the metadata (context, project, # priority). Store it in a temporary variable, then clear it from # the original string. Strip all remaining whitespace at the end. current_contexts = [] current_projects = [] - current_priority, priority = nil, nil + current_priority = nil - until ((context = todo[Context_Regex]).nil?) - index = todo.index(context) - todo[index..(index+context.length)] = "" - current_contexts << context.match(Context_Regex)[1] - contexts << context.match(Context_Regex)[1] + current_contexts = todo.pull_regex(Context_Regex) + unless current_contexts.empty? + @contexts.merge(current_contexts) end - until ((project = todo[Project_Regex]).nil?) - index = todo.index(project) - todo[index..(index+project.length)] = "" - current_projects << project.match(Project_Regex)[1] - projects << project.match(Project_Regex)[1] + current_projects = todo.pull_regex(Project_Regex) + unless current_projects.empty? + @projects.merge(current_projects) end - priority = todo[Priority_Regex] - unless priority.nil? - index = todo.index(priority) - todo[index..(index+priority.length)] = "" - current_priority = priority.match(Priority_Regex)[1] - end + current_priority = todo.pull_regex(Priority_Regex) todo.strip! - @todos << Mastodon::Todo.new(todo, current_contexts, current_projects, current_priority) + @todos << Mastodon::Todo.new(todo, current_contexts, current_projects, current_priority.first) end end # How many todos there are. def size