Class: CheckPlease::Diffs

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/check_please/diffs.rb

Overview

Custom collection class for Diff instances. Can retrieve members using indexes or paths.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(diff_list = nil, flags: {}) ⇒ Diffs

Returns a new instance of Diffs.



9
10
11
12
13
14
15
16
# File 'lib/check_please/diffs.rb', line 9

def initialize(diff_list = nil, flags: {})
  @flags = Flags.reify(flags)
  @list = []
  @hash = {}
  Array(diff_list).each do |diff|
    self << diff
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/check_please/diffs.rb', line 58

def method_missing(meth, *args, &blk)
  if formats.include?(meth.to_sym)
    CheckPlease::Printers.render(self, format: meth)
  else
    super
  end
end

Instance Attribute Details

#flagsObject (readonly)

Returns the value of attribute flags.



8
9
10
# File 'lib/check_please/diffs.rb', line 8

def flags
  @flags
end

Instance Method Details

#<<(diff) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/check_please/diffs.rb', line 31

def <<(diff)
  if flags.fail_fast && length > 0
    throw :max_diffs_reached
  end

  if (n = flags.max_diffs)
    # It seems no one can help me now / I'm in too deep, there's no way out
    throw :max_diffs_reached if length >= n
  end

  @list << diff
  @hash[diff.path] = diff
end

#[](key) ⇒ Object

this is probably a terrible idea, but this method:

  • treats integer keys as array-style positional indexes

  • treats string keys as path strings and does a hash-like lookup (raising if the path is not found)

(In my defense, I only did it to make the tests easier to write.)



23
24
25
26
27
28
29
# File 'lib/check_please/diffs.rb', line 23

def [](key)
  if key.is_a?(Integer)
    @list[key]
  else
    @hash.fetch(key)
  end
end

#dataObject



45
46
47
# File 'lib/check_please/diffs.rb', line 45

def data
  @list.map(&:attributes)
end

#filter_by_flags(flags) ⇒ Object



49
50
51
52
# File 'lib/check_please/diffs.rb', line 49

def filter_by_flags(flags)
  new_list = @list.reject { |diff| Path.new(diff.path).excluded?(flags) }
  self.class.new(new_list, flags: flags)
end

#formatsObject



66
67
68
# File 'lib/check_please/diffs.rb', line 66

def formats
  CheckPlease::Printers::FORMATS
end

#to_s(flags = {}) ⇒ Object



54
55
56
# File 'lib/check_please/diffs.rb', line 54

def to_s(flags = {})
  CheckPlease::Printers.render(self, flags)
end