text-db 0.2.0.beta → 0.3.0.beta

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,7 @@ require "textdb/version"
8
8
  # - check data file extension only at the end of name
9
9
  # - documentation
10
10
  # - unite textdb.* and event.processor.* methods
11
+ # - Event.processor - delete also the key
11
12
 
12
13
  module Textdb
13
14
 
@@ -52,32 +52,34 @@ module Textdb
52
52
  self[name]
53
53
  end
54
54
 
55
+ # Listener listen only for file
55
56
  def build_value(name)
56
57
  name_without_ext = name.gsub(Textdb.config.data_file_extension, "")
57
58
 
58
- self[name_without_ext] = Textdb::Data::Value.new(File.join(@path, name), self)
59
- self[name_without_ext]
59
+ begin
60
+ self[name_without_ext]
61
+ rescue Textdb::ExistError
62
+ self[name_without_ext] = Textdb::Data::Value.new(File.join(@path, name), self)
63
+ self[name_without_ext]
64
+ end
60
65
  end
61
66
 
62
67
  def create_key(name)
63
- if Textdb.config.listen
64
- Textdb::Event.listener.create_skip << File.join(@path, name)
65
- end
68
+ to_return = build_key(name)
66
69
 
67
70
  Dir.mkdir(File.join(@full_path, name))
68
71
 
69
- build_key(name)
72
+ to_return
70
73
  end
71
74
 
72
75
  def create_value(name)
73
76
  name = "#{name}#{Textdb.config.data_file_extension}"
74
77
 
75
- if Textdb.config.listen
76
- Textdb::Event.listener.create_skip << File.join(@path, name)
77
- end
78
-
78
+ to_return = build_value(name)
79
+
79
80
  File.open(File.join(@full_path, name), 'w') { |f| f.write('') }
80
- build_value(name)
81
+
82
+ to_return
81
83
  end
82
84
 
83
85
  def show
@@ -25,6 +25,10 @@ module Textdb
25
25
  @data = File.read(@full_path)
26
26
  end
27
27
 
28
+ def nil_data
29
+ @data = nil
30
+ end
31
+
28
32
  def update(value)
29
33
  File.open(@full_path, 'w') { |f| f.write(value) }
30
34
  show!
@@ -32,8 +36,8 @@ module Textdb
32
36
 
33
37
  def destroy
34
38
  begin
35
- File.delete(@full_path)
36
- parent.delete(@name)
39
+ destroy_file
40
+ destroy_value
37
41
  rescue
38
42
  return false
39
43
  end
@@ -41,6 +45,14 @@ module Textdb
41
45
  return true
42
46
  end
43
47
 
48
+ def destroy_file
49
+ File.delete(@full_path)
50
+ end
51
+
52
+ def destroy_value
53
+ parent.delete(@name)
54
+ end
55
+
44
56
  def inspect
45
57
  %{#<Value::0x#{object_id} @data="#{@data}">}
46
58
  end
@@ -4,10 +4,7 @@ module Textdb
4
4
  module Event
5
5
  class Listener
6
6
 
7
- attr_accessor :create_skip
8
-
9
7
  def initialize
10
- @create_skip = []
11
8
  end
12
9
 
13
10
  def config
@@ -1,13 +1,15 @@
1
1
  module Textdb
2
2
  module Event
3
3
  class Processor
4
-
4
+
5
5
  def initialize
6
6
  end
7
7
 
8
- def create(files)
9
- puts "create: ---#{files}---"
8
+ def config
9
+ Textdb.config
10
+ end
10
11
 
12
+ def create(files)
11
13
  files.each do |file|
12
14
  pointer, value = get(file, true)
13
15
  pointer.build_value(value)
@@ -15,11 +17,23 @@ module Textdb
15
17
  end
16
18
 
17
19
  def update(files)
18
- puts "update: ---#{files}---"
20
+ files.each do |file|
21
+ pointer, value = get(file)
22
+
23
+ value = remove_ext(value)
24
+
25
+ pointer[value].nil_data
26
+ end
19
27
  end
20
28
 
21
29
  def delete(files)
22
- puts "delete: ---#{files}---"
30
+ files.each do |file|
31
+ pointer, value = get(file)
32
+
33
+ value = remove_ext(value)
34
+
35
+ pointer[value].destroy_value
36
+ end
23
37
  end
24
38
 
25
39
  def get(relative, create = false)
@@ -34,57 +48,17 @@ module Textdb
34
48
  if create
35
49
  pointer = pointer.build_key(key)
36
50
  else
37
- pointer[key]
51
+ pointer = pointer[key]
38
52
  end
39
53
  end
40
54
  end
41
55
 
42
56
  return pointer, value
43
57
  end
44
-
45
- # def get(r, remove_ext = false)
46
- # keys = r.split('/')
47
- # last = keys.pop
48
58
 
49
- # if remove_ext
50
- # last = last.gsub(config.data_file_extension, '')
51
- # end
52
-
53
- # pointer = Textdb.root
54
- # keys.each do |key|
55
- # pointer = pointer[key]
56
- # end
57
-
58
- # return pointer, last
59
- # end
60
-
61
- # def create(b, r, t)
62
- # pointer, last = get(r)
63
-
64
- # unless @create_skip.delete('/' + r).nil?
65
- # return pointer[last]
66
- # end
67
-
68
- # if t == :directory
69
- # pointer.build_key(last)
70
- # else
71
- # pointer.build_value(last)
72
- # end
73
-
74
- # puts "create (directory: #{t == :directory ? "true " : "false"}): ---#{r}---"
75
- # end
76
-
77
- # def update(b, r, t)
78
- # pointer, last = get(r, true)
79
-
80
- # pointer[last].show!
81
-
82
- # puts "update (directory: #{t == :directory ? "true " : "false"}): ---#{r}---"
83
- # end
84
-
85
- # def delete(b, r, t)
86
- # puts "delete (directory: #{t == :directory ? "true " : "false"}): ---#{r}---"
87
- # end
59
+ def remove_ext(name)
60
+ name.gsub(config.data_file_extension, '')
61
+ end
88
62
 
89
63
  end
90
64
  end
@@ -1,3 +1,3 @@
1
1
  module Textdb
2
- VERSION = "0.2.0.beta"
2
+ VERSION = "0.3.0.beta"
3
3
  end
@@ -39,9 +39,23 @@ describe "Textdb.create" do
39
39
  expect { Textdb.create }.to raise_error(Textdb::BlockRequired)
40
40
  end
41
41
 
42
- context "listen for changes" do
43
-
42
+ context "listen" do
44
43
 
44
+ it "create a file" do
45
+ expect { Textdb.read { key2.value2_2 } }.to raise_error(Textdb::ExistError)
46
+
47
+ File.open(File.join(Textdb.config.base_folder, 'key2', 'value2_2.data'), 'w') { |f| f.write("text") }
48
+
49
+ sleep 0.5
50
+
51
+ expect(Textdb.read { key2.value2_2 }).to eql("text")
52
+ end
53
+
54
+ it ".create overrites .listen.create" do
55
+ value_class = Textdb.create { key2.value2_3 }
56
+
57
+ expect( Textdb.root['key2']['value2_3'] ).to eql(value_class)
58
+ end
45
59
 
46
60
  end
47
61
 
@@ -1,8 +1,11 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe "Textdb.update" do
4
+ let(:base_folder) { Textdb.config.base_folder }
5
+ let(:data_file_ext) { Textdb.config.data_file_extension }
6
+
4
7
  it "create with update" do
5
- file = File.join RSpec.configuration.dir, "key3", "value3_1#{Textdb.config.data_file_extension}"
8
+ file = File.join base_folder, "key3", "value3_1#{data_file_ext}"
6
9
 
7
10
  expect( File.exist?(file) ).to eql(false)
8
11
 
@@ -14,11 +17,28 @@ describe "Textdb.update" do
14
17
  end
15
18
 
16
19
  it "update" do
17
- file = File.join RSpec.configuration.dir, "key3", "value3_1#{Textdb.config.data_file_extension}"
20
+ file = File.join base_folder, "key3", "value3_1#{data_file_ext}"
18
21
 
19
22
  Textdb.update("text 2") { key3.value3_1 }
20
23
 
21
24
  expect( File.read(file) ).to eql("text 2")
22
25
  end
23
26
 
27
+ context "listen" do
28
+ it "update a file" do
29
+ Textdb.create { key3.value3_2 }.update("text")
30
+
31
+ sleep 0.5
32
+
33
+ expect( Textdb.read { key3.value3_2 } ).to eql("text")
34
+
35
+ file = File.join Textdb.config.base_folder, "key3", "value3_2#{data_file_ext}"
36
+
37
+ File.open(file, 'w') { |f| f.write("text 2") }
38
+
39
+ sleep 0.5
40
+
41
+ expect( Textdb.read { key3.value3_2 } ).to eql("text 2")
42
+ end
43
+ end
24
44
  end
@@ -1,6 +1,9 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe "Textdb.delete" do
4
+ let(:base_folder) { Textdb.config.base_folder }
5
+ let(:data_file_ext) { Textdb.config.data_file_extension }
6
+
4
7
  before(:all) do
5
8
  Textdb.create { key5.key5_1.value5_1_1 }
6
9
  Textdb.create { key5.key5_1.value5_1_2 }
@@ -11,7 +14,7 @@ describe "Textdb.delete" do
11
14
  end
12
15
 
13
16
  it "value" do
14
- file = File.join Textdb.config.base_folder, "key5", "key5_1", "value5_1_1#{Textdb.config.data_file_extension}"
17
+ file = File.join base_folder, "key5", "key5_1", "value5_1_1#{data_file_ext}"
15
18
 
16
19
  expect( File.exist?(file) ).to eql(true)
17
20
 
@@ -23,7 +26,7 @@ describe "Textdb.delete" do
23
26
  end
24
27
 
25
28
  it "key" do
26
- dir = File.join Textdb.config.base_folder, "key6"
29
+ dir = File.join base_folder, "key6"
27
30
 
28
31
  expect( Dir.exist?(dir) ).to eql(true)
29
32
 
@@ -35,7 +38,7 @@ describe "Textdb.delete" do
35
38
  end
36
39
 
37
40
  it "recursive" do
38
- dir = File.join Textdb.config.base_folder, "key5"
41
+ dir = File.join base_folder, "key5"
39
42
 
40
43
  Textdb.delete { key5 }
41
44
 
@@ -45,4 +48,20 @@ describe "Textdb.delete" do
45
48
  expect { Textdb.read { key5.key5_1.value5_1_3 } }.to raise_error(Textdb::ExistError)
46
49
  expect { Textdb.read { key5.value5_1 } }.to raise_error(Textdb::ExistError)
47
50
  end
51
+
52
+ context "listen" do
53
+ it "delete a file" do
54
+ Textdb.create { key5.key5_2.value5_2_1 }
55
+
56
+ sleep 0.5
57
+
58
+ file = File.join base_folder, "key5", "key5_2", "value5_2_1#{data_file_ext}"
59
+
60
+ File.delete(file)
61
+
62
+ sleep 0.5
63
+
64
+ expect { Textdb.read { key5.key5_2.value5_2_1 } }.to raise_error(Textdb::ExistError)
65
+ end
66
+ end
48
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: text-db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0.beta
4
+ version: 0.3.0.beta
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors: