Sha256: 4c7849de08b3a34e7f0024a1c179171249951030c7eb842f301edcdde1f1159b

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

module Komic
  module Utils extend self
    # 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.
    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

    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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
komic-cli-0.1.2 lib/komic/utils.rb
komic-cli-0.1.1 lib/komic/utils.rb