Last active
August 29, 2015 14:21
-
-
Save jcarhuazv/310a50961b1327a085ca to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<!-- Archivo dentro de '/views' --> | |
<h1>Lista de alimentos</h1> | |
<ul> | |
<% @app.alimentos.each do |id, alimento| %> | |
<li><%= alimento %></li> | |
<% end %> | |
</ul> |
This file contains hidden or 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
require 'sinatra' | |
require './papa_delmont' | |
before do | |
@app = PapaDelmont::App.new | |
@app.vista = self | |
end | |
#usando < get '/' do > funciona bien | |
get '/alimentos' do | |
@app.lista | |
end | |
public def alimentos_leidos | |
erb :alimentos | |
end |
This file contains hidden or 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
require 'sinatra' | |
require './come_platzi' | |
ruta = File.expand_path(File.dirname(__FILE__)) | |
set :environment, :development | |
set :root, ruta | |
set :app_file, File.join(ruta, 'come_platzi.rb') | |
disable :run | |
run Sinatra::Application |
This file contains hidden or 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
source 'https://rubygems.org' | |
gem 'sinatra', '1.4.6' |
This file contains hidden or 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 PapaDelmont | |
class Alimento | |
def initialize(desc, creacion, compra = nil) | |
@desc, @creacion, @compra = desc, creacion, compra | |
end | |
def creacion | |
@creacion | |
end | |
def desc | |
@desc | |
end | |
def comprado? | |
@compra | |
end | |
def compra | |
@compra | |
end | |
def to_s | |
@desc | |
end | |
end | |
class App | |
def initialize() | |
@vista = nil | |
end | |
def vista=(v) | |
@vista = v | |
end | |
def alimentos | |
@alimentos | |
end | |
def lista | |
@alimentos = [] | |
File.open('alimentos.txt', 'r') do |f| | |
identificador = 1 | |
f.readlines.each do |linea| | |
@alimentos << [identificador, Alimento.new(*linea.chomp.split(/,/))] | |
identificador += 1 | |
end | |
end | |
@vista.alimentos_leidos | |
end | |
end | |
class Cli | |
def initialize(app) | |
@app = app | |
end | |
def self.run | |
ui = Cli.new(App.new) | |
ui.empezar | |
end | |
def alimentos_leidos | |
@app.alimentos.each do |id,alimento| | |
puts "#{alimento.desc} (#{id})" | |
end | |
end | |
def lista | |
@app.lista | |
end | |
def empezar | |
@app.vista = self | |
begin | |
cmd = ARGV.shift.to_sym | |
case cmd | |
when :nuevo | |
puts "aca va nuevo" | |
when :lista | |
lista | |
when :comprar | |
puts "aca va a comprar" | |
end | |
rescue | |
puts "indicar informacion de como usar" | |
end | |
end | |
end | |
end | |
# p = PapaDelmont::Cli.new | |
# p.empezar | |
# PapaDelmont::Cli.run | |
# pollo = PapaDelmont::Alimento.new('Pollo', Time.now, nil) | |
# puts "creacion: #{pollo.creacion}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment