Class: Cigale::Exts

Inherits:
Object
  • Object
show all
Defined in:
lib/cigale/exts.rb

Direct Known Subclasses

CLI, MacroContext

Instance Method Summary collapse

Instance Method Details

#asplode(node) ⇒ Object

Given ‘=> “b”`, returns [“a”, “b”]. Given `“a”`, returns [“a”, {}]



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cigale/exts.rb', line 24

def asplode(node)
  type = nil
  spec = {}

  case node
  when Hash
    type, spec = first_pair(node)
  when String
    type = node
  else
    raise "Invalid markup: #{node.inspect}"
  end

  return type, spec
end

#boolp(val, default) ⇒ Object

Give a default to a boolean parameter (val || default) doesn’t work because val may be false instead of nil



67
68
69
70
71
72
73
# File 'lib/cigale/exts.rb', line 67

def boolp (val, default)
  if val.nil?
    default
  else
    val
  end
end

#dig(h, dotted_path) ⇒ Object

Lets you do ‘dig(hash, “a.b.c”)` instead of `hash[“b“]`



7
8
9
10
11
12
13
14
15
# File 'lib/cigale/exts.rb', line 7

def dig(h, dotted_path)
  parts = dotted_path.split '.', 2
  match = h[parts[0]]
  if !parts[1] or match.nil?
    return match
  else
    return dig(match, parts[1])
  end
end

#first_pair(h) ⇒ Object

Given => b, :c => d, :e => f, returns [:a, b]



18
19
20
# File 'lib/cigale/exts.rb', line 18

def first_pair(h)
  h.each_pair.next
end

#toa(input) ⇒ Object

Make sure something is an array



41
42
43
44
45
46
47
48
49
50
# File 'lib/cigale/exts.rb', line 41

def toa (input)
  case input
  when Array
    return input
  when nil
    return []
  else
    return [input]
  end
end

#toh(input) ⇒ Object

Make sure something is a hash



53
54
55
56
57
58
59
60
61
62
# File 'lib/cigale/exts.rb', line 53

def toh (input)
  case input
  when Hash
    return input
  when nil
    return {}
  else
    raise "Can't toh {input.inspect}"
  end
end