Module: Collapsium::PrototypeMatch
- Included in:
- UberHash
- Defined in:
- lib/collapsium/prototype_match.rb
Overview
Provides prototype matching for Hashes. See #prototype_match
Instance Method Summary collapse
-
#prototype_match(prototype, strict = false) ⇒ Boolean
Given a prototype Hash, returns true if (recursively): - this hash contains all the prototype’s keys, and - this hash contains all the prototype’s values Note that this is not the same as equality.
Instance Method Details
#prototype_match(prototype, strict = false) ⇒ Boolean
Given a prototype Hash, returns true if (recursively):
-
this hash contains all the prototype’s keys, and
-
this hash contains all the prototype’s values
Note that this is not the same as equality. If the prototype provides a nil value for any key, then any value in this Hash is considered to be valid.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/collapsium/prototype_match.rb', line 25 def prototype_match(prototype, strict = false) # Prototype contains keys not in the Hash,so that's a failure. if not (prototype.keys - keys).empty? return false end # In strict evaluation, the Hash may also not contain keys that are not # in the prototoype. if strict and not (keys - prototype.keys).empty? return false end # Now we have to examine the prototype's values. prototype.each do |key, value| # We can skip any nil values in the prototype. They exist only to ensure # the key is present. if value.nil? next end # If the prototype value is a Hash, then the Hash value also has to be, # and we have to recurse into this Hash. if value.is_a?(Hash) if not self[key].is_a?(Hash) return false end self[key].extend(PrototypeMatch) if not self[key].prototype_match(value) return false end next end # Otherwise the prototype value must be equal to the Hash's value if self[key] != value return false end end # All other cases must be true. return true end |