Module: GClouder::Helpers

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



5
6
7
# File 'lib/gclouder/helpers.rb', line 5

def self.included(klass)
  klass.extend Helpers
end

Instance Method Details

#hash_to_args(hash) ⇒ Object

Raises:

  • (StandardError)


9
10
11
12
13
14
15
# 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) ⇒ Boolean

Returns:

Raises:

  • (StandardError)


54
55
56
57
# 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) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Object

Raises:

  • (StandardError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Boolean

Returns:



32
33
34
35
36
37
# File 'lib/gclouder/helpers.rb', line 32

def valid_json?(object)
  JSON.parse(object.to_s)
  return true
rescue JSON::ParserError
  return false
end