poros 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e7ec142e948b6710c081338f06724f8563917a217fa8f87311114e51eed3bcc9
4
- data.tar.gz: adc6edc71a8151feb56cd5ceb642d973afc9c160d1ce90d7afb7d535332b45df
3
+ metadata.gz: cb33cb3edeb46a15e44bb915e71420a9c92ed99234ee52ab3d083b2e963b9807
4
+ data.tar.gz: 503c54369c8579a4a0d7fa41a2ae5bdb3cbdd59e8b4a4c999ad531d104858825
5
5
  SHA512:
6
- metadata.gz: 41f991b29ee7c2e91e959a07c87d2e5fefe296c1467db50adfb586710656b29a9963aa97d631c8b479a5c89e2e58f72e2cb19bd0278020d1c1ef59b96a50044d
7
- data.tar.gz: 02f9a28651945c27e73564a1d9d003503e3336f771c18706dd3a9de23fd458d40c5d013a2dfe2b4f34f2a6f3c84909d88e20a59ecb464a2abfc3504f78e3da50
6
+ metadata.gz: 02f0e7d5fbb462f9e4e29595c8d2e22fc775410b4b7782e1db43c787f078c4b8b07c61ebb2b57c4ea35215a4f1074faadc1bdbd297be29dd409f493c5a5bf31b
7
+ data.tar.gz: 95c434c7a70309d7bf2181dfbee83c9e0856969bff04051353b8b06c1c0391768e7802e3fe37457b712893b7950af959f75e7f0a75b1cfb866ccfc92fb256752
@@ -1,169 +1,171 @@
1
- module Poros::ClassMethods
2
- attr_accessor :in_transaction, :data_changed
3
-
4
- def poro_attr(*attrs)
5
- @poro_attrs = [:uuid] | attrs
6
- attrs.each { |column|
7
- class_eval "attr_accessor :#{column}"
8
- }
9
- end
10
-
11
- def poro_attrs
12
- @poro_attrs ||= []
13
- end
14
-
15
- def poro_index(*attrs)
16
- @poro_indexes ||= []
17
- @poro_indexes += attrs
18
- end
19
-
20
- def poro_indexes
21
- @poro_indexes ||= []
22
- end
1
+ module Poros
2
+ module ClassMethods
3
+ attr_accessor :in_transaction, :data_changed
4
+
5
+ def poro_attr(*attrs)
6
+ @poro_attrs = [:uuid] | attrs
7
+ attrs.each { |column|
8
+ class_eval "attr_accessor :#{column}"
9
+ }
10
+ end
23
11
 
24
- def find(uuid)
25
- attrs = YAML.load(File.read(file_path(uuid)))
26
- attrs.delete(:uuid)
12
+ def poro_attrs
13
+ @poro_attrs ||= []
14
+ end
27
15
 
28
- object = new(attrs)
29
- object.uuid = uuid
30
- object
31
- end
16
+ def poro_index(*attrs)
17
+ @poro_indexes ||= []
18
+ @poro_indexes += attrs
19
+ end
32
20
 
33
- def file_path(uuid)
34
- FileUtils.mkdir_p(data_directory) unless File.exist?(data_directory)
35
- File.join(data_directory, file_name(uuid))
36
- end
21
+ def poro_indexes
22
+ @poro_indexes ||= []
23
+ end
37
24
 
38
- def data_directory
39
- "./db/#{self}/"
40
- end
25
+ def find(uuid)
26
+ attrs = YAML.load(File.read(file_path(uuid)))
27
+ attrs.delete(:uuid)
41
28
 
42
- def file_name(uuid)
43
- "#{uuid}.yml"
44
- end
29
+ object = new(attrs)
30
+ object.uuid = uuid
31
+ object
32
+ end
45
33
 
46
- def index_file
47
- File.join(data_directory, "indexes.yml")
48
- end
34
+ def file_path(uuid)
35
+ FileUtils.mkdir_p(data_directory) unless File.exist?(data_directory)
36
+ File.join(data_directory, file_name(uuid))
37
+ end
49
38
 
50
- def all
51
- Dir.glob(File.join(data_directory, '*.yml')).map { |file|
52
- next if file == index_file
53
- data = YAML.load(File.read(file))
54
- find(data[:uuid])
55
- }.compact
56
- end
39
+ def data_directory
40
+ "./db/#{self}/"
41
+ end
57
42
 
58
- def where(query)
59
- indexed, table_scan = query.partition { |index, key| poro_indexes.include?(index) }
43
+ def file_name(uuid)
44
+ "#{uuid}.yml"
45
+ end
60
46
 
61
- indexed_results = indexed.map { |key, value|
62
- case value
63
- when Regexp
64
- index_data[key].keys.flat_map { |value_name|
65
- index_data[key][value_name] if value =~ value_name
66
- }.compact
67
- when Array
68
- value.flat_map { |value_name| index_data[key][value_name] }
69
- when Proc
70
- index_data[key].keys.flat_map { |value_name|
71
- index_data[key][value_name] if value.call(value_name)
72
- }.compact
73
- else
74
- index_data[key].has_key?(value) ?
75
- index_data[key][value] : []
76
- end
77
- }.inject(:&)
47
+ def index_file
48
+ File.join(data_directory, "indexes.yml")
49
+ end
78
50
 
79
- if table_scan.size > 0
80
- scanned_results = Dir.glob(File.join(data_directory, '*.yml')).map { |file|
51
+ def all
52
+ Dir.glob(File.join(data_directory, '*.yml')).map { |file|
81
53
  next if file == index_file
82
54
  data = YAML.load(File.read(file))
83
- data[:uuid] if table_scan.all? { |key, value|
84
- case value
85
- when Regexp
86
- value =~ data[key]
87
- when Array
88
- value.include?(data[key])
89
- when Proc
90
- value.call(data[key])
91
- else
92
- data[key] == value
93
- end
94
- }
55
+ find(data[:uuid])
95
56
  }.compact
96
57
  end
97
58
 
98
- if indexed.size > 0 && table_scan.size > 0
99
- results = indexed_results & scanned_results
100
- elsif indexed.size > 0
101
- results = indexed_results
102
- else
103
- results = scanned_results
59
+ def where(query)
60
+ indexed, table_scan = query.partition { |index, key| poro_indexes.include?(index) }
61
+
62
+ indexed_results = indexed.map { |key, value|
63
+ case value
64
+ when Regexp
65
+ index_data[key].keys.flat_map { |value_name|
66
+ index_data[key][value_name] if value =~ value_name
67
+ }.compact
68
+ when Array
69
+ value.flat_map { |value_name| index_data[key][value_name] }
70
+ when Proc
71
+ index_data[key].keys.flat_map { |value_name|
72
+ index_data[key][value_name] if value.call(value_name)
73
+ }.compact
74
+ else
75
+ index_data[key].has_key?(value) ?
76
+ index_data[key][value] : []
77
+ end
78
+ }.inject(:&)
79
+
80
+ if table_scan.size > 0
81
+ scanned_results = Dir.glob(File.join(data_directory, '*.yml')).map { |file|
82
+ next if file == index_file
83
+ data = YAML.load(File.read(file))
84
+ data[:uuid] if table_scan.all? { |key, value|
85
+ case value
86
+ when Regexp
87
+ value =~ data[key]
88
+ when Array
89
+ value.include?(data[key])
90
+ when Proc
91
+ value.call(data[key])
92
+ else
93
+ data[key] == value
94
+ end
95
+ }
96
+ }.compact
97
+ end
98
+
99
+ if indexed.size > 0 && table_scan.size > 0
100
+ results = indexed_results & scanned_results
101
+ elsif indexed.size > 0
102
+ results = indexed_results
103
+ else
104
+ results = scanned_results
105
+ end
106
+
107
+ results.map { |uuid| find(uuid) }
104
108
  end
105
109
 
106
- results.map { |uuid| find(uuid) }
107
- end
110
+ def index_data
111
+ return @index_data if @index_data
108
112
 
109
- def index_data
110
- return @index_data if @index_data
113
+ data = File.exist?(index_file) ? YAML.load(File.read(index_file)) : {}
114
+ # Make sure we always have every index as a key
115
+ poro_indexes.each do |index|
116
+ data[index] = {} unless data.has_key?(index)
117
+ end
111
118
 
112
- data = File.exist?(index_file) ? YAML.load(File.read(index_file)) : {}
113
- # Make sure we always have every index as a key
114
- poro_indexes.each do |index|
115
- data[index] = {} unless data.has_key?(index)
119
+ @index_data = data
116
120
  end
117
121
 
118
- @index_data = data
119
- end
122
+ def write_index_data
123
+ File.write(index_file, @index_data.to_yaml)
124
+ end
120
125
 
121
- def write_index_data
122
- File.write(index_file, @index_data.to_yaml)
123
- end
126
+ def update_index(object)
127
+ remove_from_index(object, false)
124
128
 
125
- def update_index(object)
126
- remove_from_index(object, false)
129
+ index_data
127
130
 
128
- index_data
131
+ poro_indexes.each do |index|
132
+ @index_data[index] = {} unless @index_data.has_key?(index)
133
+ value = object.send(index)
134
+ @index_data[index][value] ||= []
135
+ @index_data[index][value] = @index_data[index][value] | [object.uuid]
136
+ end
129
137
 
130
- poro_indexes.each do |index|
131
- @index_data[index] = {} unless @index_data.has_key?(index)
132
- value = object.send(index)
133
- @index_data[index][value] ||= []
134
- @index_data[index][value] = @index_data[index][value] | [object.uuid]
138
+ write_index_data unless @in_transaction
135
139
  end
136
140
 
137
- write_index_data unless @in_transaction
138
- end
139
-
140
- def remove_from_index(object, perist = true)
141
- index_data
141
+ def remove_from_index(object, perist = true)
142
+ index_data
142
143
 
143
- poro_indexes.each do |index|
144
- @index_data[index] = {} unless @index_data.has_key?(index)
145
- @index_data[index].keys.each do |value|
146
- @index_data[index][value] ||= []
147
- @index_data[index][value] -= [object.uuid]
144
+ poro_indexes.each do |index|
145
+ @index_data[index] = {} unless @index_data.has_key?(index)
146
+ @index_data[index].keys.each do |value|
147
+ @index_data[index][value] ||= []
148
+ @index_data[index][value] -= [object.uuid]
149
+ end
148
150
  end
151
+ write_index_data if !@in_transaction && perist
149
152
  end
150
- write_index_data if !@in_transaction && perist
151
- end
152
153
 
153
- def rebuild_indexes
154
- transaction do
155
- @data_changed = true
156
- @index_data = {}
157
- File.delete(index_file) if File.exist?(index_file)
158
- all.each { |object| update_index(object) }
154
+ def rebuild_indexes
155
+ transaction do
156
+ @data_changed = true
157
+ @index_data = {}
158
+ File.delete(index_file) if File.exist?(index_file)
159
+ all.each { |object| update_index(object) }
160
+ end
159
161
  end
160
- end
161
162
 
162
- def transaction(&block)
163
- @in_transaction = true
164
- @data_changed = false
165
- block.call
166
- @in_transaction = false
167
- write_index_data if @data_changed
163
+ def transaction(&block)
164
+ @in_transaction = true
165
+ @data_changed = false
166
+ block.call
167
+ @in_transaction = false
168
+ write_index_data if @data_changed
169
+ end
168
170
  end
169
171
  end
@@ -1,25 +1,27 @@
1
1
  require 'poros/info'
2
2
 
3
- module Poros::InstanceMethods
4
- attr_accessor :uuid
5
- def uuid
6
- @uuid ||= SecureRandom.uuid
7
- end
3
+ module Poros
4
+ module InstanceMethods
5
+ attr_accessor :uuid
6
+ def uuid
7
+ @uuid ||= SecureRandom.uuid
8
+ end
8
9
 
9
- def poros
10
- @poros ||= Poros::Info.new(self)
11
- end
10
+ def poros
11
+ @poros ||= Poros::Info.new(self)
12
+ end
12
13
 
13
- def destroy
14
- File.delete(poros.file_path)
15
- self.class.remove_from_index(self)
16
- self
17
- end
14
+ def destroy
15
+ File.delete(poros.file_path)
16
+ self.class.remove_from_index(self)
17
+ self
18
+ end
18
19
 
19
- def save
20
- File.write(poros.file_path, poros.to_h.to_yaml)
21
- self.class.data_changed = true
22
- self.class.update_index(self)
23
- self
20
+ def save
21
+ File.write(poros.file_path, poros.to_h.to_yaml)
22
+ self.class.data_changed = true
23
+ self.class.update_index(self)
24
+ self
25
+ end
24
26
  end
25
27
  end
data/lib/poros/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Poros
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: poros
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Ross Earle