Class: Bluepine::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/bluepine/resolver.rb

Overview

Responsible for registering and looking up the schemas and endpoints

Examples:

Register via :schemas and :endpoints options

resolver = Resolver.new(schemas: [], endpoints: [])

Register via block

resolver = Resolver.new do
  schema :user do
    string :username
  end

  schema :team do
    string :name
  end

  endpoint "/users" do
    string :username
  end
end

Manually register new schema/endpoint

resolver.schema(:user) do
  string :username
end

resolver.endpoint("/teams") do
  post :create
end

Register an existing schema/endpoint

resolver.schemas.register(:user, user_schema)

Constant Summary collapse

Endpoint =
Bluepine::Endpoint
Attributes =
Bluepine::Attributes
SchemaNotFound =
Bluepine::Error.create("Endpoint %s cannot be found")
EndpointNotFound =
Bluepine::Error.create("Schema %s cannot be found")

Instance Method Summary collapse

Constructor Details

#initialize(schemas: [], endpoints: [], schema_registry: nil, endpoint_registry: nil, &block) ⇒ Resolver

Returns a new instance of Resolver.



41
42
43
44
45
46
47
48
# File 'lib/bluepine/resolver.rb', line 41

def initialize(schemas: [], endpoints: [], schema_registry: nil, endpoint_registry: nil, &block)
  @registries = {
    schemas:   create_schema_registry(schemas, schema_registry),
    endpoints: create_endpoint_registry(endpoints, endpoint_registry)
  }

  instance_exec(&block) if block_given?
end

Instance Method Details

#endpoint(path, options = {}, &block) ⇒ Object



74
75
76
77
78
# File 'lib/bluepine/resolver.rb', line 74

def endpoint(path, options = {}, &block)
  return resolve(:endpoints, Endpoint.normalize_name(path)) unless block_given?

  register(:endpoints, path, options, &block)
end

#endpointsObject

Exposes endpoint registry



64
65
66
# File 'lib/bluepine/resolver.rb', line 64

def endpoints
  @registries[:endpoints]
end

#register(type, name, *args, &block) ⇒ Object



54
55
56
# File 'lib/bluepine/resolver.rb', line 54

def register(type, name, *args, &block)
  @registries[type].create(name, *args, &block)
end

#resolve(type, name) ⇒ Object



50
51
52
# File 'lib/bluepine/resolver.rb', line 50

def resolve(type, name)
  @registries[type].get(name)
end

#schema(name, options = {}, &block) ⇒ Object



68
69
70
71
72
# File 'lib/bluepine/resolver.rb', line 68

def schema(name, options = {}, &block)
  return resolve(:schemas, name) unless block_given?

  register(:schemas, name, options, &block)
end

#schemasObject

Exposes schema registry



59
60
61
# File 'lib/bluepine/resolver.rb', line 59

def schemas
  @registries[:schemas]
end