Class: Taco

Inherits:
Object show all
Defined in:
lib/taco/taco.rb

Defined Under Namespace

Classes: Ambiguous, NotFound

Constant Summary collapse

HOME_DIR =
'.taco'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path = nil) ⇒ Taco

Returns a new instance of Taco.



15
16
17
# File 'lib/taco/taco.rb', line 15

def initialize(root_path=nil)
  @home = File.join(root_path || Dir.getwd, HOME_DIR)
end

Instance Attribute Details

#homeObject

Returns the value of attribute home.



10
11
12
# File 'lib/taco/taco.rb', line 10

def home
  @home
end

Instance Method Details

#init!Object

Raises:

  • (IOError)


19
20
21
22
23
24
25
# File 'lib/taco/taco.rb', line 19

def init!
  raise IOError.new("Could not create #{@home}\nDirectory already exists.") if File.exists?(@home)

  FileUtils.mkdir_p(@home)

  "Initialized #{@home}"
end

#list(opts = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/taco/taco.rb', line 66

def list(opts={})
  filter_match = if opts.fetch(:filters, []).size > 0
    conditions = opts[:filters].map do |filter|
      attr, val = filter.split(':')
      attr = Issue.schema_attr_expand(attr)
      %Q|i.send("#{attr}").to_s.downcase == "#{val.downcase}"|
    end.join ' && '

    # FIXME: eval-ing user input? madness!
    eval "Proc.new { |i| #{conditions} }"
  else
    nil
  end

  ids = Dir.glob("#{@home}/*")

  ids.map do |name|
    id = File.basename name
    issue = Issue.from_json(open(name) { |f| f.read })

    next unless filter_match.nil? || filter_match.call(issue)

    raise Issue::Invalid.new("Issue ID does not match filename: #{issue.id} != #{id}") unless issue.id == id

    the_short_id = 8.upto(id.size).each do |n|
      the_short_id = id[0...n]
      break the_short_id unless ids.count { |i| i.include? the_short_id } > 1
    end

    # because the length of the short_id is determinable only within the context of a group of issues
    # (because it must long enough to be unique), we can only define it on Issue in the context of a group
    #
    issue.instance_eval "def short_id; #{the_short_id.inspect}; end"

    issue
  end.reject(&:nil?).sort
end

#read(issue_id) ⇒ Object

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/taco/taco.rb', line 38

def read(issue_id)
  issue_path = File.join(@home, issue_id)

  unless File.exist? issue_path
    entries = Dir[File.join(@home, "*#{issue_id}*")]

    raise NotFound.new("Issue not found.") unless entries.size > 0
    unless entries.size == 1
      issue_list = entries.map do |entry|
        issue = read(File.basename(entry))
        "#{issue.id} : #{issue.summary}"
      end
      raise Ambiguous.new("Found several matching issues:\n%s" % issue_list.join("\n"))
    end

    issue_path = entries[0]
    issue_id = File.basename entries[0]
  end

  the_json = open(issue_path) { |f| f.read }

  issue = Issue.from_json the_json

  raise Issue::Invalid.new("Issue ID does not match filename: #{issue.id} != #{issue_id}") unless issue.id == issue_id

  issue
end

#write!(issue_or_issues) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/taco/taco.rb', line 27

def write!(issue_or_issues)
  issues = issue_or_issues.is_a?(Array) ? issue_or_issues : [ issue_or_issues ]

  issues.each do |issue|
    the_json = issue.to_json # do this first so we don't bother the filesystem if the issue is invalid
    open(File.join(@home, issue.id), 'w') { |f| f.write(the_json) }
  end

  issue_or_issues
end