Class: Ant::Nanoservice::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/ant/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
# File 'lib/ant/nanoservice/schema.rb', line 16

def initialize(schema)
  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/ant/nanoservice/schema.rb', line 14

def repositories
  @repositories
end

#schemaObject (readonly)

Returns the value of attribute schema.



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

def schema
  @schema
end

Instance Method Details

#mount_grape_helpers(api, schema_name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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