Class: ImplicitSchema

Inherits:
BasicObject
Defined in:
lib/implicit_schema.rb

Overview

Wraps a Hash object, and raises ImplicitSchema::ValidationError when #[] is called with a missing key

Constant Summary collapse

ValidationError =
::Class.new(::RuntimeError)

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ ImplicitSchema

Returns a new instance of ImplicitSchema.



6
7
8
# File 'lib/implicit_schema.rb', line 6

def initialize(hash)
  @hash = hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



10
11
12
# File 'lib/implicit_schema.rb', line 10

def method_missing(sym, *args, &block)
  @hash.send(sym, *args, &block)
end

Instance Method Details

#[](k) ⇒ Object



14
15
16
17
18
# File 'lib/implicit_schema.rb', line 14

def [](k)
  return wrap(@hash[k]) if key?(k)
  fail(ValidationError, "Missing key: `#{k.inspect}` - available keys: " \
                        "(#{keys.map(&:inspect).join(', ')})")
end

#inspectObject



20
21
22
# File 'lib/implicit_schema.rb', line 20

def inspect
  "<<#{@hash.inspect}>>"
end