Class: Safer::HashProtocol::Only

Inherits:
Single show all
Defined in:
lib/safer/hashprotocol.rb

Overview

Check that the Hash only contains elements that match the base Safer::HashProtocol object.

Instance Method Summary collapse

Methods inherited from Single

#self

Methods inherited from Base

#self

Constructor Details

#initialize(base) ⇒ Only

The description for this object will be “ONLY #Safer::HashProtocol::Only.basebase.description”.



277
278
279
# File 'lib/safer/hashprotocol.rb', line 277

def initialize(base)
  super("ONLY ", base)
end

Instance Method Details

#===(h) ⇒ Object

Check that all keys in a hash are matched by the base HashProtocol object.



283
284
285
286
287
288
289
290
# File 'lib/safer/hashprotocol.rb', line 283

def ===(h)
  remaining = h.dup
  if self.base.match(h, remaining)
    remaining.empty?
  else
    false
  end
end

#match(h, remaining) ⇒ Object

Check that all keys in a hash are matched by the base HashProtocol object. If they are, then empty remaining.



294
295
296
297
298
299
300
301
# File 'lib/safer/hashprotocol.rb', line 294

def match(h, remaining)
  if self === h
    remaining.clear
    true
  else
    false
  end
end