Module: StrokeDB::Console

Defined in:
lib/strokedb/console.rb

Constant Summary collapse

RAM_BASE_PATH =
'.console-ram.strokedb'

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/strokedb/console.rb', line 10

def self.included(klass)
  klass.module_eval do
    
    def setup
      if ARGV.last.is_a? String
        if (@@loc = ARGV.pop) == 'RAM'
          puts 'Using in-memory storage.'
          FileUtils.rm_rf RAM_BASE_PATH
          ::StrokeDB::Config.build :default => true, :storages => [:memory], :base_path => RAM_BASE_PATH
        else
          puts "Using #{@@loc} storage."
          ::StrokeDB::Config.build :default => true, :base_path => @@loc
        end
      else
        @@loc = '.console.strokedb'
        ::StrokeDB::Config.build :default => true, :base_path => @@loc
      end
      @@store = ::StrokeDB::default_store
      @@saved = false
    end
    
    def save!
      @@store.storage.sync_chained_storages!
      @@saved = true
      @@store
    end
    def saved?
      @@saved
    end

    def clear!
      if @@loc == 'RAM'
        FileUtils.rm_rf RAM_BASE_PATH
      else
        FileUtils.rm_rf @@loc
      end
      setup
      "Database has been wiped out."
    end

    def find(*args)
      @@store.find(*args)
    end

    def store
      @@store
    end
    
    def sandbox?
      @@sandbox
    end
    
    def reload!
      silence_warnings do
        load "strokedb.rb"
      end
      StrokeDB
    end
    
    def help!
      puts ("
        - save!           Save the console's store (by default, a file in your current directory named console.strokedb)
        - clear!          Drop the console's store (destructive, and launches you out of the session)
        - find <uuid>     Find document by UUID in the console's store (example: find 'a4430ff1-6cb4-4428-a292-7ab8b77de467')
        - Doc             Alias for Document
        - store           Alias for the console's store's object
        ".unindent!)
    end
    
  end
  klass.send(:include, StrokeDB)
  klass.send(:setup)
  
  puts "StrokeDB #{::StrokeDB::VERSION} (help! for more info)"
end