Module: UmbrellioUtils::Misc

Extended by:
Misc
Included in:
Misc
Defined in:
lib/umbrellio_utils/misc.rb

Defined Under Namespace

Classes: StrictHash

Instance Method Summary collapse

Instance Method Details

#build_infinite_hashHash

Builds empty hash which recursively returns empty hash, if key is not found. Also note, that this hash and all subhashes has set #default_proc. To reset this attribute use #reset_defaults_for_hash

Examples:

Dig to key

h = UmbrellioUtils::Misc.build_infinite_hash => {}
h.dig(:kek, :pek) => {}
h => { kek: { pek: {} } }

Returns:

  • (Hash)

    empty infinite hash.



48
49
50
# File 'lib/umbrellio_utils/misc.rb', line 48

def build_infinite_hash
  Hash.new { |hash, key| hash[key] = Hash.new(&hash.default_proc) }
end

#merge_ranges(*ranges) ⇒ Object

Ranges go from high to low priority



31
32
33
34
# File 'lib/umbrellio_utils/misc.rb', line 31

def merge_ranges(*ranges)
  ranges = ranges.map { |x| x.present? && x.size == 2 ? x : [nil, nil] }
  ranges.first.zip(*ranges[1..]).map { |x| x.find(&:present?) }
end

#normalize_bank_name(name) ⇒ Object

needs any_ascii gem to work



73
74
75
76
77
78
79
80
# File 'lib/umbrellio_utils/misc.rb', line 73

def normalize_bank_name(name)
  result = AnyAscii.transliterate(name).gsub(/[^A-Za-z0-9]+/, " ").upcase.squish
  result.sub!(/\ATHE\s+/, "")
  result.sub!(/\bLIMITED\b/, "LTD")
  result.sub!(/\bCOMPANY\b/, "CO")
  result.sub!(/\bCORPORATION\b/, "CORP")
  result
end

#reset_defaults_for_hash(hash) ⇒ Hash

Deeply sets #default and #default_proc values to nil.

Parameters:

  • hash (Hash)

    hash for which you want to reset defaults.

Returns:

  • (Hash)

    reset hash.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/umbrellio_utils/misc.rb', line 59

def reset_defaults_for_hash(hash)
  hash.dup.tap do |dup_hash|
    dup_hash.default = nil
    dup_hash.default_proc = nil

    dup_hash.transform_values! do |obj|
      next obj.deep_dup unless obj.is_a?(Hash)

      reset_defaults_for_hash(obj)
    end
  end
end

#table_sync(scope, delay: 1, routing_key: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/umbrellio_utils/misc.rb', line 12

def table_sync(scope, delay: 1, routing_key: nil)
  scope.in_batches do |batch|
    batch_for_sync = batch.all.reject { |model| model.try(:skip_table_sync?) }
    next if batch_for_sync.empty?

    model_class = batch_for_sync.first.class.name
    TableSync::Publishing::Batch.new(
      object_class: model_class,
      original_attributes: batch_for_sync.map(&:values),
      routing_key:,
    ).publish_now

    sleep delay
  end
end