Module: LB::Persistence::Functions

Extended by:
Transproc::Composer, Transproc::Registry
Defined in:
lib/lb/persistence/functions.rb

Overview

LB transproc functions

Class Method Summary collapse

Class Method Details

.debug_p(value) ⇒ Object



18
19
20
# File 'lib/lb/persistence/functions.rb', line 18

def debug_p(value)
  p value
end

.debug_pp(value) ⇒ Object



22
23
24
# File 'lib/lb/persistence/functions.rb', line 22

def debug_pp(value)
  pp value
end

.debug_puts(value) ⇒ Object



26
27
28
# File 'lib/lb/persistence/functions.rb', line 26

def debug_puts(value)
  puts value
end

.empty_hash?(hash) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/lb/persistence/functions.rb', line 93

def empty_hash?(hash)
  hash.is_a?(Hash) && hash.values.all?(&:nil?)
end

.inject_array(array, model) ⇒ Object Also known as: model



79
80
81
# File 'lib/lb/persistence/functions.rb', line 79

def inject_array(array, model)
  t(:map_array, t(:constructor_inject, model)).call(array)
end

.inject_if_given(value, model) ⇒ Object



74
75
76
77
# File 'lib/lb/persistence/functions.rb', line 74

def inject_if_given(value, model)
  t(:guard, ->(_v) { !model.nil? },
    t(:constructor_inject, model)).call(value)
end

.reject_array(array, function) ⇒ Object



89
90
91
# File 'lib/lb/persistence/functions.rb', line 89

def reject_array(array, function)
  Array(array).reject { |value| function[value] }
end

.remove_key_prefix(keys, prefix) ⇒ Object



70
71
72
# File 'lib/lb/persistence/functions.rb', line 70

def remove_key_prefix(keys, prefix)
  t(:map_keys, t(:remove_prefix, prefix)).call(keys)
end

.remove_key_prefix_inject(keys, prefix, model = nil) ⇒ Object



63
64
65
66
67
68
# File 'lib/lb/persistence/functions.rb', line 63

def remove_key_prefix_inject(keys, prefix, model = nil)
  compose do |ops|
    ops << t(:remove_key_prefix, prefix)
    ops << t(:inject_if_given, model)
  end.call(keys)
end

.remove_key_prefix_inject_for(hash, key, prefix, model = nil) ⇒ Object



51
52
53
54
# File 'lib/lb/persistence/functions.rb', line 51

def remove_key_prefix_inject_for(hash, key, prefix, model = nil)
  t(:map_value, key,
    t(:remove_key_prefix_inject_value, prefix, model)).call(hash)
end

.remove_key_prefix_inject_hash_for(hash, key, prefix, model = nil) ⇒ Object



84
85
86
87
# File 'lib/lb/persistence/functions.rb', line 84

def remove_key_prefix_inject_hash_for(hash, key, prefix, model = nil)
  t(:map_value, key,
    t(:remove_key_prefix_inject, prefix, model)).call(hash)
end

.remove_key_prefix_inject_value(value, prefix, model = nil) ⇒ Object



56
57
58
59
60
61
# File 'lib/lb/persistence/functions.rb', line 56

def remove_key_prefix_inject_value(value, prefix, model = nil)
  compose do |ops|
    ops << t(:reject_array, t(:empty_hash?))
    ops << t(:map_array, t(:remove_key_prefix_inject, prefix, model))
  end.call(value)
end

.remove_prefix(key, prefix) ⇒ Object



30
31
32
# File 'lib/lb/persistence/functions.rb', line 30

def remove_prefix(key, prefix)
  remove_prefix_from_key(key.to_s, prefix).to_sym
end

.remove_prefix_from_key(key, prefix) ⇒ Object



34
35
36
# File 'lib/lb/persistence/functions.rb', line 34

def remove_prefix_from_key(key, prefix)
  key.start_with?(prefix) ? key.gsub(/^#{prefix}/, '') : key
end