Class: Contributions::StringUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/contributions/string_utils.rb

Class Method Summary collapse

Class Method Details

.practically_empty?(arg) ⇒ Boolean

Internal: Determine whether a string has any content.

Examples:

StringUtils.practically_empty?('')
# => true
StringUtils.practically_empty?("\n\n")
# => true
StringUtils.practically_empty?("a\n")
# => false

Returns a Boolean.

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
102
# File 'lib/contributions/string_utils.rb', line 94

def self.practically_empty?(arg)
  if arg.empty?
    return true
  elsif !arg.match /\w/
    return true
  else
    return false
  end
end

.remove_empty(array) ⇒ Object

Internal: Remove any empty arrays after a split.

array - an array of Strings.

Returns an Array of Strings (modified)



78
79
80
# File 'lib/contributions/string_utils.rb', line 78

def self.remove_empty(array)
  array.delete_if { |a| self.practically_empty?(a[0]) && a[1].nil? }
end

.short_dates(hash) ⇒ Object

Internal: Convert date format to a simpler one.

hash - a hash with a :date key

Returns a Hash.



126
127
128
129
130
131
# File 'lib/contributions/string_utils.rb', line 126

def self.short_dates(hash)
  old_date = hash[:date]
  hash[:date] = old_date.match(/(\d{4}-\d{2}-\d{2})/)[1]

  hash
end

.split!(string, separator) ⇒ Object

Public: Split the string on the give separator.

separator - the character(s) on which to split the string.

Returns a modified version of the string.



69
70
71
# File 'lib/contributions/string_utils.rb', line 69

def self.split!(string, separator)
  string.split(separator)
end

.string_to_hash(string, keys, separator, *ending) ⇒ Object

Public: Read a long string of commit data and turn it into a hash.

string - a string of commit data. separator - the separator between values in an entry. ending - the separator between entries. keys - an Array of keys for the final hash.

Returns a Hash.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/contributions/string_utils.rb', line 13

def self.string_to_hash(string, keys, separator, *ending)
  s = string.dup
  s_as_array = ending.empty? ? [s] : self.split!(s, ending[0])

  s_as_array.map! { |e| self.split!(e, separator) }
  self.remove_empty(s_as_array)

  s_as_array.map! do |e|
    e.map! { |line| line.strip }
    self.zip_to_hash(keys, e)
  end

  s_as_array.map do |e|
    self.short_dates(e)
  end

  # s_as_array.map do |e|
  #   self.zip_to_hash(keys, e)
  # end


  # self.split!(s, ending).each do |e|
  #   self.remove_empty(self.split!(e, separator))
  # end

  # Now we need the zip move, then to a hash, then replace nils with
  # ''

  # s = string.dup
  # array = string.split()
  # small_arrays = array.map { |e| e.split(separator).map { |l| l.strip } }
  # small_arrays.delete_if { |a| a[0] == "" and a[1] == nil }
  # small_arrays.map! { |a| [:sha, :date, :subject, :body].zip a }
  # small_arrays




  # results = []
  # s = string.dup
  # s.gsub!(/(\d{4}-\d{2}-\d{2})/) { |f| " BREAK " + f }
  # s = s.split(" BREAK ")
  # s.delete_if { |l| l.empty? }
  # s.each do |line|
  #   m = /(?<date>\d{4}-\d{2}-\d{2})/.match line
  #   results.push m["date"]
  # end

  # results
end

.zip_to_hash(keys, values) ⇒ Object

Internal: Convert a pair of arrays into a hash with the first as keys.

keys - an Array of keys. values - an Array of values.

Returns a Hash.



111
112
113
114
115
116
117
118
119
# File 'lib/contributions/string_utils.rb', line 111

def self.zip_to_hash(keys, values)
  value = Hash.new
  zipped = keys.zip values
  zipped.each do |pair|
    value[pair.first] = pair.last || ''
  end

  value
end