Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
API Versioning and Inheritance
module API
module V1
class UsersController
def index
'v1'
end
def show
'v1'
end
end
class PostsController
def index
'v1'
end
end
end
module V2
include V1
class UsersController < UsersController
def index
'v2'
end
end
end
module V3
include V2
class UsersController < UsersController
def index
'v3'
end
undef_method :show
end
class PostsController < PostsController
def show
'v3'
end
end
end
end
(1..3).each do |i|
puts "Version #{i}"
puts '========='
[:UsersController, :PostsController].each do |klass|
puts klass
instance = API.const_get("V#{i}").const_get(klass).new
puts "\tindex: #{instance.index rescue 'undefined'}"
puts "\tshow: #{instance.show rescue 'undefined'}"
puts
end
end
# Version 1
# =========
# UsersController
# index: v1
# show: v1
#
# PostsController
# index: v1
# show: undefined
#
# Version 2
# =========
# UsersController
# index: v2
# show: v1
#
# PostsController
# index: v1
# show: undefined
#
# Version 3
# =========
# UsersController
# index: v3
# show: undefined
#
# PostsController
# index: v1
# show: v3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment