Class: Gwtf::Items
- Inherits:
-
Object
- Object
- Gwtf::Items
- Defined in:
- lib/gwtf/items.rb
Class Method Summary collapse
- .config_file(data_dir) ⇒ Object
-
.due_text(datadir) ⇒ Object
overview text of all due items in all projects.
-
.overview_text(datadir, all = false) ⇒ Object
overview text of open (or all) items in all projects.
- .setup(data_dir) ⇒ Object
Instance Method Summary collapse
-
#each_item ⇒ Object
Iterates over all items in a project.
-
#file_for_item(item) ⇒ Object
Returns the path where the JSON data for a item might be found in the current project.
-
#initialize(data_dir, project) ⇒ Items
constructor
A new instance of Items.
-
#item_ids ⇒ Object
Array of integer IDs that belong to this project in ascending order.
-
#list_text(all = false, only_if_active = false) ⇒ Object
Returns a blob of text that represents a list of items in a project.
-
#load_item(item) ⇒ Object
Loads an item from disk.
-
#new_item ⇒ Object
Creates a new blank item with the next available ID, saves it and saves the global config with the next id that should be assigned.
-
#read_config ⇒ Object
Reads the overall gwtf config file.
-
#save_config ⇒ Object
Saves the overall gwtf config.
-
#stats ⇒ Object
Returns an array of total, open and closed items for the current project.
Constructor Details
#initialize(data_dir, project) ⇒ Items
Returns a new instance of Items.
60 61 62 63 64 65 66 |
# File 'lib/gwtf/items.rb', line 60 def initialize(data_dir, project) raise "Data directory #{data_dir} does not exist" unless File.directory?(data_dir) @project = project @data_dir = data_dir @config = read_config end |
Class Method Details
.config_file(data_dir) ⇒ Object
3 4 5 |
# File 'lib/gwtf/items.rb', line 3 def self.config_file(data_dir) File.(File.join(data_dir, "..", "gwtf.json")) end |
.due_text(datadir) ⇒ Object
overview text of all due items in all projects
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/gwtf/items.rb', line 44 def self.due_text(datadir) overview = StringIO.new Gwtf.projects(datadir).each do |project| next if project == "reminders" items = Gwtf::Items.new(File.join(datadir, project), project) items.each_item do |item| overview.puts "%s: %s" % [ project, item.to_s ] if item.due? end end overview.string end |
.overview_text(datadir, all = false) ⇒ Object
overview text of open (or all) items in all projects
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/gwtf/items.rb', line 24 def self.overview_text(datadir, all=false) overview = StringIO.new Gwtf.projects(datadir).each do |project| next if project == "reminders" items = Gwtf::Items.new(File.join(datadir, project), project) if items.item_ids.size > 0 if text = items.list_text(all, true) overview.puts text overview.puts end end end overview.string end |
.setup(data_dir) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/gwtf/items.rb', line 7 def self.setup(data_dir) require 'fileutils' raise "#{data_dir} already exist" if File.exist?(data_dir) FileUtils.mkdir_p(File.join(data_dir, "backups")) FileUtils.mkdir_p(File.join(data_dir, "archive")) FileUtils.mkdir_p(File.join(data_dir, "garbage")) unless File.exist?(config_file(data_dir)) File.open(config_file(data_dir), "w") do |f| f.print({"next_item" => 0}.to_json) end end end |
Instance Method Details
#each_item ⇒ Object
Iterates over all items in a project
113 114 115 |
# File 'lib/gwtf/items.rb', line 113 def each_item item_ids.each {|item| yield load_item(item) } end |
#file_for_item(item) ⇒ Object
Returns the path where the JSON data for a item might be found in the current project
108 109 110 |
# File 'lib/gwtf/items.rb', line 108 def file_for_item(item) File.join(@data_dir, "#{item}.gwtf") end |
#item_ids ⇒ Object
Array of integer IDs that belong to this project in ascending order
103 104 105 |
# File 'lib/gwtf/items.rb', line 103 def item_ids Dir.entries(@data_dir).grep(/\.gwtf$/).map{|i| File.basename(i, ".gwtf")}.map{|i| Integer(i)}.sort end |
#list_text(all = false, only_if_active = false) ⇒ Object
Returns a blob of text that represents a list of items in a project
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/gwtf/items.rb', line 135 def list_text(all=false, only_if_active=false) list = StringIO.new items = StringIO.new count = {"open" => 0, "closed" => 0} each_item do |item| count[ item[:status] ] += 1 items.puts item.to_s if (all || item.open?) end count["total"] = count["open"] + count["closed"] return nil if only_if_active && count["open"] == 0 list.puts "Project %s items: %d / %d" % [ @project, count["open"], count["total"] ] list.puts list.puts items.string list.string end |
#load_item(item) ⇒ Object
Loads an item from disk
82 83 84 85 86 |
# File 'lib/gwtf/items.rb', line 82 def load_item(item) raise "Item #{item} does not exist" unless File.exist?(file_for_item(item)) Item.new(file_for_item(item), @project) end |
#new_item ⇒ Object
Creates a new blank item with the next available ID, saves it and saves the global config with the next id that should be assigned
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/gwtf/items.rb', line 70 def new_item item = Item.new(nil, @project) item.item_id = @config["next_item"] item.file = File.join(@data_dir, "#{item.item_id}.gwtf") @config["next_item"] += 1 save_config item end |
#read_config ⇒ Object
Reads the overall gwtf config file
89 90 91 |
# File 'lib/gwtf/items.rb', line 89 def read_config JSON.parse(File.read(Items.config_file(@data_dir))) end |
#save_config ⇒ Object
Saves the overall gwtf config
94 95 96 97 98 99 100 |
# File 'lib/gwtf/items.rb', line 94 def save_config raise "Config has not been loaded" unless @config File.open(Items.config_file(@data_dir), "w") do |f| f.print(@config.to_json) end end |
#stats ⇒ Object
Returns an array of total, open and closed items for the current project
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/gwtf/items.rb', line 118 def stats count = {"open" => 0, "closed" => 0, "overdue" => 0, "due_soon" => 0, "due_today" => 0} each_item do |item| count[ item.status ] += 1 count["overdue"] += 1 if item.overdue? count["due_soon"] += 1 if item.days_till_due == 1 count["due_today"] += 1 if item.days_till_due == 0 end count["total"] = count["open"] + count["closed"] count end |