Class: Resources::Card

Inherits:
Base
  • Object
show all
Defined in:
lib/minglr/resources/card.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

configure, print_collection, warn

Class Method Details

.create(options = {}, status_property = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/minglr/resources/card.rb', line 5

def self.create(options = {}, status_property = nil)
  options.merge!({status_property.to_sym => "New"}) if status_property
  card = self.new(options)
  if card.save
    card.reload
    puts "Card ##{card.number} created"
  else
    warn "Unable to create card"
  end
end

.move(card_number, options = {}, config = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/minglr/resources/card.rb', line 16

def self.move(card_number, options = {}, config = {})
  if card_to_move = find(card_number)
    transition_options = { :card => card_number }
    transition_options.merge!({ :comment => options[:comment]}) if options[:comment]
    if config[:status_property]
      current_status = card_to_move.send(config[:status_property])
    else
      warn "No known status of card ##{card_number}, cannot move!"
      return
    end
    next_transition = nil
    
    card_type = card_to_move.card_type_name.downcase
    case card_type
    when /defect/
      status_states = config.select do |key, value|
        key.to_s =~ /^defect/
      end
    when /task/
      status_states = config.select do |key, value|
        key.to_s =~ /^task_state_/
      end
    when /story/
      status_states = config.select do |key, value|
        key.to_s =~ /^story_state/
      end
    else
      warn "No transitions defined for card of type #{card_to_move.card_type_name}"
      return
    end
    status_states = status_states.collect {|state| state.last }.collect {|state| state.split(">").collect { |value| value.strip } }
    next_transition = status_states.select {|state| state.first.downcase == current_status.downcase }.first.last
    transition_options.merge!({ :transition => next_transition })

    if response = TransitionExecution.create(transition_options)
      if response.attributes["status"] == "completed"
        puts "Moved card from #{current_status} to #{next_transition}"
      end
    end
  else
    warn "No card ##{card_number} found to move"
  end
end


60
61
62
63
64
65
66
67
68
69
70
# File 'lib/minglr/resources/card.rb', line 60

def self.print_all(options = [], status_property = nil)
  attributes = [:number, :card_type_name, status_property, :name].compact
  cards = find(:all)
  cards.send(:extend, Minglr::Extensions::Array)
  cards = cards.filter(attributes, options)
  if cards.any?
    print_collection(cards, attributes)
  else
    warn "No cards found"
  end
end


72
73
74
75
76
77
78
# File 'lib/minglr/resources/card.rb', line 72

def self.print_card(card_number, status_property = nil)
  if card = find(card_number.to_i)
    puts card.to_s(status_property)
  else
    warn "No card ##{card_number} found"
  end
end

.update(card_number, options = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/minglr/resources/card.rb', line 80

def self.update(card_number, options = {})
  if card_to_update = find(card_number)
    options.each do |attribute, value|
      card_to_update.send("#{attribute.to_s}=".to_sym, value)
    end
    card_to_update.save
    puts "Card ##{card_to_update.number} updated\n\n"
    puts card_to_update.to_s
  else
    warn "Unable to update card ##{card_number}"
  end
end

Instance Method Details

#to_s(status_property = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/minglr/resources/card.rb', line 93

def to_s(status_property = nil)
  attachments = []
  begin
    attachments = Attachment.find(:all, :params => { :card_number => number })
    attachments = attachments.collect do |attachment|
      "* #{attachment.file_name}: #{Resources::Base.site + attachment.url}"
    end
  rescue ActiveResource::ResourceNotFound => error
    attachments = ["N/A"]
  end
  output = <<-EOS
 Number: #{number}
   Name: #{name}
   Type: #{card_type_name.nil? ? "N/A" : card_type_name}
 Status: #{send(status_property) if status_property && self.respond_to?(status_property)}
Description: #{description.nil? ? "N/A" : description}

Attachments:
  #{attachments.join("\n")}
EOS
  output
end