ActiveFiles

by Clinton R. Nixon

URL: github.com/crnixon/active_files

Description

This library attempts to implement an ActiveRecord-like interface to a directory structure of flat files containing serialized objects, probably in YAML.

Features/problems

  • Serializes any object

  • No validations yet

Requirements

To run tests, you need the spect and Shoulda gems.

Synopsis

ActiveFiles.base_dir = '/tmp/af'

class Person < Hash
  include ActiveFiles::Record
  add_file_id_to_initialize
end

class Wingdom
  include ActiveFiles::Record

  attr_reader :feathers, :color

  def initialize(id, feathers, color)
    self.file_id = id
    @feathers = feathers
    @color = color
  end

  def to_activefile
    { :plumoj => @feathers.to_s.split(//).reverse,
      :koloro => [@color, @color, @color].join('\/') }.to_yaml.reverse
  end

  def self.from_activefile(yaml, file_id)
    hash = YAML::load(yaml.reverse)
    feathers = hash[:plumoj].reverse.inject("") { |s, c| s << c }.to_i
    color = hash[:koloro].split('\/')[1]
    Wingdom.new(file_id, feathers, color)
  end
end

herbert = Person.new('herbert')
herbert['address'] = '103 Choctaw Dr'
herbert['birthday'] = Date.parse('2/4/1979')
herbert.save

also_herbert = Person.find("herbert")
also_herbert = Person.find(:first, "herb*")
herbs = Person.find("herb*")
herbs = Person.find(:all, "herb*")

License

(ISC License)

Copyright © 2008 Clinton R. Nixon of Viget Labs

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.