Class: Helpers

Inherits:
Object
  • Object
show all
Defined in:
lib/danarchy_sys/helpers.rb

Overview

Routine methods for DanarchySys

Class Method Summary collapse

Class Method Details

.array_to_numhash(array) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/danarchy_sys/helpers.rb', line 4

def self.array_to_numhash(array)
  numbered_hash = {}
  
  array.each.with_index(1) do |item, id|
    numbered_hash[id] = item
  end

  numbered_hash
end

.check_nested_hash_value(hash, key, value) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/danarchy_sys/helpers.rb', line 56

def self.check_nested_hash_value(hash, key, value)
  check = false

  hash.each_value do |val|
    check = true if val[key].end_with?(value)
  end

  check
end

.hash_largest_key(hash) ⇒ Object



40
41
42
# File 'lib/danarchy_sys/helpers.rb', line 40

def self.hash_largest_key(hash)
  hash.keys.map(&:to_s).max_by(&:size)
end

.hash_largest_nested_key(hash) ⇒ Object



48
49
50
# File 'lib/danarchy_sys/helpers.rb', line 48

def self.hash_largest_nested_key(hash)
  hash.each_value.flat_map(&:keys).max_by(&:size)
end

.hash_largest_nested_value(hash) ⇒ Object



52
53
54
# File 'lib/danarchy_sys/helpers.rb', line 52

def self.hash_largest_nested_value(hash)
  hash.each_value.flat_map(&:values).max_by(&:size)
end

.hash_largest_value(hash) ⇒ Object



44
45
46
# File 'lib/danarchy_sys/helpers.rb', line 44

def self.hash_largest_value(hash)
  hash.values.map(&:to_s).max_by(&:size)
end

.hash_to_numhash(hash) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/danarchy_sys/helpers.rb', line 14

def self.hash_to_numhash(hash)
  numbered_hash = {}

  hash.map.with_index(1) do | (k, v), index |
    numbered_hash[index] = {k => v}
  end

  numbered_hash
end

.object_to_hash(object) ⇒ Object



24
25
26
27
# File 'lib/danarchy_sys/helpers.rb', line 24

def self.object_to_hash(object)
  return nil if !object
  object.all_attributes
end

.objects_to_numhash(objects) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/danarchy_sys/helpers.rb', line 29

def self.objects_to_numhash(objects)
  return nil if !objects
  numbered_object_hash = {}

  objects.map.with_index(1) do | obj, index |
    numbered_object_hash[index] = obj.all_attributes
  end

  numbered_object_hash
end