Skip to content

Instantly share code, notes, and snippets.

@aristotelesbr
Created March 1, 2015 16:42
Show Gist options
  • Save aristotelesbr/68f5ffcc17f7ab02bbec to your computer and use it in GitHub Desktop.
Save aristotelesbr/68f5ffcc17f7ab02bbec to your computer and use it in GitHub Desktop.
require "fileutils"
class Revista
def self.find(id)
raise DocumentNotFound,
"Arquivo db/revistas/#{id} não encontrado.", caller
unless File.exists?("db/revistas/#{id}.yml")
YAML.load File.open("db/revistas/#{id}.yml", "r")
encontrado
end
end
attr_reader :titulo, :id, :destroyed
attr_accessor :valor
#Cria
def initialize (titulo, valor)
@titulo = titulo
@valor = valor
@id = self.class.next_id #Atribui um id ao objeto Revista
@destroyed = false
end
#Salva
...
...
#Procura
def self.find(id)
YAML.load File.open("db/revistas/#{id}.yml", "r")
end
#Destroy
...
...
private
def serialize
YAML.dump self
end
def self.next_id
Dir.glob("db/revistas/*.yml").size + 1
end
end
@dodops
Copy link

dodops commented Mar 1, 2015

class Revista
  class DocumentNotFound < IOError
  end

  def self.find(id)
    unless File.exists?("db/revistas/#{id}.yml")
      raise DocumentNotFound, "Arquivo db/revistas/#{id} não encontrado.", caller
    else
      YAML.load File.open("db/revistas/#{id}.yml", "r")
      encontrado
    end
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment