Class: Kybus::Nanoservice::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/kybus/nanoservice/schema.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Schema

Returns a new instance of Schema.



16
17
18
19
20
21
22
# File 'lib/kybus/nanoservice/schema.rb', line 16

def initialize(schema)
  raise('`models` config is missing') if schema['models'].nil?
  raise('`repositories` is not defined') if schema['repositories'].nil?

  build_schemas(schema['models'])
  build_repositories(schema['models'], schema['repositories'])
end

Instance Attribute Details

#repositoriesObject (readonly)

Returns the value of attribute repositories.



14
15
16
# File 'lib/kybus/nanoservice/schema.rb', line 14

def repositories
  @repositories
end

#schemaObject (readonly)

Returns the value of attribute schema.



14
15
16
# File 'lib/kybus/nanoservice/schema.rb', line 14

def schema
  @schema
end

Instance Method Details

#factory_builder(schema_name) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/kybus/nanoservice/schema.rb', line 63

def factory_builder(schema_name)
  model = schema[schema_name]
  repo = repositories[schema_name]
  factory = Kybus::Storage::Factory.new(model)
  factory.register(:default, :primary)
  factory.register(:primary, repo)
  factory
end

#mount_grape_helpers(api, schema_name) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kybus/nanoservice/schema.rb', line 48

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 = Kybus::Storage::Factory.new(model)
        factory.register(:default, :primary)
        factory.register(:primary, repo)
        factory
      end
    end
  end
end