Module: Trellohub::Form::Card

Included in:
Trellohub::Form
Defined in:
lib/trellohub/form/card.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.accessible_attributesObject



16
17
18
# File 'lib/trellohub/form/card.rb', line 16

def accessible_attributes
  self.prefix(self.valid_attributes + %i(list_name))
end

.included(base) ⇒ Object



28
29
30
31
32
33
# File 'lib/trellohub/form/card.rb', line 28

def included(base)
  base.class_eval do
    attr_accessor(*Trellohub::Form::Card.accessible_attributes)
    attr_reader(*Trellohub::Form::Card.readable_attributes)
  end
end

.prefix(array) ⇒ Object



24
25
26
# File 'lib/trellohub/form/card.rb', line 24

def prefix(array)
  array.map { |key| :"card_#{key}" }
end

.readable_attributesObject



20
21
22
# File 'lib/trellohub/form/card.rb', line 20

def readable_attributes
  self.prefix %i(labels)
end

.valid_attributesObject



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

def valid_attributes
  %i(
    closed
    desc
    idBoard
    idList
    name
    idMembers
  )
end

Instance Method Details

#build_card_attributes_by_cardObject



62
63
64
65
# File 'lib/trellohub/form/card.rb', line 62

def build_card_attributes_by_card
  list = Trellohub::List.find_by(id: @origin_card.idList)
  @card_list_name = list.name if list
end

#build_issue_attributes_by_cardObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/trellohub/form/card.rb', line 67

def build_issue_attributes_by_card
  @issue_title = @origin_card.name.gsub(card_name_prefix_matcher, '')
  @issue_state = @state = @origin_card.closed ? 'closed' : 'open'

  if @origin_card.desc =~ key_matcher
    @issue_repository = $1
    @issue_number = $2
    @key = "#{$1}##{$2}"

    repo = Trellohub.repository_by(full_name: @issue_repository)
    @issue_milestone = repo.milestone.title if repo.milestone?
  end

  unless @origin_card.idMembers.empty?
    member = Trellohub::Member.find_by(id: @origin_card.idMembers.first)
    @issue_assignee = member.username if member
  end

  if @card_list_name
    label = Trellohub.list_by(name: @card_list_name).issue_label
    @issue_labels = label ? [label] : []
  end
end

#card_idObject



114
115
116
117
118
119
120
121
# File 'lib/trellohub/form/card.rb', line 114

def card_id
  if @card_id.nil? && @imported_from == :issue
    form = Trellohub::Form.with_cards.find_by_key(@key)
    @card_id = form.card_id if form
  end

  @card_id
end

#card_name_prefix_matcherObject



58
59
60
# File 'lib/trellohub/form/card.rb', line 58

def card_name_prefix_matcher
  /^[\w\-]+#\d+\s/
end

#card_update?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/trellohub/form/card.rb', line 123

def card_update?
  !card_id.nil?
end

#close_cardObject



135
136
137
# File 'lib/trellohub/form/card.rb', line 135

def close_card
  Trell.update_card(@card_id, to_valid_card.merge(closed: true))
end

#create_cardObject



127
128
129
# File 'lib/trellohub/form/card.rb', line 127

def create_card
  Trell.create_card(to_valid_card)
end

#import_card(card) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/trellohub/form/card.rb', line 36

def import_card(card)
  @origin_card = card.dup
  @imported_from = :card

  card.attrs.keys.each do |key|
    next if key == :badges
    instance_variable_set(:"@card_#{key}", card.send(key))
  end

  build_card_attributes_by_card
  build_issue_attributes_by_card
end

#key_matcherObject

e.g.

> #<MatchData

“synced_issue: github.com/linyows-Z_2/trellohub-foo_aaa-123/issues/127” 1:“linyows-Z_2/trellohub-foo_aaa-123” 2:“127”>



54
55
56
# File 'lib/trellohub/form/card.rb', line 54

def key_matcher
  /synced_issue:\shttps?:\/\/.*?\/([\w\-\/]+)\/(?:issues|pulls)\/(\d+)/
end

#save_as_cardObject



139
140
141
142
143
144
145
# File 'lib/trellohub/form/card.rb', line 139

def save_as_card
  case
  when card_update? && open? then update_card
  when card_update? && closed? then close_card
  when open? then create_card
  end
end

#to_cardObject



153
154
155
156
157
158
159
160
# File 'lib/trellohub/form/card.rb', line 153

def to_card
  Hash[Trellohub::Form::Card.accessible_attributes.map { |key|
    [
      key.to_s.gsub('card_', '').to_sym,
      instance_variable_get(:"@#{key}")
    ]
  }]
end

#to_valid_cardObject



147
148
149
150
151
# File 'lib/trellohub/form/card.rb', line 147

def to_valid_card
  Hash[Trellohub::Form::Card.valid_attributes.map { |key|
    [key, instance_variable_get(:"@card_#{key}")]
  }]
end

#update_cardObject



131
132
133
# File 'lib/trellohub/form/card.rb', line 131

def update_card
  Trell.update_card(@card_id, to_valid_card)
end