Module: Entity::List

Defined in:
lib/rbbt/rest/entity/list.rb

Class Method Summary collapse

Class Method Details

.delete_list(entity_type, id, user) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rbbt/rest/entity/list.rb', line 88

def self.delete_list(entity_type, id, user)
  path = list_file(entity_type, id, user)

  "This list does not belong to #{ user }: #{[entity_type, id] * ": "}" unless File.exists? path

  Misc.lock path do
    begin
      FileUtils.rm path if File.exists? path
    rescue
      raise $!
    end
  end
end

.list_file(entity_type, id, user = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rbbt/rest/entity/list.rb', line 14

def self.list_file(entity_type, id, user = nil)
  id = Entity::REST.clean_element(id)
  id = Misc.sanitize_filename(id)

  entity_type = entity_type.to_s.split(":").first

  raise "Ilegal list id: #{ id }" unless Misc.path_relative_to Entity.entity_list_cache, File.join(Entity.entity_list_cache, id)

  path = if user.nil?
    Entity.entity_list_cache[entity_type.to_s][id]
  else
    Entity.entity_list_cache[entity_type.to_s][user.to_s][id]
  end

  path.find
end

.list_files(user = nil) ⇒ Object



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
# File 'lib/rbbt/rest/entity/list.rb', line 31

def self.list_files(user = nil)

  path = user.nil? ?
    File.join(Entity.entity_list_cache, '*', '*') :
    File.join(Entity.entity_list_cache, user, '*', '*') 

  lists = {}
  Dir.glob(path).each do |file|
    next if File.directory? file

    file = File.expand_path(file)
    raise "Ilegal path: #{ file }. Not relative to #{File.expand_path(Entity.entity_list_cache)}" unless 
      Misc.path_relative_to(File.expand_path(Entity.entity_list_cache), file)

    if user.nil?
      entity_type, list = file.split("/")[-2..-1]
    else
      user, entity_type, list = file.split("/")[-3..-1]
    end

    lists[entity_type] ||= []
    lists[entity_type] << list
  end

  lists
end

.load_list(entity_type, id, user = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rbbt/rest/entity/list.rb', line 58

def self.load_list(entity_type, id, user = nil)
  path = list_file(entity_type, id, user)
  path = list_file(entity_type, id, :public) unless path != nil and File.exists? path
  path = list_file(entity_type, id) unless path != nil and File.exists? path

  raise "List not found: #{ id }" if path.nil? or not File.exists? path

  begin
    list = Annotated.load_tsv TSV.open(path)
    list.extend AnnotatedArray
    list
  rescue
    Log.exception $!
    nil
  end
end

.save_list(entity_type, id, list, user = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rbbt/rest/entity/list.rb', line 75

def self.save_list(entity_type, id, list, user = nil)
  path = list_file(entity_type, id, user)

  Misc.lock path do
    begin
      Open.write(path, Annotated.tsv(list, :all).to_s)
    rescue
      FileUtils.rm path if File.exists? path
      raise $!
    end
  end
end