Created
March 8, 2015 14:13
-
-
Save aristotelesbr/b1a0454250948d30a827 to your computer and use it in GitHub Desktop.
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
require 'fileutils' | |
module ActiveFile | |
def save | |
@new_record = false | |
File.open("db/revistas/#{@id}.yml", "w") do |file| | |
file.puts serialize | |
end | |
end | |
def destroy | |
unless @destroy or @new_record | |
@destroyed = true | |
FileUtils.rm "db/revistas/#{@id}.yml" | |
end | |
end | |
module ClassMethods | |
def find(id) | |
raise DocumentNotFound, | |
"Arquivo db/revistas/#{id} nao encontrado.", caller | |
unless File.exists?("db/revistas/#{id}.yml") | |
YAML.load File.open("db/revistas/#{id}.yml", "r") | |
end | |
end | |
def next_id | |
Dir.glob("db/revistas/*yml").size + 1 | |
end | |
def field(name) | |
@fields ||= [] | |
@fields << name | |
get = %Q{ | |
def #{name} | |
@#{name} | |
end | |
} | |
set = %Q{ | |
def #{name}=(valor) | |
@#{name}=valor | |
end | |
} | |
self.class_eval get | |
self.class_eval set | |
end | |
end | |
def included(base) | |
base.extend ClassMethods | |
base.class_eval do | |
attr_reader :id, :destroyed, :new_record | |
def initialize | |
@id = self.class.next_id | |
@destroyed = false | |
@new_record = true | |
end | |
end | |
end | |
private | |
def serialize | |
YAML.dump self | |
end | |
end |
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
NoMethodError: undefined method `field' for Revista:Class | |
from /home/ruby/loja_virtual/lib/revista.rb:6:in `<class:Revista>' |
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
# coding: utf-8 | |
class Revista | |
#mixing | |
include ActiveFile | |
field :titulo | |
field :valor | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment