jsondb 0.0.2 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,73 +1,83 @@
1
1
  module JSONdb
2
+
2
3
  class FileOps
3
4
 
4
- attr_reader :writeable, :filename, :folder, :raw, :new_file
5
+ include JSONdb::Logger
6
+
7
+ attr_reader :folder, :writeable, :filename, :filetype, :new_file
8
+
9
+ attr_accessor :contents
5
10
 
6
- def initialize(folder, filename, filetype, writeable = true)
11
+ def initialize(folder, filename, filetype, json = true)
7
12
  @folder = folder
8
- @writeable = writeable
9
- @filename = File.join(@folder, filename)
13
+ @filename = File.expand_path(File.join(@folder, filename))
10
14
  @filetype = filetype
11
- if exists?(@folder)
12
- load
13
- else
14
- raise "Folder '#{@folder}' does not exists."
15
- end
15
+ @contents = ""
16
+ @json = json
16
17
  end
17
18
 
18
- def contents=(contents)
19
- @contents = contents
19
+ def read
20
+ begin
21
+ if File.exists?(@filename)
22
+ file = File.open(@filename, 'r')
23
+ raw = file.read
24
+ @new_file = false
25
+ log("File '#{@filename}' readed.", :debug)
26
+ else
27
+ raw = JSONdb.constants.default_file_content[@filetype]
28
+ @new_file = true
29
+ end
30
+ if @json == true
31
+ @contents = JSON.parse(raw)
32
+ else
33
+ @contents = raw
34
+ end
35
+ file.close if file
36
+ log("File '#{@filename}' parsed.", :debug)
37
+ return true
38
+ rescue
39
+ log("File '#{@filename}' could not read the file.", :error)
40
+ return false
41
+ end
20
42
  end
21
43
 
22
- def contents
23
- @contents
44
+ def write
45
+ begin
46
+ file = File.open(@filename, 'w')
47
+ if @json == true
48
+ file.write(JSON.pretty_generate(@contents))
49
+ else
50
+ file.write(@contents)
51
+ end
52
+ file.close
53
+ @new_file = false
54
+ log("File '#{@filename}' content updated.", :debug)
55
+ return true
56
+ rescue
57
+ log("File '#{@filename}' could not open and write the file.", :error)
58
+ return false
59
+ end
24
60
  end
25
61
 
26
- def save
27
- return false if @writeable == false
28
- @file = File.open(@filename, 'w')
29
- @raw = JSON.pretty_generate(@contents)
30
- @file.write(@raw)
31
- @file.close
32
- @new_file = false
33
- return true
62
+ def write_line(line)
63
+ begin
64
+ file = File.open(@filename, 'a+')
65
+ file.write("#{line}\n")
66
+ file.close
67
+ @new_file = false
68
+ rescue
69
+ log("Couldn't write line on file '#{@filename}'.", :error)
70
+ end
34
71
  end
35
72
 
36
73
  def destroy
37
- File.delete(@filename) if exists?(@filename)
38
- end
39
-
40
- def close
41
- @file.close if @file
42
- end
43
-
44
- private
45
-
46
- def exists?(file)
47
- File.exists?(File.expand_path(file))
48
- end
49
-
50
- def load
51
- @new_file = !exists?(@filename)
52
- if exists?(@filename)
53
- @file = File.open(@filename, 'r')
54
- @raw = @file.read
74
+ if File.exists?(@filename)
75
+ File.delete(@filename)
76
+ log("File '#{@filename}' deleted.", :info)
55
77
  else
56
- @raw = default_content
78
+ log("File '#{@filename}' does not exists.", :error)
57
79
  end
58
- @contents = JSON.parse(@raw)
59
- @file.close if @file
60
80
  end
61
81
 
62
- def default_content
63
- case @filetype
64
- when 'db'
65
- '{ "tables": {} }'
66
- when 'table_structure'
67
- '{ "last_id" : 0, "fields": {} }'
68
- when 'table_records'
69
- '{ }'
70
- end
71
- end
72
- end
73
- end
82
+ end # class
83
+ end # module
@@ -0,0 +1,36 @@
1
+ module JSONdb
2
+
3
+ module Logger
4
+
5
+ # LogLevels = {
6
+ # debug: 1,
7
+ # info: 2,
8
+ # error: 3
9
+ # }
10
+
11
+ def log(msg, log_level = :debug, force_raise_error = false)
12
+ log("Log level not allowed: '#{log_level}", :error) if !allowed_log_level?(log_level)
13
+
14
+ if log_enabled? and log_this?(log_level)
15
+ @log_file ||= FileOps.new(JSONdb.settings.log_folder, JSONdb.settings.log_file, 'log', false)
16
+ @log_file.write_line("#{Time.now} #{log_level.to_s.upcase} #{self.class.to_s} #{msg}")
17
+ puts "#{Time.now} #{log_level.to_s.upcase} #{self.class.to_s}: #{msg} " if JSONdb.settings.verbose
18
+ end
19
+ raise "#{log_level.to_s.upcase} #{self.class.to_s} #{msg}" if (JSONdb.settings.raise_errors == true or force_raise_error == true) and log_level == :error
20
+ end
21
+
22
+ def allowed_log_level?(log_level)
23
+ JSONdb.constants.loglevels.keys.include?(log_level)
24
+ end
25
+
26
+ def log_this?(log_level)
27
+ JSONdb.constants.loglevels[log_level] >= JSONdb.constants.loglevels[JSONdb.settings.log_level]
28
+ end
29
+
30
+ def log_enabled?
31
+ JSONdb.settings.log_enabled
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -1,92 +1,57 @@
1
1
  module JSONdb
2
- class Record
3
2
 
4
- include Validation
3
+ class Record
4
+
5
+ include JSONdb::Validations::Types
6
+ include JSONdb::Logger
7
+
8
+ attr_reader :table_name
9
+
10
+ def initialize(table_name, record = nil)
11
+ @record = Hash.new
12
+
13
+ @table_name = table_name
14
+ if record.nil?
15
+ @new_record = true
16
+ @persisted = false
17
+ else
18
+ @new_record = false
19
+ @persisted = true
20
+ @record = record
21
+ end
22
+ end
23
+
24
+ def fields
25
+ JSONdb.fields[table_name].to_hash.keys
26
+ end
27
+
28
+ def to_hash
29
+ @record
30
+ end
31
+
32
+ def set_default_if_nil(name)
33
+ if @record[name].nil?
34
+ @record[name] = JSONdb.fields[table_name][name].default if JSONdb.fields[table_name][name].default.nil? == false
35
+ end
36
+ return @record[name]
37
+ end
38
+
39
+ def method_missing(name, *args)
40
+ name = name.to_s
41
+
42
+ if JSONdb.fields[table_name].to_hash.keys.include?(name.sub('=', ''))
43
+ if name =~ /=$/
44
+ if @record[name] != args[0]
45
+ @record[name.sub('=', '')] = args[0]
46
+ end
47
+ else
48
+ set_default_if_nil(name) # returns default value if value(name) is nil
49
+ end
50
+ else
51
+ log("Column '#{name.sub('=', '')}' do not exists", :error)
52
+ end
53
+ end
54
+
55
+ end
5
56
 
6
- def initialize(fields, data = nil)
7
- @fields = fields
8
- if data.nil?
9
- @record = Hash.new
10
- @new = true
11
- self.id = 0
12
- else
13
- @record = data
14
- @new = false
15
- end
16
- @persisted = false
17
- set_defaults # we need to apply again the defaults
18
- end
19
-
20
- def method_missing(name, *args)
21
- attribute = name.to_s
22
-
23
- if @fields.to_hash.keys.include?(attribute.sub('=', ''))
24
- if attribute =~ /=$/
25
- if @record[attribute] != args[0]
26
- @record[attribute.sub('=', '')] = args[0]
27
- @persisted = true
28
- end
29
- else
30
- @record[attribute]
31
- end
32
- else
33
- raise "Column '#{attribute.sub('=', '')}' do not exists"
34
- end
35
- end
36
-
37
- def save_with_id(id)
38
- self.created_at = Time.now.to_i
39
- self.updated_at = Time.now.to_i
40
- self.id = id
41
- @new = false
42
- @persisted = false
43
- end
44
-
45
- def update
46
- self.updated_at = Time.now.to_i
47
- @new = false
48
- @persisted = false
49
- end
50
-
51
- def to_hash
52
- @record
53
- end
54
-
55
- def new?
56
- @new
57
- end
58
-
59
- def persisted?
60
- @persisted
61
- end
62
-
63
- def created_at
64
- Time.at(@record['created_at'])
65
- end
66
-
67
- def persisted_at
68
- Time.at(@record['updated_at'])
69
- end
70
-
71
- private
72
-
73
- def set_defaults
74
- @fields.each do |name, values|
75
- if values.default and @record[name].nil?
76
- @record[name] = values.default
77
- end
78
- end
79
- end
80
-
81
- # # TODO : Add Validation
82
-
83
- # def save!
84
- # raiseif(save, false, "Error saving the record.")
85
- # end
86
-
87
- # def save
88
- # return @table_class.save
89
- # end
90
-
91
- end
92
57
  end
@@ -0,0 +1,62 @@
1
+ module JSONdb
2
+
3
+ module Records
4
+
5
+ def record(key)
6
+ JSONdb.records[@name][key]
7
+ end
8
+
9
+ def new_record
10
+ return JSONdb::Record.new(@name)
11
+ end
12
+
13
+ def insert_record(record)
14
+ @persisted = false
15
+ @updated_at = Time.now.to_i # updated table updated_at time
16
+
17
+ record.id = new_id
18
+ record.created_at = Time.now.to_i
19
+ record.updated_at = Time.now.to_i
20
+ JSONdb.records[@name][record.id] = record
21
+ end
22
+
23
+ def update_record(record)
24
+ @persisted = false
25
+ JSONdb.records[@name][record.id] = record
26
+ end
27
+
28
+ def drop_record(record)
29
+ begin
30
+ @persisted = false
31
+ JSONdb.records[@name][record.id] = nil
32
+ JSONdb.records[@name].delete(record.id)
33
+ record = nil
34
+ return true
35
+ rescue
36
+ return false
37
+ end
38
+ end
39
+
40
+ def exists?(record)
41
+ !JSONdb.records[@name][record.id].nil?
42
+ end
43
+
44
+ def record_count
45
+ JSONdb.records[@name].keys.count
46
+ end
47
+
48
+ alias :count :record_count
49
+
50
+ def records_to_hash
51
+ to_hash = Hash.new
52
+ JSONdb.records[@name].each do |key, values|
53
+ to_hash = to_hash.merge({
54
+ key => values.to_hash
55
+ })
56
+ end
57
+ return to_hash
58
+ end
59
+
60
+ end
61
+
62
+ end
@@ -0,0 +1,126 @@
1
+ module JSONdb
2
+
3
+ class ResultSet
4
+
5
+ def initialize(table_name)
6
+ @table_name = table_name
7
+
8
+ @result = Hash.new
9
+
10
+ # TODO: It must not be here, jsut a workaround atm
11
+ JSONdb.records[@table_name].each do |k, v|
12
+ @result[k] = v.dup
13
+ end
14
+
15
+ @equal = Hash.new
16
+ @min = Hash.new
17
+ @max = Hash.new
18
+ @like = Hash.new
19
+ end
20
+
21
+ def equal(field, value)
22
+ @equal[field] = value
23
+ return self
24
+ end
25
+
26
+ def min(field, value)
27
+ @min[field] = value
28
+ return self
29
+ end
30
+
31
+ def max(field, value)
32
+ @max[field] = value
33
+ return self
34
+ end
35
+
36
+ def interval(field, min, max)
37
+ @min[field] = min
38
+ @max[field] = max
39
+ return self
40
+ end
41
+
42
+ def like(field, value)
43
+ @like[field] = value
44
+ return self
45
+ end
46
+
47
+ # TODO: For next version
48
+ # TODO: Change it, it must compare all records at the same time
49
+ # all_records.each do |r|
50
+ # eq.each do |k,v|
51
+ # ok if match
52
+ # ok_array << ok
53
+ # end
54
+ # if ok_Array.all == true
55
+ # then it match
56
+ def result
57
+ eq = result_equal if @equal.keys.count != 0
58
+ min = result_min if @min.keys.count != 0
59
+ max = result_max if @max.keys.count != 0
60
+ like = result_like if @like.keys.count != 0
61
+ return @result
62
+ end
63
+
64
+ private
65
+
66
+ def result_equal
67
+ @equal.each do |col_name, col_value|
68
+ this_result = Array.new
69
+ @result.each do |id, record|
70
+ r = record.send col_name.to_sym
71
+ if r == col_value
72
+ this_result << id
73
+ end
74
+ end
75
+ remove_if_key_not_in(this_result)
76
+ end
77
+ end
78
+
79
+ def result_min
80
+ @min.each do |col_name, col_value|
81
+ this_result = Array.new
82
+ @result.each do |id, record|
83
+ r = record.send col_name.to_sym
84
+ if r >= col_value
85
+ this_result << id
86
+ end
87
+ end
88
+ remove_if_key_not_in(this_result)
89
+ end
90
+ end
91
+
92
+ def result_max
93
+ @max.each do |col_name, col_value|
94
+ this_result = Array.new
95
+ @result.each do |id, record|
96
+ r = record.send col_name.to_sym
97
+ if r <= col_value
98
+ this_result << id
99
+ end
100
+ end
101
+ remove_if_key_not_in(this_result)
102
+ end
103
+ end
104
+
105
+ def result_like
106
+ @like.each do |col_name, col_value|
107
+ this_result = Array.new
108
+ @result.each do |id, record|
109
+ r = record.send col_name.to_sym
110
+ if r =~ Regexp.new(col_value)
111
+ this_result << id
112
+ end
113
+ end
114
+ remove_if_key_not_in(this_result)
115
+ end
116
+ end
117
+
118
+ def remove_if_key_not_in(arr)
119
+ @result.keys.each do |key|
120
+ @result.delete(key) if arr.include?(key) == false
121
+ end
122
+ end
123
+
124
+ end
125
+
126
+ end