public
Description: Plug-and-play data persistence created for small Ruby web applications.
Homepage: http://stone.rubyforge.org
Clone URL: git://github.com/ndemonner/stone.git
ndemonner (author)
Mon Apr 14 19:56:17 -0700 2008
commit  9935a97b3a9bf2aab95cd35086e78bdfd49e39e3
tree    726de9a726e1687c54431ee099bfb1408c1322d1
parent  a5bd5e928adf8ecd6c6c324c05624ead061ee09a
stone / lib / stone.rb
100644 55 lines (46 sloc) 2.017 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'fileutils'
require 'rubygems'
require 'validatable'
require 'english/inflect'
require 'facets'
require 'yaml'
 
require File.expand_path(File.dirname(__FILE__) + '/stone/core_ext/string')
require File.expand_path(File.dirname(__FILE__) + '/stone/core_ext/symbol')
require File.expand_path(File.dirname(__FILE__) + '/stone/query')
require File.expand_path(File.dirname(__FILE__) + '/stone/data_store')
require File.expand_path(File.dirname(__FILE__) + '/stone/callbacks')
require File.expand_path(File.dirname(__FILE__) + '/stone/resource')
 
STONE_ROOT = Dir.pwd
 
module Stone
  class << self
    
    # For spec stuff only
    def empty_datastore
      if File.exists? STONE_ROOT/"sandbox_for_specs/datastore"
        FileUtils.rm_rf STONE_ROOT/"sandbox_for_specs/datastore"
      end
    end
    
    # Creates or updates a datastore at +path+
    # === Parameters
    # +path+<String>::
    # Path to create or update datastore (usually an application's root)
    # +resources+<Array>:: A list of resources that exist for the application
    def start(path, resources, framework = nil)
      DataStore.local_dir = path/"datastore"
      
      # create the datastore dir unless it exists
      FileUtils.mkdir(DataStore.local_dir) unless File.exists?(DataStore.local_dir)
      
      # create a .stone_metadata that contains the resource locations
      # for Stone::Utilities to use
      File.open(DataStore.local_dir/".stone_metadata", "w") do |out|
        YAML.dump({:rsrc_path => File.dirname(resources.first)}, out)
      end unless File.exists?(DataStore.local_dir/".stone_metadata")
      
      # load each resource unless a framework has already done it
      resources.each do |resource|
        require resource unless framework == :merb || framework == :rails
        name = File.basename(resource).gsub(".rb", "").pluralize
        unless File.exists? DataStore.local_dir/name
          FileUtils.mkdir(DataStore.local_dir/name)
        end
      end
    end
    
  end # self
end # Stone