Skip to content

Instantly share code, notes, and snippets.

@ryaan-anthony
Last active April 12, 2016 01:03
Show Gist options
  • Save ryaan-anthony/61785c581d0468f9ba7b488afbb30dc7 to your computer and use it in GitHub Desktop.
Save ryaan-anthony/61785c581d0468f9ba7b488afbb30dc7 to your computer and use it in GitHub Desktop.
# data trait
module Option
def list
all.sort.unshift(default)
end
def default
'all'
end
end
# data object
class Tag
include Option
def all
[1, 2, 3]
end
end
# handle cache service
class CacheService
def initialize tag_model
@tag_model = tag_model
end
def handle(tag)
if tag == tag_model.default
Rails.cache.clear
elsif tag_model.list.include? tag
Rails.cache.delete_matched("#{tag}*")
end
end
end
# view controller
class Controller
# ex 1: display the view
def index
@tags = tag_model.list
end
# ex 2: handle an action
def action
service.handle params[:action]
redirect_to index_page
end
protected
def service
@service ||= CacheService.new tag_model
end
def tag_model
@tag_model ||= Tag.new
end
end
puts Controller.new.index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment