Class: Gwtf::Items

Inherits:
Object
  • Object
show all
Defined in:
lib/gwtf/items.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_dir, project) ⇒ Items

Returns a new instance of Items.



23
24
25
26
27
28
29
# File 'lib/gwtf/items.rb', line 23

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.expand_path(File.join(data_dir, "..", "gwtf.json"))
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_itemObject

Iterates over all items in a project



76
77
78
# File 'lib/gwtf/items.rb', line 76

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



71
72
73
# File 'lib/gwtf/items.rb', line 71

def file_for_item(item)
  File.join(@data_dir, "#{item}.gwtf")
end

#item_idsObject

Array of integer IDs that belong to this project in ascending order



66
67
68
# File 'lib/gwtf/items.rb', line 66

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gwtf/items.rb', line 94

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

  list.string
end

#load_item(item) ⇒ Object

Loads an item from disk



45
46
47
48
49
# File 'lib/gwtf/items.rb', line 45

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_itemObject

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



33
34
35
36
37
38
39
40
41
42
# File 'lib/gwtf/items.rb', line 33

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_configObject

Reads the overall gwtf config file



52
53
54
# File 'lib/gwtf/items.rb', line 52

def read_config
  JSON.parse(File.read(Items.config_file(@data_dir)))
end

#save_configObject

Saves the overall gwtf config



57
58
59
60
61
62
63
# File 'lib/gwtf/items.rb', line 57

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

#statsObject

Returns an array of total, open and closed items for the current project



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gwtf/items.rb', line 81

def stats
  count = {"open" => 0, "closed" => 0}

  each_item do |item|
    count[ item.status ] += 1
  end

  count["total"] = count["open"] + count["closed"]

  count
end