module Trellohub::Form::Card

Public Class Methods

accessible_attributes() click to toggle source
# File lib/trellohub/form/card.rb, line 16
def accessible_attributes
  self.prefix(self.valid_attributes + %i(list_name))
end
included(base) click to toggle source
# 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) click to toggle source
# File lib/trellohub/form/card.rb, line 24
def prefix(array)
  array.map { |key| :"card_#{key}" }
end
readable_attributes() click to toggle source
# File lib/trellohub/form/card.rb, line 20
def readable_attributes
  self.prefix %i(labels)
end
valid_attributes() click to toggle source
# File lib/trellohub/form/card.rb, line 5
def valid_attributes
  %i(
    closed
    desc
    idBoard
    idList
    name
    idMembers
  )
end

Public Instance Methods

build_card_attributes_by_card() click to toggle source
# 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_card() click to toggle source
# 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_id() click to toggle source
# 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_matcher() click to toggle source
# File lib/trellohub/form/card.rb, line 58
def card_name_prefix_matcher
  /^[\w\-]+#\d+\s/
end
card_update?() click to toggle source
# File lib/trellohub/form/card.rb, line 123
def card_update?
  !card_id.nil?
end
close_card() click to toggle source
# File lib/trellohub/form/card.rb, line 135
def close_card
  Trell.update_card(@card_id, to_valid_card.merge(closed: true))
end
create_card() click to toggle source
# File lib/trellohub/form/card.rb, line 127
def create_card
  Trell.create_card(to_valid_card)
end
import_card(card) click to toggle source
# 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_matcher() click to toggle source

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”>

# File lib/trellohub/form/card.rb, line 54
def key_matcher
  /synced_issue:\shttps?:\/\/.*?\/([\w\-\/]+)\/(?:issues|pulls)\/(\d+)/
end
save_as_card() click to toggle source
# 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_card() click to toggle source
# 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_card() click to toggle source
# 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_card() click to toggle source
# File lib/trellohub/form/card.rb, line 131
def update_card
  Trell.update_card(@card_id, to_valid_card)
end