Module: Txgh::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/txgh/utils.rb

Instance Method Summary collapse

Instance Method Details

#absolute_branch(branch) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/txgh/utils.rb', line 10

def absolute_branch(branch)
  return unless branch
  if is_tag?(branch)
    branch
  elsif branch.include?('heads/')
    branch
  else
    "heads/#{branch}"
  end
end

#booleanize(obj) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/txgh/utils.rb', line 49

def booleanize(obj)
  case obj
    when String
      obj.downcase == 'true'
    when TrueClass, FalseClass
      obj
  end
end

#branches_equal?(first, second) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/txgh/utils.rb', line 21

def branches_equal?(first, second)
  absolute_branch(first) == absolute_branch(second)
end

#index_on(key, arr) ⇒ Object

Builds a hash from an array of hashes using a common key present in all the elements. For example, consider this array of hashes:

arr = [

{ 'param1' => 'dogs', 'param2' => 'hairy' },
{ 'param1' => 'cats', 'param2' => 'fuzzy' }

]

calling index_on(‘param1’, arr) returns:

{

'dogs' => { 'param1' => 'dogs', 'param2' => 'hairy' },
'cats' => { 'param1' => 'cats', 'param2' => 'fuzzy' }

}



43
44
45
46
47
# File 'lib/txgh/utils.rb', line 43

def index_on(key, arr)
  arr.each_with_object({}) do |hash, ret|
    ret[hash[key]] = hash
  end
end

#is_tag?(ref) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/txgh/utils.rb', line 25

def is_tag?(ref)
  ref.include?('tags/')
end

#slugify(text) ⇒ Object



3
4
5
6
7
8
# File 'lib/txgh/utils.rb', line 3

def slugify(text)
  text
    .gsub('/', '_')
    .gsub(/[^\w\s_-]/, '')
    .gsub(/[-\s]+/, '-')
end