module GClouder::Helpers

Public Class Methods

included(klass) click to toggle source
# File lib/gclouder/helpers.rb, line 5
def self.included(klass)
  klass.extend Helpers
end

Public Instance Methods

hash_to_args(hash) click to toggle source
# File lib/gclouder/helpers.rb, line 9
def hash_to_args(hash)
  raise StandardError, "hash_to_args: input not a hash: #{hash}" unless hash.is_a?(Hash)
  hash.map { |param, value|
    next if param == "name"
    to_arg(param, value)
  }.join(" ")
end
module_exists?(name, base = self.class) click to toggle source
# File lib/gclouder/helpers.rb, line 54
def module_exists?(name, base = self.class)
  raise StandardError, "module name must be a string" unless name.is_a?(String)
  base.const_defined?(name) && base.const_get(name).instance_of?(::Module)
end
to_arg(param, value) click to toggle source
# File lib/gclouder/helpers.rb, line 17
def to_arg(param, value)
  param = param.tr("_", "-")

  value = case value
  when Boolean
    return value ? "--#{param}" : "--no-#{param}"
  when Array
    value.join(",")
  else
    value
  end

  "--#{param}='#{value}'"
end
to_deep_merge_hash(hash, hash_type = DeepMergeHash) click to toggle source
# File lib/gclouder/helpers.rb, line 39
def to_deep_merge_hash(hash, hash_type = DeepMergeHash)
  raise StandardError, "to_deep_merge_hash: argument must be a hash" unless hash.is_a?(Hash)

  hash.each do |k,v|
    case v
    when Hash
      hash_to_deep_merge_hash(hash, k, v, hash_type)
    when Array
      array_to_deep_merge_hash(v, hash_type)
    end
  end

  hash_type.new(hash)
end
valid_json?(object) click to toggle source
# File lib/gclouder/helpers.rb, line 32
def valid_json?(object)
  JSON.parse(object.to_s)
  return true
rescue JSON::ParserError
  return false
end

Private Instance Methods

array_to_deep_merge_hash(array, hash_type) click to toggle source
# File lib/gclouder/helpers.rb, line 66
def array_to_deep_merge_hash(array, hash_type)
  array.each_with_index do |e,i|
    case e
    when Hash
      hash_to_deep_merge_hash(array, i, e, hash_type)
    when Array
      array_to_deep_merge_hash(array, hash_type)
    end
  end
end
hash_to_deep_merge_hash(obj, index, hash, hash_type) click to toggle source
# File lib/gclouder/helpers.rb, line 61
def hash_to_deep_merge_hash(obj, index, hash, hash_type)
  obj[index] = hash_type.new(hash)
  to_deep_merge_hash(obj[index], hash_type)
end