3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
# File 'lib/isomorfeus_data/lucid_file/mixin.rb', line 3
def self.included(base)
base.extend(Isomorfeus::Data::GenericClassApi)
base.include(Isomorfeus::Data::GenericInstanceApi)
def changed!
@_changed = true
end
def to_transport
hash = {}
hash['revision'] = revision if revision
hash['data_uri'] = data_uri
result = { @class_name => { @key => hash }}
result.deep_merge!(@class_name => { @previous_key => { new_key: @key }}) if @previous_key
result
end
def content_type
_reload
return @_data_uri_object.content_type if @_data_uri_object
nil
end
def content_type=(c)
changed!
_content_type(c)
end
def _content_type(c)
_reload
@_data_uri_string = _format_data_uri(c, @_data_uri_object.data)
@_data_uri_object = URI::Data.new(@_data_uri_string)
c
end
def data
_reload
@_data_uri_object.data
end
def data=(d)
changed!
_data(d)
end
def _data(d)
_reload
@_data_uri_string = _format_data_uri(self.content_type, d)
@_data_uri_object = URI::Data.new(@_data_uri_string)
d
end
def data_uri
_reload
@_data_uri_string
end
def data_uri=(d)
changed!
_data_uri(d)
end
def _data_uri(d)
_reload
if d.class == URI::Data
@_data_uri_string = _format_data_uri(d.content_type, d.data)
@_data_uri_object = d
else
@_data_uri_string = d
@_data_uri_object = URI::Data.new(d)
end
@_data_uri_string
end
def data_uri_object
_reload
@_data_uri_object
end
def _format_data_uri(c, d)
"data:#{c};base64,#{Base64.encode64(d).chop}"
end
if RUBY_ENGINE == 'opal'
base.instance_exec do
def files_path
end
def files_path=(_)
end
end
def initialize(key: nil, content_type: nil, data: nil, data_uri: nil, revision: nil, _loading: false)
@key = key.nil? ? SecureRandom.uuid : key.to_s
@class_name = self.class.name
@class_name = @class_name.split('>::').last if @class_name.start_with?('#<')
_update_paths
@_revision = revision ? revision : Redux.fetch_by_path(:data_state, :revision, @class_name, @key)
@_changed = false
@_reload = false
loaded = loaded?
@_data_uri_object = nil
@_data_uri_string = nil
if data_uri
self.data_uri = data_uri
elsif data
self.data = data
self.content_type = content_type if content_type
else
self._data('')
self._content_type(content_type) if content_type
end
end
def _load_from_store!
@_changed = false
@_reload = true
end
def _reload
return unless @_reload
@_reload = false
d = Redux.fetch_by_path(*@_store_path)
self._data_uri(d) if d
end
def _update_paths
@_store_path = [:data_state, @class_name, @key, :data_uri]
end
else
Isomorfeus.add_valid_file_class(base) unless base == LucidFile::Base
base.instance_exec do
def instance_from_transport(instance_data, _included_items_data)
key = instance_data[self.name].keys.first
revision = instance_data[self.name][key].key?('revision') ? instance_data[self.name][key]['revision'] : nil
data_uri = instance_data[self.name][key].key?('data_uri') ? instance_data[self.name][key]['data_uri'] : nil
new(key: key, revision: revision, data_uri: data_uri)
end
def files_path
@files_path ||= Isomorfeus.files_path
end
def files_path=(f)
@files_path = f
end
def check_and_prepare_path(key:)
Isomorfeus.raise_error(message: 'Invalid key (contains ".." or "\\")') if key.include?('..') || key.include?('\\')
elements = key.split('/')
Isomorfeus.raise_error(message: 'Invalid key (contains more than 2 slashes "/")') if elements.size > 3
file = elements.pop
if elements.size > 0
dir_path = ::File.expand_path(::File.join(files_path, *elements))
FileUtils.mkdir_p(path) unless Dir.exist?(dir_path)
::File.join(dir_path, file)
else
FileUtils.mkdir_p(files_path) unless Dir.exist?(files_path)
::File.join(files_path, file)
end
end
execute_create do
self.save
end
execute_destroy do |key:|
file = check_and_prepare_path(key: key)
::FileUtils.rm_f(file)
true
end
execute_load do |key:|
file = check_and_prepare_path(key: key)
d = ::File.read(file)
instance = self.new(key: key)
instance.data = d
instance
end
execute_save do
file = self.class.check_and_prepare_path(key: self.key)
::File.write(file, self.data)
self
end
end
def initialize(key: nil, content_type: nil, data: nil, data_uri: nil, revision: nil)
@key = key.nil? ? SecureRandom.uuid : key.to_s
@class_name = self.class.name
@class_name = @class_name.split('>::').last if @class_name.start_with?('#<')
@_revision = revision
@_data_uri_object = nil
@_data_uri_string = nil
if data_uri
self._data_uri(data_uri)
else
self._data(data ? data : '')
self._content_type(content_type) if content_type
end
@_changed = false
end
def _reload
end
def _unchange!
@_data_uri = @_changed_data_uri if @_changed
@_changed = false
end
end
end
|