Module: Entity::Map

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

Class Method Summary collapse

Class Method Details

.delete_map(entity_type, column, id, user) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rbbt/rest/entity/map.rb', line 84

def self.delete_map(entity_type, column, id, user)
  path = map_file(entity_type, column, id, user)

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

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

.load_map(entity_type, column, id, user = nil) ⇒ Object



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

def self.load_map(entity_type, column, id, user = nil)
  path = map_file(entity_type, column, id, user)
  path = map_file(entity_type, column, id, :public) unless path != nil and File.exist? path
  path = map_file(entity_type, column, id) unless path != nil and File.exist? path

  begin
    RbbtRESTHelpers.load_tsv(path).first
  rescue
    Log.error{"Error loading map #{ path }: #{$!.message}"}
    nil
  end
end

.map_file(entity_type, column, 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/map.rb', line 14

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

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

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

  path = if user.nil?
           Dir.glob(File.join(Entity.entity_map_cache, entity_type.to_s, column, Regexp.quote(id))).first || File.join(Entity.entity_map_cache, entity_type.to_s, column, id)
         else
           Dir.glob(File.join(Entity.entity_map_cache, user.to_s, entity_type.to_s, column, Regexp.quote(id))).first || File.join(Entity.entity_map_cache, user.to_s, entity_type.to_s, column, id)
         end

  path
end

.map_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/map.rb', line 31

def self.map_files(user = nil)
  path = user.nil? ?
    File.join(Entity.entity_map_cache, '*', '*') :
    File.join(Entity.entity_map_cache, user, '*', '*') 

  maps = {}
  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_map_cache)}" unless 
      Misc.path_relative_to(File.expand_path(Entity.entity_map_cache), file)

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

    maps[entity_type] ||= []
    maps[entity_type][column] ||= []
    maps[entity_type][column] << map
  end

  maps
end

.save_map(entity_type, column, id, map, user = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rbbt/rest/entity/map.rb', line 71

def self.save_map(entity_type, column, id, map, user = nil)
  path = map_file(entity_type, column, id, user)

  Misc.lock path do
    begin
      RbbtRESTHelpers.save_tsv(map, path)
    rescue
      FileUtils.rm path if path and File.exist? path
      raise $!
    end
  end
end