Created
May 11, 2012 09:50
-
-
Save fphilipe/2658685 to your computer and use it in GitHub Desktop.
API Versioning and Inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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