class Bluepine::Resolver

Responsible for registering and looking up the schemas and endpoints

@example Register via :schemas and :endpoints options

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

@example 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

@example Manually register new schema/endpoint

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

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

@example Register an existing schema/endpoint

resolver.schemas.register(:user, user_schema)

Constants

Attributes

Attributes registry holds the references to all attributes

@see .create
















Endpoint
EndpointNotFound
SchemaNotFound

Public Class Methods

new(schemas: [], endpoints: [], schema_registry: nil, endpoint_registry: nil, &block) click to toggle source
# 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

Public Instance Methods

endpoint(path, options = {}, &block) click to toggle source
# 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
endpoints() click to toggle source

Exposes endpoint registry

# File lib/bluepine/resolver.rb, line 64
def endpoints
  @registries[:endpoints]
end
register(type, name, *args, &block) click to toggle source
# File lib/bluepine/resolver.rb, line 54
def register(type, name, *args, &block)
  @registries[type].create(name, *args, &block)
end
resolve(type, name) click to toggle source
# File lib/bluepine/resolver.rb, line 50
def resolve(type, name)
  @registries[type].get(name)
end
schema(name, options = {}, &block) click to toggle source
# 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
schemas() click to toggle source

Exposes schema registry

# File lib/bluepine/resolver.rb, line 59
def schemas
  @registries[:schemas]
end

Private Instance Methods

create_endpoint_registry(endpoints, registry = nil) click to toggle source
# File lib/bluepine/resolver.rb, line 90
def create_endpoint_registry(endpoints, registry = nil)
  return registry.(endpoints) if registry

  Registry.new(endpoints, error: EndpointNotFound) do |path, options = {}, block|
    endpoint = Endpoint.new(path, options, &block)
    @objects[endpoint.name] = endpoint
  end
end
create_schema_registry(schemas, registry = nil) click to toggle source
# File lib/bluepine/resolver.rb, line 82
def create_schema_registry(schemas, registry = nil)
  return registry.(schemas) if registry

  Registry.new(schemas, error: SchemaNotFound) do |name, options = {}, block|
    @objects[name] = Attributes.create(:object, name, options, &block)
  end
end