aboutsummaryrefslogtreecommitdiff
path: root/core/store.rb
blob: 5e1cf672b003c81a0e09761e2f9c2b81038561c7 (plain) (blame)
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
require 'pstore'

module Dinobot
  module Core
    class Store
      attr_accessor :data

      def initialize(file)
        Dir.mkdir(File.dirname(file)) unless Dir.exist?(File.dirname(file))
        @store = PStore.new(file, true)

        read

        @data ||= Hash.new
      end

      def [](key)
        @data[key]
      end

      def []=(key, value)
        @data[key] = value
      end

      def read
        @store.transaction(true) do
          @data = @store[:data]
        end
      end

      def save
        @store.transaction do
          @store[:data] = @data
        end
      end
    end
  end
end