Class: Marathon::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/marathon/util.rb

Overview

Some helper things.

Class Method Summary collapse

Class Method Details

.add_choice(opts, name, value, allowed, nil_allowed = true) ⇒ Object

Check parameter and add it to hash if not nil. opts: hash of parameters name: parameter’s name value: parameter’s value allowed: array of allowd values nil_allowed: allow nil values



36
37
38
39
# File 'lib/marathon/util.rb', line 36

def add_choice(opts, name, value, allowed, nil_allowed = true)
  validate_choice(name, value, allowed, nil_allowed)
  opts[name] = value if value
end

.hmap!(hash, &block) ⇒ Object

Implement map! on a hash



92
93
94
95
96
97
98
99
100
# File 'lib/marathon/util.rb', line 92

def hmap!(hash, &block)
  hash.keys.each do |key|
    new_hash = block.call(key, hash[key])
    new_key = new_hash.keys.first
    hash[new_key] = new_hash[new_key]
    hash.delete(key) unless key == new_key
  end
  hash
end

.items_to_pretty_s(item) ⇒ Object

Stringify an item or an array of items.



81
82
83
84
85
86
87
88
89
# File 'lib/marathon/util.rb', line 81

def items_to_pretty_s(item)
  if item.nil?
    nil
  elsif item.is_a?(Array)
    item.map { |e| e.to_pretty_s }.join(',')
  else
    item.to_pretty_s
  end
end

.keywordize_hash!(hash, ignore_keys = [:env]) ⇒ Object

Swap keys of the hash against their symbols. hash: the hash ignore_keys: don’t keywordize hashes under theses keys



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/marathon/util.rb', line 44

def keywordize_hash!(hash, ignore_keys = [:env])
  if hash.is_a?(Hash)
    hmap!(hash) do |k, v|
      key = k.to_sym
      if ignore_keys.include?(key) and v.is_a?(Hash)
        {key => v}
      else
        {key => keywordize_hash!(v)}
      end
    end
  elsif hash.is_a?(Array)
    hash.map! { |e| keywordize_hash!(e) }
  end
  hash
end

.merge_keywordized_hash(h1, h2) ⇒ Object

Merge two hashes but keywordize both.



76
77
78
# File 'lib/marathon/util.rb', line 76

def merge_keywordized_hash(h1, h2)
  keywordize_hash!(h1).merge(keywordize_hash!(h2))
end

.remove_keys(hash, keys) ⇒ Object

Remove keys from hash and all it’s sub hashes. hash: the hash keys: list of keys to remove



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/marathon/util.rb', line 63

def remove_keys(hash, keys)
  if hash.is_a?(Hash)
    new_hash = {}
    hash.each { |k, v| new_hash[k] = remove_keys(v, keys) unless keys.include?(k) }
    new_hash
  elsif hash.is_a?(Array)
    hash.map { |e| remove_keys(e, keys) }
  else
    hash
  end
end

.validate_choice(name, value, allowed, nil_allowed = true) ⇒ Object

Checks if parameter is of allowed value. name: parameter’s name value: parameter’s value allowed: array of allowd values nil_allowed: allow nil values



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/marathon/util.rb', line 10

def validate_choice(name, value, allowed, nil_allowed = true)
  value = value[name] if value.is_a?(Hash)
  if value.nil?
    unless nil_allowed
      raise Marathon::Error::ArgumentError, "#{name} must not be nil"
    end
  else
    # value is not nil
    unless allowed.include?(value)
      if nil_allowed
        raise Marathon::Error::ArgumentError,
              "#{name} must be one of #{allowed.join(', ')} or nil, but is '#{value}'"
      else
        raise Marathon::Error::ArgumentError,
              "#{name} must be one of #{allowed.join(', ')} or nil, but is '#{value}'"
      end
    end
  end
end