Sha256: ea3fdc220b79ffc9e47d4576c38d7f9ae3cc99e7ce2ed03c866d6521f51dabfd

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

module FileRecord
  module Persistence
    extend ActiveSupport::Concern
    include ActiveModel::Serializers::JSON

    module ClassMethods
      def find(name)
        if File.exists? filename(name)
          self.new.from_json(File.read(filename(name)))
        else
          nil 
        end
      end

      def all
        Dir.new(Rails.root + "tmp/file_records").entries.reject do |name|
          File.directory?(name)  
        end.map do |name|
          find(name)
        end
      end
      
    def create(attributes = {})
      obj = self.new(attributes)
      obj.save ? obj : nil
    end

      def filename(name)
        Rails.root + "tmp/file_records/" + name
      end
    end

    def persisted?
      id && File.exists?(self.class.filename(id))
    end


    def save
        File.open(self.class.filename(id), 'w') {|f| f.write(to_json) } 
    end


    def destroy
      File.delete(self.class.filename(id)) if persisted?
    end

    def update_attributes(attributes)
      @attributes.merge!(attributes)
      sanitize_attributes
      save
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
file_record-0.1.0 lib/file_record/persistence.rb