Skip to content

Instantly share code, notes, and snippets.

@forresty
Created July 17, 2017 01:54
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 forresty/69ca42b4b7a7bbf6df7fcd1b27cd5d0e to your computer and use it in GitHub Desktop.
Save forresty/69ca42b4b7a7bbf6df7fcd1b27cd5d0e to your computer and use it in GitHub Desktop.
require_relative 'tasks_view'
class AudibleTasksView < TasksView
def puts(args)
super
`say '#{args}'`
end
end
class Task
attr_reader :name
def initialize(name)
@name = name
@done = false
end
def done?
@done
end
def mark_as_done
@done = true
end
end
require_relative 'tasks_view'
require_relative 'audible_tasks_view'
require_relative 'tasks_repository'
class TasksController
def initialize(repository)
@repository = repository
# @view = TasksView.new
@view = AudibleTasksView.new
end
def list
@tasks = @repository.all
@view.display(@tasks)
end
def add
name = @view.ask_for(:name)
new_task = Task.new(name)
@repository.add_task(new_task)
end
def mark_as_done
index = @view.ask_for_index('mark as done')
@repository.mark_as_done(index)
end
def destroy
index = @view.ask_for_index('delete')
@repository.delete(index)
end
end
task = Task.new('wake up')
repo = TasksRepository.new('non_existing.csv')
# repo.add_task(task)
controller = TasksController.new(repo)
controller.add
controller.list
# # repo.mark_as_done(0)
controller.mark_as_done
# puts
controller.list
require_relative 'task'
class TasksRepository
def initialize(csv_file)
@csv_file = csv_file
# array of Task objects
@tasks = []
# TODO: load from CSV
end
def all
@tasks
end
def add_task(task)
@tasks << task
save
end
def mark_as_done(index)
@tasks[index].mark_as_done
save
end
def delete(index)
@tasks.delete_at(index)
save
end
private
def save
# TODO: save all the tasks
end
end
# human and computer interface
# human 1..infinity
# computer 0..inifity
class TasksView
def display(tasks)
tasks.each_with_index do |task, index|
abc_index = ('A'.ord + index).chr
puts "#{abc_index} [#{ task.done? ? 'X' : ' ' }] #{task.name}"
end
end
def ask_for(stuff)
puts "enter #{stuff} of new task: "
print '> '
gets.chomp
end
def ask_for_index(why)
puts "enter index of task to #{why}: "
print '> '
abc_index = gets.chomp
abc_index.ord - 'A'.ord
# gets.chomp.to_i - 1
end
end
class UsersController
def create
# 1. validate the params
# 2. save the user to db (complicated)
# -> refactor to Service Object
UserCreationService.call(params)
# 3. display / redirect
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment