Class: Issue

Inherits:
Object show all
Includes:
Comparable, Schema
Defined in:
lib/taco/issue.rb

Defined Under Namespace

Classes: Invalid, NotFound

Constant Summary collapse

TEMPLATE =
<<-EOT.strip
# Lines beginning with # will be ignored.
Summary     : %{summary}
Kind        : %{kind}
Status      : %{status}
Priority    : %{priority}
Owner       : %{owner}

# Everything between the --- lines is Issue Description
---
%{description}
---
EOT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Schema

included, #schema_errors, #to_hash

Constructor Details

#initialize(attributes = {}, changelog = []) ⇒ Issue

Returns a new instance of Issue.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/taco/issue.rb', line 41

def initialize(attributes={}, changelog=[])
  attributes = Hash[attributes.map { |k, v| [ k.to_sym, v ] }]

  @new = attributes[:created_at].nil? && attributes[:id].nil?

  @changelog = []

  attributes.each do |attr, value|
    schema_attr = self.class.schema_attributes[attr]
    raise ArgumentError.new("unknown attribute: #{attr}") unless schema_attr
    if schema_attr[:settable]
      self.send "#{attr}=", attributes[attr]
    end
  end

  self.id = attributes[:id] || SecureRandom.uuid.gsub('-', '')
  self.created_at = attributes[:created_at] || Time.now
  self.updated_at = attributes[:updated_at] || Time.now

  if changelog.size > 0
    @changelog = changelog.map do |thing|
      if thing.is_a? Change
        thing
      else
        Change.new thing
      end
    end
  end

  self
end

Instance Attribute Details

#changelogObject (readonly)

Returns the value of attribute changelog.



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

def changelog
  @changelog
end

Class Method Details

.from_json(the_json) ⇒ Object



173
174
175
176
177
178
179
180
181
# File 'lib/taco/issue.rb', line 173

def self.from_json(the_json)
  begin
    hash = JSON.parse(the_json)
  rescue JSON::ParserError => e
    raise Issue::Invalid.new(e.to_s)
  end

  Issue.new(hash['issue'], hash['changelog'])
end

.from_template(text) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/taco/issue.rb', line 183

def self.from_template(text)
  issue = { :description => '' }
  reading_description = false

  text.lines.each_with_index do |line, index|
    next if line =~ /^#/ || (!reading_description && line =~ /^\s*$/)

    if line =~ /^---$/
      # FIXME: this means that there can be multiple description blocks in the template!
      #
      reading_description = !reading_description
      next
    end

    if !reading_description && line =~ /^(\w+)\s*:\s*(.*)$/
      key, value = $1.downcase.to_sym, $2.strip

      if schema_attributes.include?(key) && schema_attributes[key][:settable]
        issue[key] = value
      else
        raise ArgumentError.new("Unknown Issue attribute: #{key} on line #{index+1}") unless schema_attributes.include?(key)
        raise ArgumentError.new("Cannot set write-protected Issue attribute: #{key} on line #{index+1}")
      end
    elsif reading_description
      issue[:description] += line
    else
      raise ArgumentError.new("Cannot parse line #{index+1}")
    end
  end

  Issue.new(issue)
end

Instance Method Details

#<=>(other) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/taco/issue.rb', line 84

def <=>(other)
  if self.class.schema_attributes.all? { |attr, opts| self.send(attr) == other.send(attr) }
    r = 0
  else
    if self.created_at == other.created_at
      r = self.id <=> other.id
    else
      r = self.created_at <=> other.created_at
    end

    # this clause should not return 0, we've already established inequality
    #
    r = -1 if r == 0
  end

  r
end

#inspectObject



102
103
104
105
106
107
108
# File 'lib/taco/issue.rb', line 102

def inspect
  fields = self.class.schema_attributes.map do |attr, opts|
    "@#{attr}=#{self.send(attr).inspect}"
  end.join ', '

  "#<#{self.class}:0x%016x %s>" % [ object_id, fields ]
end

#new?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/taco/issue.rb', line 80

def new?
  @new
end

#schema_attribute_change(attribute, old_value, new_value) ⇒ Object



73
74
75
76
77
78
# File 'lib/taco/issue.rb', line 73

def schema_attribute_change(attribute, old_value, new_value)
  if self.class.schema_attributes[attribute][:settable]
    self.updated_at = Time.now
    @changelog << Change.new(:attribute => attribute, :old_value => old_value, :new_value => new_value)
  end
end

#schema_valid?Object



9
# File 'lib/taco/issue.rb', line 9

alias_method :schema_valid?, :valid?

#to_json(state = nil) ⇒ Object



141
142
143
144
145
# File 'lib/taco/issue.rb', line 141

def to_json(state=nil)
  valid? :raise => true
  hash = { :issue => self.to_hash, :changelog => changelog }
  JSON.pretty_generate(hash)
end

#to_s(opts = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/taco/issue.rb', line 110

def to_s(opts={})
  text = <<-EOT.strip
ID          : #{id}
Created At  : #{date(created_at)}
Updated At  : #{date(updated_at)}

Summary     : #{summary}
Kind        : #{kind}
Status      : #{status}
Priority    : #{priority}
Owner       : #{owner}

---
#{description}
EOT

  if opts[:changelog]
    changelog_str = changelog.map { |c| %Q|# #{c.to_s.strip.gsub(/\n/, "\n# ")}| }.join("\n")
    text << %Q|\n---\n\n#{changelog_str}|
  end

  text
end

#to_template(opts = {}) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/taco/issue.rb', line 147

def to_template(opts={})
  if new?
    header = "# New Issue\n#"
    body = TEMPLATE % self.to_hash.merge(opts.fetch(:defaults, {}))
    footer = ""
  else
    header =<<-EOT
# Edit Issue
#
# ID          : #{id}
# Created At  : #{created_at}
# Updated At  : #{updated_at}
#
EOT
    body = TEMPLATE % self.to_hash

    footer =<<-EOT
# ChangeLog
#
#{changelog.map { |c| %Q|# #{c.to_s.strip.gsub(/\n/, "\n# ")}| }.join("\n")}
EOT
  end

  (header + "\n" + body + "\n\n" + footer).strip
end

#update_from_template!(text) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
# File 'lib/taco/issue.rb', line 216

def update_from_template!(text)
  new_issue = Issue.from_template(text)

  attrs = self.class.schema_attributes.map do |attr, opts|
    if opts[:settable] && self.send(attr) != (new_value = new_issue.send(attr))
      self.send("#{attr}=", new_value)
    end
  end

  self
end

#valid?(opts = {}) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



134
135
136
137
138
139
# File 'lib/taco/issue.rb', line 134

def valid?(opts={})
  valid = schema_valid?
  error = schema_errors.first
  raise Invalid.new("attribute #{error.first}: #{error[1].inspect} is not a valid value") if !valid && opts[:raise]
  valid
end