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.



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

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.



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

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.



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

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.



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

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