Module: Komic::Utils

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

Instance Method Summary collapse

Instance Method Details

#create_progress(title, total) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/komic/utils.rb', line 51

def create_progress(title, total)
  # green background
  color_code = "\e[0m\e[32m\e[7m\e[1m"
  reset_code = "\e[0m"
  progress_status = "#{color_code} %p%% #{reset_code}"

  ProgressBar.create( :format         => "%a %bᗧ%i #{progress_status} %t",
                      :title          => title,
                      :progress_mark  => ' ',
                      :remainder_mark => '',
                      :total => total,
                      :starting_at    => 0 )
end

#deep_merge_hashes(master_hash, other_hash) ⇒ Object

Merges a master hash with another hash, recursively.

master_hash - the "parent" hash whose values will be overridden other_hash - the other hash whose values will be persisted after the merge

This code was lovingly stolen from some random gem: http://gemjack.com/gems/tartan-0.1.1/classes/Hash.html

Thanks to whoever made it.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/komic/utils.rb', line 14

def deep_merge_hashes(master_hash, other_hash)
  target = master_hash.dup

  other_hash.each_key do |key|
    if other_hash[key].is_a? Hash and target[key].is_a? Hash
      target[key] = Utils.deep_merge_hashes(target[key], other_hash[key])
      next
    end

    target[key] = other_hash[key]
  end

  target
end

#get_relative_path(path, root) ⇒ Object



44
45
46
47
48
49
# File 'lib/komic/utils.rb', line 44

def get_relative_path(path, root)
  File.join('./',
    Pathname.new(path).relative_path_from(
      Pathname.new(root)
    ))
end

#parse_size(size) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/komic/utils.rb', line 29

def parse_size(size)
  width_range, height_range = size.split('x')
  width, height = [width_range, height_range].map do |range|
    unless range.nil?
      min, max = range.split('-')
      r = range.to_i
      unless max.nil?
        r = Random.rand(min.to_i...max.to_i)
      end
      r
    end
  end
  return { width: width, height: height }
end