Class: FlashFlow::Data::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/flash_flow/data/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remotes, config = nil) ⇒ Collection

Returns a new instance of Collection.



11
12
13
14
15
16
17
18
19
# File 'lib/flash_flow/data/collection.rb', line 11

def initialize(remotes, config=nil)
  @remotes = remotes
  @branches = {}

  if config && config['class'] && config['class']['name']
    collection_class = Object.const_get(config['class']['name'])
    @collection_instance = collection_class.new(config['class'])
  end
end

Instance Attribute Details

#branchesObject

Returns the value of attribute branches.



9
10
11
# File 'lib/flash_flow/data/collection.rb', line 9

def branches
  @branches
end

#remotesObject

Returns the value of attribute remotes.



9
10
11
# File 'lib/flash_flow/data/collection.rb', line 9

def remotes
  @remotes
end

Class Method Details

.branches_from_hash(hash) ⇒ Object



34
35
36
37
38
# File 'lib/flash_flow/data/collection.rb', line 34

def self.branches_from_hash(hash)
  hash.each do |key, val|
    hash[key] = val.is_a?(Branch) ? val : Branch.from_hash(val)
  end
end

.fetch(remotes, config = nil) ⇒ Object



21
22
23
24
25
# File 'lib/flash_flow/data/collection.rb', line 21

def self.fetch(remotes, config=nil)
  collection = new(remotes, config)
  collection.fetch
  collection
end

.from_hash(remotes, hash, collection_instance = nil) ⇒ Object



27
28
29
30
31
32
# File 'lib/flash_flow/data/collection.rb', line 27

def self.from_hash(remotes, hash, collection_instance=nil)
  collection = new(remotes)
  collection.branches = branches_from_hash(hash.dup)
  collection.instance_variable_set(:@collection_instance, collection_instance)
  collection
end

Instance Method Details

#add_story(remote, ref, story_id) ⇒ Object



159
160
161
162
163
164
165
166
# File 'lib/flash_flow/data/collection.rb', line 159

def add_story(remote, ref, story_id)
  branch = get(url_from_remote(remote), ref)
  branch.stories ||= []
  branch.stories << story_id

  @collection_instance.add_story(branch, story_id) if @collection_instance.respond_to?(:add_story)
  branch
end

#add_to_merge(remote, ref) ⇒ Object



123
124
125
126
127
128
# File 'lib/flash_flow/data/collection.rb', line 123

def add_to_merge(remote, ref)
  branch = record(remote, nil, ref)
  branch.current_record = true
  @collection_instance.add_to_merge(branch) if @collection_instance.respond_to?(:add_to_merge)
  branch
end


176
177
178
# File 'lib/flash_flow/data/collection.rb', line 176

def branch_link(branch)
  @collection_instance.branch_link(branch) if @collection_instance.respond_to?(:branch_link)
end

#can_ship?(branch) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/flash_flow/data/collection.rb', line 172

def can_ship?(branch)
  @collection_instance.respond_to?(:can_ship?) ? @collection_instance.can_ship?(branch) : true
end

#code_reviewed?(branch) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/flash_flow/data/collection.rb', line 168

def code_reviewed?(branch)
  @collection_instance.respond_to?(:code_reviewed?) ? @collection_instance.code_reviewed?(branch) : true
end

#current_branchesObject



89
90
91
# File 'lib/flash_flow/data/collection.rb', line 89

def current_branches
  to_a.select { |branch| branch.current_record }
end

#eachObject



85
86
87
# File 'lib/flash_flow/data/collection.rb', line 85

def each
  to_a.each
end

#failuresObject



97
98
99
# File 'lib/flash_flow/data/collection.rb', line 97

def failures
  current_branches.select { |branch| branch.fail? }
end

#fetchObject



109
110
111
112
113
114
115
# File 'lib/flash_flow/data/collection.rb', line 109

def fetch
  return unless @collection_instance.respond_to?(:fetch)

  @collection_instance.fetch.each do |b|
    update_or_add(b)
  end
end

#get(remote_url, ref) ⇒ Object



40
41
42
# File 'lib/flash_flow/data/collection.rb', line 40

def get(remote_url, ref)
  @branches[key(remote_url, ref)]
end

#mark_all_as_currentObject



117
118
119
120
121
# File 'lib/flash_flow/data/collection.rb', line 117

def mark_all_as_current
  @branches.each do |_, branch|
    branch.current_record = true
  end
end

#mark_deleted(branch) ⇒ Object



145
146
147
148
149
150
# File 'lib/flash_flow/data/collection.rb', line 145

def mark_deleted(branch)
  update_or_add(branch)
  branch.deleted!
  @collection_instance.mark_deleted(branch) if @collection_instance.respond_to?(:mark_deleted)
  branch
end

#mark_failure(branch, conflict_sha = nil) ⇒ Object



138
139
140
141
142
143
# File 'lib/flash_flow/data/collection.rb', line 138

def mark_failure(branch, conflict_sha=nil)
  update_or_add(branch)
  branch.fail!(conflict_sha)
  @collection_instance.mark_failure(branch) if @collection_instance.respond_to?(:mark_failure)
  branch
end

#mark_success(branch) ⇒ Object



152
153
154
155
156
157
# File 'lib/flash_flow/data/collection.rb', line 152

def mark_success(branch)
  update_or_add(branch)
  branch.success!
  @collection_instance.mark_success(branch) if @collection_instance.respond_to?(:mark_success)
  branch
end

#mergeableObject



93
94
95
# File 'lib/flash_flow/data/collection.rb', line 93

def mergeable
  current_branches.select { |branch| (branch.success? || branch.fail? || branch.unknown?) }
end

#removalsObject



105
106
107
# File 'lib/flash_flow/data/collection.rb', line 105

def removals
  to_a.select { |branch| branch.removed? }
end

#remove_from_merge(remote, ref) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/flash_flow/data/collection.rb', line 130

def remove_from_merge(remote, ref)
  branch = record(remote, nil, ref)
  branch.current_record = true
  branch.removed!
  @collection_instance.remove_from_merge(branch) if @collection_instance.respond_to?(:remove_from_merge)
  branch
end

#reverse_merge(old) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/flash_flow/data/collection.rb', line 53

def reverse_merge(old)
  merged_branches = @branches.dup

  merged_branches.each do |_, info|
    info.updated_at = Time.now
    info.created_at ||= Time.now
  end

  old.branches.each do |full_ref, info|
    if merged_branches.has_key?(full_ref)
      branch = merged_branches[full_ref]

      branch.created_at = info.created_at
      branch.resolutions = info.resolutions.to_h.merge(branch.resolutions.to_h)
      branch.stories = info.stories.to_a | merged_branches[full_ref].stories.to_a
      branch.merge_order ||= info.merge_order
      if branch.fail?
        branch.conflict_sha ||= info.conflict_sha
      end
    else
      merged_branches[full_ref] = info
      merged_branches[full_ref].status = nil
    end
  end

  self.class.from_hash(remotes, merged_branches, @collection_instance)
end

#set_resolutions(branch, resolutions) ⇒ Object



180
181
182
183
184
185
# File 'lib/flash_flow/data/collection.rb', line 180

def set_resolutions(branch, resolutions)
  update_or_add(branch)
  branch.set_resolutions(resolutions)
  @collection_instance.set_resolutions(branch) if @collection_instance.respond_to?(:set_resolutions)
  branch
end

#successesObject



101
102
103
# File 'lib/flash_flow/data/collection.rb', line 101

def successes
  current_branches.select { |branch| branch.success? }
end

#to_aObject



81
82
83
# File 'lib/flash_flow/data/collection.rb', line 81

def to_a
  @branches.values
end

#to_hashObject Also known as: to_h



44
45
46
47
48
49
50
# File 'lib/flash_flow/data/collection.rb', line 44

def to_hash
  {}.tap do |hash|
    @branches.each do |key, val|
      hash[key] = val.to_hash
    end
  end
end