Class: Apigen::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/apigen/migration.rb

Overview

Migration is the base class for API definition migrations.

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ Migration

Returns a new instance of Migration.



7
8
9
# File 'lib/apigen/migration.rb', line 7

def initialize(api)
  @api = api
end

Instance Method Details

#add_endpoint(name, &block) ⇒ Object



15
16
17
18
# File 'lib/apigen/migration.rb', line 15

def add_endpoint(name, &block)
  raise 'You must pass a block when calling `add_endpoint`.' unless block_given?
  @api.endpoint(name, &block)
end

#add_model(name, &block) ⇒ Object



40
41
42
# File 'lib/apigen/migration.rb', line 40

def add_model(name, &block)
  @api.model(name, &block)
end

#remove_endpoint(*names) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/apigen/migration.rb', line 31

def remove_endpoint(*names)
  endpoints = @api.endpoints
  # This is not algorithmically optimal. We won't do it millions of times though.
  names.each do |name|
    raise "No such endpoint :#{name}." unless endpoints.find { |e| e.name == name }
  end
  endpoints.reject! { |e| names.include?(e.name) }
end

#remove_model(*names) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/apigen/migration.rb', line 55

def remove_model(*names)
  models = @api.models
  names.each do |name|
    raise "No such model :#{name}." unless models.key? name
  end
  names.each { |model_name| models.delete(model_name) }
end

#upObject



11
12
13
# File 'lib/apigen/migration.rb', line 11

def up
  raise 'Migration subclasses must implement #up.'
end

#update_endpoint(name, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/apigen/migration.rb', line 20

def update_endpoint(name, &block)
  endpoint = @api.endpoints.find { |e| e.name == name }
  error = if !endpoint
            "No such endpoint #{name}."
          elsif !block_given?
            'You must pass a block when calling `update_endpoint`.'
          end
  raise error unless error.nil?
  endpoint.instance_eval(&block)
end

#update_model(name, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/apigen/migration.rb', line 44

def update_model(name, &block)
  model = @api.models[name]
  error = if !model
            "No such model :#{name}."
          elsif !block_given?
            'You must pass a block when calling `update_model`.'
          end
  raise error unless error.nil?
  model.instance_eval(&block)
end