Class: Safer::HashProtocol::HasKey

Inherits:
Base
  • Object
show all
Defined in:
lib/safer/hashprotocol.rb

Overview

Terminal Safer::HashProtocol implementation, checks that a key exists in a Hash.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#self

Constructor Details

#initialize(key) ⇒ HasKey

The match operation will check that key exists in the Hash. key.inspect is used for the description.



152
153
154
155
# File 'lib/safer/hashprotocol.rb', line 152

def initialize(key)
  super(key.inspect)
  self.safer_hashprotocol_haskey__key = key
end

Class Method Details

.create(*args) ⇒ Object

Convenience function creates a HasKey object for each argument passed in.



177
178
179
# File 'lib/safer/hashprotocol.rb', line 177

def self.create(*args)
  args.map do |el| self.new(el) end
end

Instance Method Details

#===(h) ⇒ Object

Check that the key from self.initialize is stored in Hash h.



159
160
161
# File 'lib/safer/hashprotocol.rb', line 159

def ===(h)
  h.has_key?(self.safer_hashprotocol_haskey__key)
end

#match(h, remaining) ⇒ Object

Check that the key from self.initialize is stored in Hash h. If it is, remove that key from remaining.



165
166
167
168
169
170
171
172
# File 'lib/safer/hashprotocol.rb', line 165

def match(h, remaining)
  if h.has_key?(self.safer_hashprotocol_haskey__key)
    remaining.delete(self.safer_hashprotocol_haskey__key)
    true
  else
    false
  end
end