Class: ArchiveStorage::StorageRule

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role, storage_key, after: nil, condition: nil, scope: nil, max_byte_size: nil) ⇒ StorageRule

Returns a new instance of StorageRule.



7
8
9
10
11
12
13
14
# File 'lib/archive_storage/storage_rule.rb', line 7

def initialize(role, storage_key, after: nil, condition: nil, scope: nil, max_byte_size: nil)
  @role = role.to_sym
  @storage_key = storage_key.to_sym
  @after = after
  @condition = condition
  @scope = scope
  @max_byte_size = normalize_byte_size(max_byte_size)
end

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



5
6
7
# File 'lib/archive_storage/storage_rule.rb', line 5

def after
  @after
end

#conditionObject (readonly)

Returns the value of attribute condition.



5
6
7
# File 'lib/archive_storage/storage_rule.rb', line 5

def condition
  @condition
end

#max_byte_sizeObject (readonly)

Returns the value of attribute max_byte_size.



5
6
7
# File 'lib/archive_storage/storage_rule.rb', line 5

def max_byte_size
  @max_byte_size
end

#roleObject (readonly)

Returns the value of attribute role.



5
6
7
# File 'lib/archive_storage/storage_rule.rb', line 5

def role
  @role
end

#scopeObject (readonly)

Returns the value of attribute scope.



5
6
7
# File 'lib/archive_storage/storage_rule.rb', line 5

def scope
  @scope
end

#storage_keyObject (readonly)

Returns the value of attribute storage_key.



5
6
7
# File 'lib/archive_storage/storage_rule.rb', line 5

def storage_key
  @storage_key
end

Instance Method Details

#apply_scope(relation) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/archive_storage/storage_rule.rb', line 26

def apply_scope(relation)
  case scope
  when Symbol, String
    relation.public_send(scope)
  when Proc
    scope.arity.zero? ? relation.instance_exec(&scope) : scope.call(relation)
  else
    relation.respond_to?(:merge) ? relation.merge(scope) : scope
  end
end

#byte_size_allowed?(byte_size) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
# File 'lib/archive_storage/storage_rule.rb', line 47

def byte_size_allowed?(byte_size)
  return true unless max_byte_size?
  return false if byte_size.nil?

  byte_size <= max_byte_size
end

#eligible?(record, now:, timestamp_attribute:, byte_size: nil) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/archive_storage/storage_rule.rb', line 16

def eligible?(record, now:, timestamp_attribute:, byte_size: nil)
  old_enough?(record, now: now, timestamp_attribute: timestamp_attribute) &&
    condition_matches?(record) &&
    byte_size_allowed?(byte_size)
end

#max_byte_size?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/archive_storage/storage_rule.rb', line 37

def max_byte_size?
  !max_byte_size.nil?
end

#requires_byte_size_for?(record, now:, timestamp_attribute:) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/archive_storage/storage_rule.rb', line 41

def requires_byte_size_for?(record, now:, timestamp_attribute:)
  max_byte_size? &&
    old_enough?(record, now: now, timestamp_attribute: timestamp_attribute) &&
    condition_matches?(record)
end

#scoped?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/archive_storage/storage_rule.rb', line 22

def scoped?
  !scope.nil?
end