Module: Natsuzora::Contract::AST

Defined in:
lib/natsuzora/contract/ast.rb,
lib/natsuzora/contract/ast/any.rb,
lib/natsuzora/contract/ast/ref.rb,
lib/natsuzora/contract/ast/list.rb,
lib/natsuzora/contract/ast/node.rb,
lib/natsuzora/contract/ast/record.rb,
lib/natsuzora/contract/ast/scalar.rb

Overview

Contract AST nodes and their serialization helpers.

Defined Under Namespace

Classes: Any, List, Node, Record, Ref, Scalar

Class Method Summary collapse

Class Method Details

.from_h(hash) ⇒ Object

Build an AST::Node tree from its hash representation.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/natsuzora/contract/ast.rb', line 15

def self.from_h(hash)
  case hash['kind']
  when 'any'
    Any.new
  when 'scalar'
    scalar_type = hash['type'].to_sym
    modifier = (hash['modifier'] || 'none').to_sym
    Scalar.new(scalar_type, modifier)
  when 'object'
    required = hash['required'] || []
    properties = (hash['properties'] || {}).transform_values { |v| from_h(v) }
    Record.new(properties, required)
  when 'array'
    items = from_h(hash['items'])
    List.new(items)
  when 'ref'
    Ref.new(hash['name'])
  else
    raise ArgumentError, "Unknown contract kind: #{hash['kind']}"
  end
end