Module: Xqsr3::HashUtilities::KeyMatching

Defined in:
lib/xqsr3/hash_utilities/key_matching.rb

Overview

+include+-able module that provides ::has_match?, #has_match?, ::match, and #match methods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.do_has_match_(h, re, **options) ⇒ Object

:nodoc:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/xqsr3/hash_utilities/key_matching.rb', line 100

def self.do_has_match_ h, re, **options # :nodoc:

  ::Xqsr3::Quality::ParameterChecking.check_parameter h, 'h', responds_to: [ :[], :has_key?, :each ]

  return true if h.has_key? re

  case re
  when ::Regexp

    h.each do |k, v|

      case k
      when ::Regexp

        next
      else

        return true if k.to_s =~ re
      end
    end
  else

    h.each do |k, v|

      case k
      when ::Regexp

        return true if re.to_s =~ k
      else

        next
      end
    end
  end

  false
end

.do_match_(h, re, **options) ⇒ Object

:nodoc:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/xqsr3/hash_utilities/key_matching.rb', line 62

def self.do_match_ h, re, **options # :nodoc:

  ::Xqsr3::Quality::ParameterChecking.check_parameter h, 'h', responds_to: [ :[], :has_key?, :each ]

  return h[re] if h.has_key? re

  case re
  when ::Regexp

    h.each do |k, v|

      case k
      when ::Regexp

        next
      else

        return v if k.to_s =~ re
      end
    end
  else

    h.each do |k, v|

      case k
      when ::Regexp

        return v if re.to_s =~ k
      else

        next
      end
    end
  end

  nil
end

.has_match?(h, re, **options) ⇒ Boolean

Returns true if the hash h contains a key object that matches the given re, according to the given options

Returns:

  • (Boolean)


149
150
151
152
# File 'lib/xqsr3/hash_utilities/key_matching.rb', line 149

def self.has_match? h, re, **options

  Xqsr3::HashUtilities::KeyMatching.do_has_match_ h, re, **options
end

.match(h, re, **options) ⇒ Object

Retrieves the value object corresponding to the first key object that matches the given re, in the hash h, according to the given options.



142
143
144
145
# File 'lib/xqsr3/hash_utilities/key_matching.rb', line 142

def self.match h, re, **options

  Xqsr3::HashUtilities::KeyMatching.do_match_ h, re, **options
end

Instance Method Details

#has_match?(h, re, **options) ⇒ Boolean

Returns true if the hash h contains a key object that matches the given re, according to the given options

Returns:

  • (Boolean)


164
165
166
167
# File 'lib/xqsr3/hash_utilities/key_matching.rb', line 164

def has_match? h, re, **options

  Xqsr3::HashUtilities::KeyMatching.do_has_match_ h, re, **options
end

#match(h, re, **options) ⇒ Object

Retrieves the value object corresponding to the first key object that matches the given re, in the hash h, according to the given options.



157
158
159
160
# File 'lib/xqsr3/hash_utilities/key_matching.rb', line 157

def match h, re, **options

  Xqsr3::HashUtilities::KeyMatching.do_match_ h, re, **options
end