Class: Philiprehberger::TypedHash::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/typed_hash/schema.rb

Overview

Schema definition for a typed hash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strict: false) ⇒ Schema

Returns a new instance of Schema.

Parameters:

  • strict (Boolean) (defaults to: false)

    when true, reject unknown keys



8
9
10
11
# File 'lib/philiprehberger/typed_hash/schema.rb', line 8

def initialize(strict: false)
  @fields = {}
  @strict = strict
end

Instance Attribute Details

#fieldsHash (readonly)

Returns the field definitions.

Returns:

  • (Hash)

    the field definitions



17
18
19
# File 'lib/philiprehberger/typed_hash/schema.rb', line 17

def fields
  @fields
end

#strictBoolean (readonly)

Returns whether unknown keys are rejected.

Returns:

  • (Boolean)

    whether unknown keys are rejected



14
15
16
# File 'lib/philiprehberger/typed_hash/schema.rb', line 14

def strict
  @strict
end

Instance Method Details

#from_json(json_str) ⇒ Instance

Deserialize a JSON string into a typed hash instance

Parameters:

  • json_str (String)

    JSON string

Returns:



75
76
77
78
# File 'lib/philiprehberger/typed_hash/schema.rb', line 75

def from_json(json_str)
  data = JSON.parse(json_str, symbolize_names: true)
  new(data)
end

#key(name, type, default: nil, optional: false, coerce: nil) ⇒ void

This method returns an undefined value.

Define a key with a type and options

Parameters:

  • name (Symbol)

    the key name

  • type (Class)

    the expected type

  • default (Object, nil) (defaults to: nil)

    default value

  • optional (Boolean) (defaults to: false)

    whether the key is optional

  • coerce (Proc, nil) (defaults to: nil)

    coercion function



27
28
29
30
31
32
33
34
# File 'lib/philiprehberger/typed_hash/schema.rb', line 27

def key(name, type, default: nil, optional: false, coerce: nil)
  @fields[name] = {
    type: type,
    default: default,
    optional: optional,
    coerce: coerce
  }
end

#keysArray<Symbol>

Return the declared top-level key names in definition order

Only returns top-level keys — nested schemas are represented by their parent key, not by their inner fields. The returned Array is a fresh copy; mutating it does not affect the schema.

Returns:

  • (Array<Symbol>)

    the declared top-level key names in definition order



59
60
61
# File 'lib/philiprehberger/typed_hash/schema.rb', line 59

def keys
  @fields.keys
end

#nested(name, optional: false) {|schema| ... } ⇒ void

This method returns an undefined value.

Define a nested typed hash schema

Parameters:

  • name (Symbol)

    the key name

  • optional (Boolean) (defaults to: false)

    whether the nested schema is optional

Yields:

  • (schema)

    block receiving a new Schema for the nested hash



42
43
44
45
46
47
48
49
50
# File 'lib/philiprehberger/typed_hash/schema.rb', line 42

def nested(name, optional: false, &block)
  nested_schema = Schema.new(strict: @strict)
  nested_schema.instance_eval(&block)
  @fields[name] = {
    type: Hash,
    optional: optional,
    nested_schema: nested_schema
  }
end

#new(data = {}) ⇒ Instance

Create a new typed hash instance from data

Parameters:

  • data (Hash) (defaults to: {})

    the input data

Returns:



67
68
69
# File 'lib/philiprehberger/typed_hash/schema.rb', line 67

def new(data = {})
  Instance.new(self, data)
end