Class: Canistor::Storage::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/canistor/storage/bucket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ Bucket

Returns a new instance of Bucket.



18
19
20
21
22
23
24
# File 'lib/canistor/storage/bucket.rb', line 18

def initialize(**attributes)
  @access_keys = Set.new
  clear
  attributes.each do |name, value|
    public_send("#{name}=", value)
  end
end

Instance Attribute Details

#access_keysObject (readonly)

Returns the value of attribute access_keys.



15
16
17
# File 'lib/canistor/storage/bucket.rb', line 15

def access_keys
  @access_keys
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/canistor/storage/bucket.rb', line 13

def name
  @name
end

#objectsObject (readonly)

Returns the value of attribute objects.



16
17
18
# File 'lib/canistor/storage/bucket.rb', line 16

def objects
  @objects
end

#regionObject

Returns the value of attribute region.



12
13
14
# File 'lib/canistor/storage/bucket.rb', line 12

def region
  @region
end

Instance Method Details

#[](name) ⇒ Object



26
27
28
# File 'lib/canistor/storage/bucket.rb', line 26

def [](name)
  @objects[name]
end

#[]=(name, value) ⇒ Object



30
31
32
# File 'lib/canistor/storage/bucket.rb', line 30

def []=(name, value)
  @objects[name] = value
end

#allow_access_to(access_key_ids) ⇒ Object



90
91
92
# File 'lib/canistor/storage/bucket.rb', line 90

def allow_access_to(access_key_ids)
  access_keys.merge(access_key_ids)
end

#clearObject



80
81
82
# File 'lib/canistor/storage/bucket.rb', line 80

def clear
  @objects = {}
end

#delete(context, access_key_id, subject) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/canistor/storage/bucket.rb', line 69

def delete(context, access_key_id, subject)
  if !access_keys.include?(access_key_id)
    Canistor::ErrorHandler.serve_access_denied(context, subject)
  elsif object = objects[subject.key]
    @objects.delete(object.key)
    object.delete(context, subject)
  else
    Canistor::ErrorHandler.serve_no_such_key(context, subject)
  end
end

#dig(*segments) ⇒ Object



34
35
36
# File 'lib/canistor/storage/bucket.rb', line 34

def dig(*segments)
  @objects.dig(*segments)
end

#get(context, access_key_id, subject) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/canistor/storage/bucket.rb', line 48

def get(context, access_key_id, subject)
  if !access_keys.include?(access_key_id)
    Canistor::ErrorHandler.serve_access_denied(context, subject)
  elsif object = objects[subject.key] 
    object.get(context, subject)
  else
    Canistor::ErrorHandler.serve_no_such_key(context, subject)
  end
end

#head(context, access_key_id, subject) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/canistor/storage/bucket.rb', line 38

def head(context, access_key_id, subject)
  if !access_keys.include?(access_key_id)
    Canistor::ErrorHandler.serve_access_denied(context, subject)
  elsif object = objects[subject.key]
    object.head(context, subject)
  else
    Canistor::ErrorHandler.serve_no_such_key(context, subject)
  end
end

#headersObject



94
95
96
# File 'lib/canistor/storage/bucket.rb', line 94

def headers
  {}
end

#put(context, access_key_id, subject) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/canistor/storage/bucket.rb', line 58

def put(context, access_key_id, subject)
  if access_keys.include?(access_key_id)
    Canistor.take_fail(:store) { return }
    object = find_or_build_object(subject, context)
    self[subject.key] = object
    object.put(context, subject)
  else
    Canistor::ErrorHandler.serve_access_denied(context, subject)
  end
end

#to_sObject



84
85
86
87
88
# File 'lib/canistor/storage/bucket.rb', line 84

def to_s
  @objects.values.map do |object|
    ' * ' + object.label
  end.join("\n")
end