Class: Atum::Generation::Generators::BaseGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/atum/generation/generators/base_generator.rb

Constant Summary collapse

TEMPLATE_NAME =
nil

Instance Method Summary collapse

Constructor Details

#initialize(module_name, schema, url, options) ⇒ BaseGenerator

Generate a static client that uses Atum under the hood. This is a good option if you want to ship a gem or generate API documentation using Yard.

Parameters:

  • module_name (String)

    The name of the module, as rendered in a Ruby source file, to use for the generated client.

  • schema (Schema)

    The schema instance to generate the client from.

  • url (String)

    The URL for the API service.

  • options (Hash)

    Configuration for links. Possible keys include:

    • default_headers: Optionally, a set of headers to include in every request made by the client. Default is no custom headers.



17
18
19
20
21
22
# File 'lib/atum/generation/generators/base_generator.rb', line 17

def initialize(module_name, schema, url, options)
  @module_name = module_name
  @schema = schema
  @url = url
  @options = options
end

Instance Method Details

#contextObject



24
25
26
# File 'lib/atum/generation/generators/base_generator.rb', line 24

def context
  ErbContext.new(context_hash.merge(module_name: @module_name))
end

#context_hashObject

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/atum/generation/generators/base_generator.rb', line 28

def context_hash
  raise NotImplementedError, 'Subclasses must define context_hash'
end

#generateObject



36
37
38
# File 'lib/atum/generation/generators/base_generator.rb', line 36

def generate
  template.evaluate(context)
end

#resourcesObject



40
41
42
# File 'lib/atum/generation/generators/base_generator.rb', line 40

def resources
  @schema.resource_schemas.map { |r| GeneratorResource.new(r) }
end

#templateObject



32
33
34
# File 'lib/atum/generation/generators/base_generator.rb', line 32

def template
  @template ||= Erubis::Eruby.new(File.read(template_path))
end