Module: Txgh::Utils
Instance Method Summary collapse
- #absolute_branch(branch) ⇒ Object
- #booleanize(obj) ⇒ Object
- #branches_equal?(first, second) ⇒ Boolean
- #deep_symbolize_keys(obj) ⇒ Object
- #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
- #url_safe_relative_branch(branch) ⇒ 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
63 64 65 66 67 68 69 70 |
# File 'lib/txgh/utils.rb', line 63 def booleanize(obj) case obj when String obj.downcase == 'true' when TrueClass, FalseClass obj end end |
#branches_equal?(first, second) ⇒ Boolean
31 32 33 |
# File 'lib/txgh/utils.rb', line 31 def branches_equal?(first, second) absolute_branch(first) == absolute_branch(second) end |
#deep_symbolize_keys(obj) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/txgh/utils.rb', line 72 def deep_symbolize_keys(obj) case obj when Hash obj.each_with_object({}) do |(k, v), ret| ret[k.to_sym] = deep_symbolize_keys(v) end when Array obj.map do |elem| deep_symbolize_keys(elem) end else obj end end |
#git_hash_blob(str) ⇒ Object
39 40 41 |
# File 'lib/txgh/utils.rb', line 39 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' }
}
57 58 59 60 61 |
# File 'lib/txgh/utils.rb', line 57 def index_on(key, arr) arr.each_with_object({}) do |hash, ret| ret[hash[key]] = hash end end |
#is_tag?(ref) ⇒ Boolean
35 36 37 |
# File 'lib/txgh/utils.rb', line 35 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 |
#url_safe_relative_branch(branch) ⇒ Object
27 28 29 |
# File 'lib/txgh/utils.rb', line 27 def url_safe_relative_branch(branch) CGI.escape(relative_branch(branch)) end |