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) ⇒ StorageRule

Returns a new instance of StorageRule.



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

def initialize(role, storage_key, after: nil, condition: nil, scope: nil)
  @role = role.to_sym
  @storage_key = storage_key.to_sym
  @after = after
  @condition = condition
  @scope = scope
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

#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



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

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

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

Returns:

  • (Boolean)


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

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

#scoped?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/archive_storage/storage_rule.rb', line 20

def scoped?
  !scope.nil?
end