Class: Trellohub::Form

Inherits:
Object
  • Object
show all
Includes:
Card, Issue
Defined in:
lib/trellohub/form.rb,
lib/trellohub/form/card.rb,
lib/trellohub/form/issue.rb

Defined Under Namespace

Modules: Card, Issue

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Issue

accessible_attributes, #assign_card_list_by_issue, #assign_card_members_by_issue, #build_card_attributes_by_issue, #build_issue_attributes_by_issue, #close_issue, #create_issue, #import_issue, included, #issue_body, #issue_repository_name, #issue_update?, prefix, readable_attributes, #save_as_issue, #to_issue, #to_valid_issue, #update_issue, valid_attributes

Methods included from Card

accessible_attributes, #build_card_attributes_by_card, #build_issue_attributes_by_card, #card_id, #card_name_prefix_matcher, #card_update?, #close_card, #create_card, #import_card, included, #key_matcher, prefix, readable_attributes, #save_as_card, #to_card, #to_valid_card, #update_card, valid_attributes

Class Method Details

.array_extObject



25
26
27
28
29
30
31
# File 'lib/trellohub/form.rb', line 25

def array_ext
  <<-METHODS
    def find_by_key(key)
      self.find { |form| form.key == key }
    end
  METHODS
end

.common_attributesObject



10
11
12
13
14
15
16
# File 'lib/trellohub/form.rb', line 10

def common_attributes
  %i(
    key
    state
    imported_from
  )
end

.compare(base, target) ⇒ Object



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
# File 'lib/trellohub/form.rb', line 68

def compare(base, target)
  return unless base.updated_at < target.updated_at
  type = target.imported_from

  printings = [[
    "#{type} attribute",
    "#{'base'.green} (#{base.imported_from}: #{base.own_key}, #{base.updated_at})",
    "#{'comparison'.yellow} (#{target.imported_from}: #{target.own_key}, #{target.updated_at})"
  ]] if Trellohub.debug

  diff = target.send(:"to_valid_#{type}").each.with_object({}) do |(key, value), hash|
    base_value = base.send(:"to_valid_#{type}")[key]
    hash[key] = value unless value == base_value

    printings << [
      key,
      base_value.to_s.color(value == base_value ? :green : :red),
      value.to_s.yellow
    ] if Trellohub.debug
  end

  if Trellohub.debug && !diff.empty?
    max = printings.max_lengths
    printings.each.with_index(1) do |line, index|
      puts '[DIFF: Update the base]' if index == 1
      puts 3.times.map.with_index { |i| ''.ljust(max[i], '-') }.join(' | ') if index == 2
      puts line.map.with_index { |word, i| "#{word}".ljust(max[i]) }.join(' | ')
    end
  end

  diff unless diff.empty?
end

.origin_attributesObject



18
19
20
21
22
23
# File 'lib/trellohub/form.rb', line 18

def origin_attributes
  %i(
    origin_issue
    origin_card
  )
end

.with_cardsObject



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

def with_cards
  @cards ||= self.with_cards!
end

.with_cards!Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/trellohub/form.rb', line 57

def with_cards!
  array = Trellohub::Card.all.
    each.with_object([]) do |card, forms|
    form = Trellohub::Form.new
    form.import_card card
    forms << form
  end
  array.instance_eval array_ext
  array
end

.with_issuesObject



33
34
35
# File 'lib/trellohub/form.rb', line 33

def with_issues
  @issues ||= self.with_issues!
end

.with_issues!Object



37
38
39
40
41
42
43
# File 'lib/trellohub/form.rb', line 37

def with_issues!
  array = Trellohub.repositories.each.with_object([]) do |repo, forms|
    forms.concat with_issues_on(repo)
  end
  array.instance_eval array_ext
  array
end

.with_issues_on(repo) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/trellohub/form.rb', line 45

def with_issues_on(repo)
  repo.issues.each.with_object([]) do |issue, forms|
    form = Trellohub::Form.new
    form.import_issue repo.full_name, issue
    forms << form
  end
end

Instance Method Details

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?
  @state == 'closed'
end

#closed_atObject



113
114
115
116
# File 'lib/trellohub/form.rb', line 113

def closed_at
  return @issue_closed_at if @imported_from == :issue
  @closed_at ||= card_closed_at || card_created_at
end

#created_atObject



104
105
106
# File 'lib/trellohub/form.rb', line 104

def created_at
  @created_at ||= send(:"#{@imported_from}_#{__method__}")
end

#open?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/trellohub/form.rb', line 118

def open?
  @state == 'open'
end

#own_keyObject



126
127
128
129
130
131
132
# File 'lib/trellohub/form.rb', line 126

def own_key
  if @imported_from == :issue
    @key
  else
    @card_shortLink
  end
end

#to_hashObject



134
135
136
137
138
139
140
# File 'lib/trellohub/form.rb', line 134

def to_hash
  Hash[instance_variables.map { |variable|
    next if variable.is_a?(Sawyer::Resource)
    variable_no_at = variable.to_s.gsub('@', '')
    [variable_no_at.to_sym, instance_variable_get(:"#{variable}")]
  }.compact]
end

#updated_atObject



108
109
110
111
# File 'lib/trellohub/form.rb', line 108

def updated_at
  return @issue_updated_at if @imported_from == :issue
  @updated_at ||= card_updated_at || card_created_at
end