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”.



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

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.



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

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.



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

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