Skip to content

Instantly share code, notes, and snippets.

@fphilipe
Created May 11, 2012 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fphilipe/2658685 to your computer and use it in GitHub Desktop.
Save fphilipe/2658685 to your computer and use it in GitHub Desktop.
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