Class: Hash

Inherits:
Object show all
Defined in:
lib/origen/core_ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#filter(filter) ⇒ Object

Filter a hash by a key filter of various types


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/origen/core_ext/hash.rb', line 26

def filter(filter)
  filtered_hash = {}
  select_logic = case filter
    when String then 'k[Regexp.new(filter)]'
    when (Fixnum || Integer || Float || Numeric) then "k[Regexp.new('#{filter}')]"
    when Regexp then 'k[filter]'
    when Symbol then 'k == filter'
    when NilClass then true
    else true
  end
  # rubocop:disable UnusedBlockArgument
  filtered_hash = select do |k, v|
    [TrueClass, FalseClass].include?(select_logic.class) ? select_logic : !!eval(select_logic)
  end
  filtered_hash
end

#idsObject


4
5
6
# File 'lib/origen/core_ext/hash.rb', line 4

def ids
  keys
end

#intersect?(hash) ⇒ Boolean

Boolean method to check if self and another hash have intersecting keys

Returns:

  • (Boolean)

16
17
18
# File 'lib/origen/core_ext/hash.rb', line 16

def intersect?(hash)
  (keys.to_set & hash.keys.to_set).empty? ? false : true
end

#intersections(hash) ⇒ Object

Returns a hash containing the intersecting keys between self and another hash


21
22
23
# File 'lib/origen/core_ext/hash.rb', line 21

def intersections(hash)
  (keys.to_set & hash.keys.to_set).to_a
end

#longest_keyObject

Returns the longest key as measured by String#length


44
45
46
# File 'lib/origen/core_ext/hash.rb', line 44

def longest_key
  keys.map(&:to_s).max_by(&:length)
end

#longest_valueObject

Returns the longest key as measured by String#length


49
50
51
# File 'lib/origen/core_ext/hash.rb', line 49

def longest_value
  values.map(&:to_s).max_by(&:length)
end

#recursive_find_by_key(key) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/origen/core_ext/hash.rb', line 53

def recursive_find_by_key(key)
  search_results = {} # Used to store results when key is a Regexp
  # Create a stack of hashes to search through for the needle which
  # is initially this hash
  stack = [self]
  # So long as there are more haystacks to search...
  while (to_search = stack.pop)
    # ...keep searching for this particular key...
    to_search.each do |k, v|
      # If this value can be recursively searched...
      if v.respond_to?(:recursive_find_by_key)
        # ...push that on to the list of places to search.
        stack << v
      elsif key.is_a? Regexp
        search_results[k] = v if key.match(k.to_s)
      else
        return v if (k == key)
      end
    end
  end
  if search_results.empty?
    return nil
  elsif search_results.size == 1
    return search_results.values.first
  else
    return search_results
  end
end

#update_common(hash) ⇒ Object

Only updates the common keys that exist in self


9
10
11
12
13
# File 'lib/origen/core_ext/hash.rb', line 9

def update_common(hash)
  each_key do |k|
    self[k] = hash[k] if hash.key? k
  end
end