Module: Interfacets::Shared::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/interfacets/shared/utils.rb

Instance Method Summary collapse

Instance Method Details

#blank?(str) ⇒ Boolean

ActiveSupport?

Returns:

  • (Boolean)


37
38
39
40
41
42
43
# File 'lib/interfacets/shared/utils.rb', line 37

def blank?(str)
  return true if str.nil?
  return str.empty? if str.is_a?(Array)
  return str.match(/\A\s*\z/) if str.is_a?(String)

  raise("unsupported blank check")
end

#facet_id_to_path(config:, facet:, id:) ⇒ Object



24
25
26
# File 'lib/interfacets/shared/utils.rb', line 24

def facet_id_to_path(config:, facet:, id:)
  [config.mount_point, facet, id].join("/")
end

#path_to_facet_id(config:, path:) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/interfacets/shared/utils.rb', line 28

def path_to_facet_id(config:, path:)
  parts = path.sub(/.*#{config.mount_point}/, "").split("/")
  id = parts.pop
  facet = parts.join("/")

  [facet, id]
end

#permissive_call(lambda) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/interfacets/shared/utils.rb', line 16

def permissive_call(lambda, ...)
  if lambda.arity.zero?
    lambda.call
  else
    lambda.call(...)
  end
end

#permissive_exec(receiver, &lambda) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/interfacets/shared/utils.rb', line 8

def permissive_exec(receiver, *, &lambda)
  if lambda.arity.zero?
    receiver.instance_exec(&lambda)
  else
    receiver.instance_exec(*, &lambda)
  end
end

#presence(obj) ⇒ Object



49
50
51
# File 'lib/interfacets/shared/utils.rb', line 49

def presence(obj)
  present?(obj) ? obj : nil
end

#present?(str) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/interfacets/shared/utils.rb', line 45

def present?(str)
  !blank?(str)
end