durable 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/durable.rb +88 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6c2e90f8719a9a6de7b5cb5f093684f973d5bc7e
4
+ data.tar.gz: 2735fca286544c8dd7913c7f9603973bdd949d46
5
+ SHA512:
6
+ metadata.gz: b8923d85bbfbbb71f0ac616718f9d0f60b4b8db11e627f156350d5b77f8bdbbc5a5a59fc7f8053e76fa140b835dc24b35d917f250bf1b48f9278e6fee83954a1
7
+ data.tar.gz: 9f297de29821313440ad9d3f90c5b609414ca49a3ea5f6126fe0c412700f0f706de5ceaf759863a32b9ccf4a7fa69ebf2463ad886b0bd537a7e7155238cef5a4
data/lib/durable.rb ADDED
@@ -0,0 +1,88 @@
1
+ ###################################
2
+ #Durable Version 1.0
3
+ #Copyright (c) 2016 Philipp Sippl
4
+ #Released under MIT License (MIT)
5
+ ###################################
6
+
7
+ at_exit do
8
+ Durable.commitAndExit
9
+ end
10
+
11
+ class Durable
12
+ @@DURABLE_HOME = ".DURABLE_HOME"
13
+ @@DURABLE_PREFIX = "."
14
+ @@AUTOCOMMIT_MODE = true
15
+ @@instances = []
16
+
17
+ @key = nil
18
+ @val = nil
19
+ @initial = nil
20
+ attr_accessor :val
21
+
22
+ def self.config(config)
23
+ @@AUTOCOMMIT_MODE = config[:autocommit] if config[:autocommit] != nil
24
+ @@DURABLE_HOME = config[:home] if config[:home] != nil
25
+ end
26
+
27
+ def self.commitAndExit
28
+ self.commit if @@AUTOCOMMIT_MODE
29
+ end
30
+
31
+ def self.commit
32
+ @@instances.map{|x| x.commit}
33
+ end
34
+
35
+ def commit
36
+ tmp = {:initial => @initial, :new => @val}
37
+ self.store_helper(@key, tmp)
38
+ end
39
+
40
+ def store_helper(key, val)
41
+ Dir.mkdir(@@DURABLE_HOME) if !Dir.exists? @@DURABLE_HOME
42
+ f = File.new(@@DURABLE_HOME+"/"+@@DURABLE_PREFIX+key.to_s, "w")
43
+ f.print(Marshal::dump(val));
44
+ f.close
45
+ end
46
+
47
+ def load_helper(key)
48
+ if(self.key_exists(key))
49
+ Marshal::load(File.read(@@DURABLE_HOME+"/"+@@DURABLE_PREFIX+key.to_s))
50
+ else
51
+ nil
52
+ end
53
+ end
54
+
55
+ def key_exists(key)
56
+ File.exist? @@DURABLE_HOME+"/"+@@DURABLE_PREFIX+key.to_s
57
+ end
58
+
59
+ def compare_helper(o1, o2)
60
+ return Marshal::dump(o1) == Marshal::dump(o2)
61
+ end
62
+
63
+ def clone_helper(o)
64
+ begin
65
+ #will fail on fixnums
66
+ o.clone
67
+ rescue
68
+ o
69
+ end
70
+ end
71
+
72
+ def initialize(obj)
73
+ @@instances << self
74
+ @key = obj.keys[0]
75
+
76
+ if(self.key_exists(@key) && compare_helper(self.load_helper(obj.keys[0])[:initial], obj[@key]) )
77
+ loaded = self.load_helper(obj.keys[0])
78
+ @val = loaded[:new]
79
+ @initial = loaded[:initial]
80
+ else
81
+ @val = obj[@key]
82
+ @initial = clone_helper(obj[@key])
83
+ tmp = {:initial => @initial, :new => @val}
84
+ self.store_helper(@key, tmp)
85
+ end
86
+ end
87
+
88
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: durable
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Philipp Sippl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Durable is a small gem that can make changes to objects and variables
14
+ persistent. Usage is super easy and only few changes to your code are needed.
15
+ email: philsippl@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/durable.rb
21
+ homepage: http://rubygems.org/gems/durable
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.0.14
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Durable makes variables or objects durable by storing them locally
45
+ test_files: []