Class: ArchiveStorage::Policy

Inherits:
Object
  • Object
show all
Defined in:
lib/archive_storage/policy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePolicy

Returns a new instance of Policy.



16
17
18
19
20
21
22
23
24
# File 'lib/archive_storage/policy.rb', line 16

def initialize
  @rules = []
  @read_fallbacks = []
  @delete_source_delay = nil
  @delete_requires_verification = true
  @include_versions = false
  @selected_versions = nil
  @timestamp_attribute = :created_at
end

Instance Attribute Details

#delete_requires_verificationObject

Returns the value of attribute delete_requires_verification.



7
8
9
# File 'lib/archive_storage/policy.rb', line 7

def delete_requires_verification
  @delete_requires_verification
end

#delete_source_delayObject

Returns the value of attribute delete_source_delay.



7
8
9
# File 'lib/archive_storage/policy.rb', line 7

def delete_source_delay
  @delete_source_delay
end

#include_versionsObject

Returns the value of attribute include_versions.



7
8
9
# File 'lib/archive_storage/policy.rb', line 7

def include_versions
  @include_versions
end

#primary_storageObject

Returns the value of attribute primary_storage.



7
8
9
# File 'lib/archive_storage/policy.rb', line 7

def primary_storage
  @primary_storage
end

#read_fallbacksObject

Returns the value of attribute read_fallbacks.



7
8
9
# File 'lib/archive_storage/policy.rb', line 7

def read_fallbacks
  @read_fallbacks
end

#rulesObject

Returns the value of attribute rules.



7
8
9
# File 'lib/archive_storage/policy.rb', line 7

def rules
  @rules
end

#selected_versionsObject

Returns the value of attribute selected_versions.



7
8
9
# File 'lib/archive_storage/policy.rb', line 7

def selected_versions
  @selected_versions
end

#timestamp_attributeObject

Returns the value of attribute timestamp_attribute.



7
8
9
# File 'lib/archive_storage/policy.rb', line 7

def timestamp_attribute
  @timestamp_attribute
end

Instance Method Details

#apply_rule_scopes(scope) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/archive_storage/policy.rb', line 65

def apply_rule_scopes(scope)
  scoped_rules = rules.select(&:scoped?)
  return scope if scoped_rules.empty?

  relations = scoped_rules.map { |rule| rule.apply_scope(scope) }
  relations.reduce do |combined, relation|
    combined.respond_to?(:or) ? combined.or(relation) : combined
  end
end

#primary_storage_keyObject



26
27
28
# File 'lib/archive_storage/policy.rb', line 26

def primary_storage_key
  primary_storage&.storage_key
end

#requires_byte_size?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/archive_storage/policy.rb', line 51

def requires_byte_size?
  rules.any?(&:max_byte_size?)
end

#requires_byte_size_for?(record, now: Time.now) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
# File 'lib/archive_storage/policy.rb', line 55

def requires_byte_size_for?(record, now: Time.now)
  rules.any? do |rule|
    rule.requires_byte_size_for?(
      record,
      now: now,
      timestamp_attribute: timestamp_attribute
    )
  end
end

#rule_for_storage(storage_key) ⇒ Object



47
48
49
# File 'lib/archive_storage/policy.rb', line 47

def rule_for_storage(storage_key)
  rules.reverse.find { |rule| rule.storage_key == storage_key.to_sym }
end

#target_rule_for(record, now: Time.now, byte_size: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/archive_storage/policy.rb', line 34

def target_rule_for(record, now: Time.now, byte_size: nil)
  eligible_rules = rules.select do |rule|
    rule.eligible?(
      record,
      now: now,
      timestamp_attribute: timestamp_attribute,
      byte_size: byte_size
    )
  end

  eligible_rules.last
end

#target_storage_for(record, now: Time.now, byte_size: nil) ⇒ Object



30
31
32
# File 'lib/archive_storage/policy.rb', line 30

def target_storage_for(record, now: Time.now, byte_size: nil)
  target_rule_for(record, now: now, byte_size: byte_size)&.storage_key
end