1
+ require 'set'
2
+
1
3
module ROCrate
2
4
##
3
5
# A Ruby abstraction of an RO-Crate.
@@ -20,8 +22,8 @@ def self.format_local_id(id)
20
22
##
21
23
# Initialize an empty RO-Crate.
22
24
def initialize ( id = IDENTIFIER , properties = { } )
23
- @data_entities = [ ]
24
- @contextual_entities = [ ]
25
+ @data_entities = Set . new
26
+ @contextual_entities = Set . new
25
27
super ( self , nil , id , properties )
26
28
end
27
29
@@ -144,8 +146,8 @@ def add_organization(id, properties = {})
144
146
# @return [Entity] the entity itself, or a clone of the entity "owned" by this crate.
145
147
def add_contextual_entity ( entity )
146
148
entity = claim ( entity )
147
- contextual_entities . delete ( entity ) # Remove (then re-add) the entity if it exists
148
- contextual_entities . push ( entity )
149
+ contextual_entities . delete? ( entity ) # Remove (then re-add) the entity if it exists
150
+ contextual_entities . add ( entity )
149
151
entity
150
152
end
151
153
@@ -156,8 +158,8 @@ def add_contextual_entity(entity)
156
158
# @return [Entity] the entity itself, or a clone of the entity "owned" by this crate.
157
159
def add_data_entity ( entity )
158
160
entity = claim ( entity )
159
- data_entities . delete ( entity ) # Remove (then re-add) the entity if it exists
160
- data_entities . push ( entity )
161
+ data_entities . delete? ( entity ) # Remove (then re-add) the entity if it exists
162
+ data_entities . add ( entity )
161
163
entity
162
164
end
163
165
@@ -191,7 +193,7 @@ def preview=(preview)
191
193
#
192
194
# @return [Array<Entity>]
193
195
def entities
194
- default_entities | data_entities | contextual_entities
196
+ default_entities | data_entities . to_a | contextual_entities . to_a
195
197
end
196
198
197
199
##
@@ -249,7 +251,7 @@ def payload
249
251
# file data entities. This ensures in the case of a conflict, the more "specific" data entities take priority.
250
252
entries = own_payload
251
253
non_self_entities = default_entities . reject { |e | e == self }
252
- sorted_entities = ( non_self_entities | data_entities ) . sort_by { |e | e . is_a? ( ROCrate ::Directory ) ? 0 : 1 }
254
+ sorted_entities = ( non_self_entities | data_entities . to_a ) . sort_by { |e | e . is_a? ( ROCrate ::Directory ) ? 0 : 1 }
253
255
254
256
sorted_entities . each do |entity |
255
257
entity . payload . each do |path , entry |
@@ -277,7 +279,7 @@ def delete(entity, remove_orphaned: true)
277
279
entity = dereference ( entity ) if entity . is_a? ( String )
278
280
return unless entity
279
281
280
- deleted = data_entities . delete ( entity ) || contextual_entities . delete ( entity )
282
+ deleted = data_entities . delete? ( entity ) || contextual_entities . delete? ( entity )
281
283
282
284
if deleted && remove_orphaned
283
285
crate_entities = crate . linked_entities ( deep : true )
0 commit comments