paneron-register 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.irbrc +1 -0
- data/.pryrc +1 -0
- data/README.adoc +163 -19
- data/docs/examples.adoc +91 -0
- data/lib/paneron/register/hierarchical.rb +24 -0
- data/lib/paneron/register/raw/data_set.rb +349 -34
- data/lib/paneron/register/raw/item.rb +147 -23
- data/lib/paneron/register/raw/item_class.rb +140 -23
- data/lib/paneron/register/raw/register.rb +452 -63
- data/lib/paneron/register/register.rb +0 -2
- data/lib/paneron/register/root_finder.rb +24 -0
- data/lib/paneron/register/validatable.rb +40 -0
- data/lib/paneron/register/version.rb +1 -1
- data/lib/paneron/register/writeable.rb +96 -0
- data/lib/paneron/register.rb +5 -0
- data/paneron-register.gemspec +1 -0
- metadata +21 -2
@@ -1,37 +1,123 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "yaml"
|
4
|
-
|
5
3
|
module Paneron
|
6
4
|
module Register
|
7
5
|
module Raw
|
8
6
|
class ItemClass
|
9
|
-
|
10
|
-
|
7
|
+
include Writeable
|
8
|
+
include Validatable
|
9
|
+
include RootFinder
|
10
|
+
|
11
|
+
attr_reader :item_class_name, :extension
|
12
|
+
|
13
|
+
attr_accessor :data_set
|
11
14
|
|
12
15
|
def initialize(
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
+
item_class_path = nil,
|
17
|
+
extension = "yaml",
|
18
|
+
data_set: nil
|
16
19
|
)
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
20
|
+
|
21
|
+
unless item_class_path.nil?
|
22
|
+
data_set_path, item_class_name = Hierarchical.split_path(item_class_path)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Deduce parent from self path,
|
26
|
+
# if only self path is specified.
|
27
|
+
@data_set = if data_set.nil?
|
28
|
+
Paneron::Register::Raw::DataSet.new(
|
29
|
+
data_set_path,
|
30
|
+
)
|
31
|
+
else
|
32
|
+
data_set
|
33
|
+
end
|
34
|
+
|
21
35
|
@item_class_name = item_class_name
|
22
|
-
@
|
36
|
+
@old_name = @item_class_name
|
23
37
|
@items_uuids = nil
|
24
38
|
@items = {}
|
39
|
+
@extension = extension
|
40
|
+
end
|
41
|
+
|
42
|
+
def item_class_path
|
43
|
+
File.join(data_set_path, item_class_name)
|
44
|
+
end
|
45
|
+
|
46
|
+
def item_class_name=(new_item_class_name)
|
47
|
+
if new_item_class_name.nil? || new_item_class_name.empty?
|
48
|
+
raise Paneron::Register::Error, "Item class name cannot be empty"
|
49
|
+
end
|
50
|
+
|
51
|
+
unless new_item_class_name.is_a?(String)
|
52
|
+
raise Paneron::Register::Error, "Item class name must be a string"
|
53
|
+
end
|
54
|
+
|
55
|
+
@item_class_name = new_item_class_name
|
25
56
|
end
|
26
57
|
|
27
|
-
def
|
58
|
+
def parent
|
59
|
+
data_set
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.name
|
63
|
+
"Item class"
|
64
|
+
end
|
65
|
+
|
66
|
+
def save_sequence
|
67
|
+
# Save self
|
68
|
+
require "fileutils"
|
69
|
+
|
70
|
+
# Move old data set to new path
|
71
|
+
old_path = File.join(data_set_path, @old_name)
|
72
|
+
if File.directory?(old_path) && @old_name != item_class_name
|
73
|
+
FileUtils.mv(old_path, self_path)
|
74
|
+
@old_name = item_class_name
|
75
|
+
else
|
76
|
+
FileUtils.mkdir_p(self_path)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Save items
|
80
|
+
item_uuids.each do |item_uuid|
|
81
|
+
items(item_uuid).save
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def self_path
|
86
|
+
item_class_path
|
87
|
+
end
|
88
|
+
|
89
|
+
def is_valid?
|
90
|
+
data_set.valid?
|
91
|
+
end
|
92
|
+
|
93
|
+
def set_data_set(new_data_set)
|
94
|
+
@data_set = new_data_set
|
95
|
+
end
|
96
|
+
|
97
|
+
def data_set_path
|
98
|
+
data_set.data_set_path
|
99
|
+
end
|
100
|
+
|
101
|
+
def add_items(new_items)
|
102
|
+
new_items = [new_items] unless new_items.is_a?(Enumerable)
|
103
|
+
new_items.each do |item|
|
104
|
+
item.set_item_class(self)
|
105
|
+
@items[item.uuid] = item
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.validate_path_before_saving
|
110
|
+
true
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.validate_path(path)
|
28
114
|
unless File.exist?(path)
|
29
115
|
raise Paneron::Register::Error,
|
30
|
-
"Item class path does not exist"
|
116
|
+
"Item class path (#{path}) does not exist"
|
31
117
|
end
|
32
118
|
unless File.directory?(path)
|
33
119
|
raise Paneron::Register::Error,
|
34
|
-
"Item class path is not a directory"
|
120
|
+
"Item class path (#{path}) is not a directory"
|
35
121
|
end
|
36
122
|
end
|
37
123
|
|
@@ -42,21 +128,52 @@ module Paneron
|
|
42
128
|
)
|
43
129
|
end
|
44
130
|
|
131
|
+
def add_item(*new_items)
|
132
|
+
new_items = [new_items] unless new_items.is_a?(Enumerable)
|
133
|
+
new_items.each do |item|
|
134
|
+
item.set_item_class(self)
|
135
|
+
@items[item.item_uuid] = item
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def spawn_item(item_uuid = nil)
|
140
|
+
new_item = Paneron::Register::Raw::Item.new(
|
141
|
+
item_uuid,
|
142
|
+
item_class: self,
|
143
|
+
)
|
144
|
+
|
145
|
+
add_item(new_item)
|
146
|
+
|
147
|
+
new_item
|
148
|
+
end
|
149
|
+
|
45
150
|
def item_uuids
|
46
|
-
@
|
47
|
-
.
|
151
|
+
if @items.empty?
|
152
|
+
Dir.glob(File.join(item_class_path, "*.#{extension}"))
|
153
|
+
.map { |file| File.basename(file, ".#{extension}") }.to_set
|
154
|
+
else
|
155
|
+
@items.keys
|
156
|
+
end
|
48
157
|
end
|
49
158
|
|
50
|
-
def items(uuid = nil)
|
159
|
+
def items(uuid = nil, refresh: false)
|
51
160
|
if uuid.nil?
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
161
|
+
@items = if !refresh && !@items.empty?
|
162
|
+
@items
|
163
|
+
else
|
164
|
+
item_uuids.reduce({}) do |acc, uuid|
|
165
|
+
acc[uuid] = items(uuid)
|
166
|
+
acc
|
167
|
+
end
|
168
|
+
end
|
169
|
+
elsif refresh
|
170
|
+
items[uuid]
|
56
171
|
else
|
57
172
|
@items[uuid] ||=
|
58
173
|
Paneron::Register::Raw::Item.new(
|
59
|
-
|
174
|
+
uuid,
|
175
|
+
item_class_path,
|
176
|
+
item_class: self,
|
60
177
|
)
|
61
178
|
end
|
62
179
|
end
|