class Ant::Nanoservice::Schema

Takes a configuration and creates the metaclasess, repositories and factory from them. this can be attached to Grape API as helpers and provide a connection to the data layer.

Attributes

repositories[R]
schema[R]

Public Class Methods

new(schema) click to toggle source
# File lib/ant/nanoservice/schema.rb, line 16
def initialize(schema)
  build_schemas(schema['models'])
  build_repositories(schema['models'], schema['repositories'])
end

Public Instance Methods

mount_grape_helpers(api, schema_name) click to toggle source
# File lib/ant/nanoservice/schema.rb, line 44
def mount_grape_helpers(api, schema_name)
  model = schema[schema_name]
  repo = repositories[schema_name]
  api.helpers do
    define_method('factory') do
      @factory ||= begin
        factory = Ant::Storage::Factory.new(model)
        factory.register(:default, :primary)
        factory.register(:primary, repo)
        factory
      end
    end
  end
end

Private Instance Methods

build_repositories(models, repository_conf) click to toggle source
# File lib/ant/nanoservice/schema.rb, line 33
def build_repositories(models, repository_conf)
  @repositories = models.each_with_object({}) do |(name, _), obj|
    obj[name] = Ant::Storage::Repository
                .from_config(@schema[name],
                             @schema_configs[name]['configs'],
                             repository_conf['default'])
  end
end
build_schemas(models) click to toggle source
# File lib/ant/nanoservice/schema.rb, line 23
def build_schemas(models)
  @schema_configs = {}
  @schema = models.each_with_object({}) do |(name, configs), obj|
    columns = configs['fields']
    @schema_configs[name] = configs
    configs['configs']['schema_name'] = name
    obj[name] = MetaTypes.build(name, columns, configs)
  end
end