Module: Txgh::Utils
Instance Method Summary collapse
- #absolute_branch(branch) ⇒ Object
- #booleanize(obj) ⇒ Object
- #branches_equal?(first, second) ⇒ Boolean
- #git_hash_blob(str) ⇒ Object
-
#index_on(key, arr) ⇒ Object
Builds a hash from an array of hashes using a common key present in all the elements.
- #is_tag?(ref) ⇒ Boolean
- #relative_branch(branch) ⇒ Object
- #slugify(text) ⇒ Object
Instance Method Details
#absolute_branch(branch) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/txgh/utils.rb', line 12 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
59 60 61 62 63 64 65 66 |
# File 'lib/txgh/utils.rb', line 59 def booleanize(obj) case obj when String obj.downcase == 'true' when TrueClass, FalseClass obj end end |
#branches_equal?(first, second) ⇒ Boolean
27 28 29 |
# File 'lib/txgh/utils.rb', line 27 def branches_equal?(first, second) absolute_branch(first) == absolute_branch(second) end |
#git_hash_blob(str) ⇒ Object
35 36 37 |
# File 'lib/txgh/utils.rb', line 35 def git_hash_blob(str) Digest::SHA1.hexdigest("blob #{str.bytesize}\0#{str}") 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' }
}
53 54 55 56 57 |
# File 'lib/txgh/utils.rb', line 53 def index_on(key, arr) arr.each_with_object({}) do |hash, ret| ret[hash[key]] = hash end end |
#is_tag?(ref) ⇒ Boolean
31 32 33 |
# File 'lib/txgh/utils.rb', line 31 def is_tag?(ref) ref.include?('tags/') end |
#relative_branch(branch) ⇒ Object
23 24 25 |
# File 'lib/txgh/utils.rb', line 23 def relative_branch(branch) branch.strip.sub(/\A(heads|tags)\//, '') end |
#slugify(text) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/txgh/utils.rb', line 5 def slugify(text) text .gsub('/', '_') .gsub(/[^\w\s_-]/, '') .gsub(/[-\s]+/, '-') end |