Class: ROF::Access

Inherits:
Object
  • Object
show all
Defined in:
lib/rof/access.rb

Overview

provide translation between access strings and the ROF access hash e.g. (“public”, owner=dbrower) –> Hash.new(read-groups: “public”, edit: “dbrower”)

Defined Under Namespace

Classes: DecodeError

Class Method Summary collapse

Class Method Details

.decode(access_string, owner = nil) ⇒ Object

convert from a string to a hash



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rof/access.rb', line 9

def self.decode(access_string, owner=nil)
  result = {}
  access_string.split(";").each do |clause|
    t = self.decode_clause(clause, owner)
    t.each do |k,v|
      if v.is_a?(Array)
        result[k] = (result.fetch(k, []) + v).uniq
      else
        result[k] = v
      end
    end
  end

  result
end

.decode_clause(access, owner) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rof/access.rb', line 37

def self.decode_clause(access, owner)
  case access
  when "public"
    {"read-groups" => ["public"], "edit" => [owner]}
  when "restricted"
    {"read-groups" => ["registered"], "edit" => [owner]}
  when "private"
    {"edit" => [owner]}
  when /^embargo=(.+)/
    {"embargo-date" => $1}
  when /^(read|readgroup|edit|editgroup|discover|discovergroup)=(.+)/
    which = $1
    who = $2.split(",")
    xwhich = which.gsub("group", "-groups")
    Hash[xwhich, who]
  else
    raise DecodeError
  end
end

.encode(access_hash) ⇒ Object

convert from a hash to a string simple because we do not try to recover “public”, et al.



27
28
29
30
31
32
33
34
35
# File 'lib/rof/access.rb', line 27

def self.encode(access_hash)
  result = []
  access_hash.each do |k,v|
    xk = k.gsub("-groups", "group").gsub("embargo-date","embargo")
    xv = v.join(',') if v.is_a?(Array)
    result << "#{xk}=#{xv}"
  end
  result.join(";")
end