Class: Nexo::PolicyService

Inherits:
Object
  • Object
show all
Defined in:
app/lib/nexo/policy_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePolicyService

Returns a new instance of PolicyService.



7
8
9
# File 'app/lib/nexo/policy_service.rb', line 7

def initialize
  clear_finders!
end

Instance Attribute Details

#findersObject (readonly)

Returns the value of attribute finders.



23
24
25
# File 'app/lib/nexo/policy_service.rb', line 23

def finders
  @finders
end

Class Method Details

.instanceObject



15
16
17
# File 'app/lib/nexo/policy_service.rb', line 15

def self.instance
  @instance
end

Instance Method Details

#applies?(folder, synchronizable) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/lib/nexo/policy_service.rb', line 25

def applies?(folder, synchronizable)
  policies = policies_for(folder)
  applied_policies = policies.select { |policy| policy.applies?(synchronizable) }
  # :nocov: TODO
  if applied_policies.any?
    aplicable_policy = applied_policies.sort_by { |policy| policy.priority }.last
    logger.debug { "Aplicable policy: #{aplicable_policy.inspect}" }
    logger.debug { "sync_policy: #{aplicable_policy.sync_policy}" }
    aplicable_policy.sync_policy.to_s == "include"
  else
    logger.debug { "No applicable policies" }
    false
  end
  # :nocov:
end

#clear_finders!Object



3
4
5
# File 'app/lib/nexo/policy_service.rb', line 3

def clear_finders!
  @finders = []
end

#policies_for(folder) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'app/lib/nexo/policy_service.rb', line 41

def policies_for(folder)
  logger.debug { "Found #{@finders.length} finders" }

  aux = @finders.map do |finder|
    finder.call(folder)
  end

  aux.flatten.compact.tap do |ret|
    logger.debug { "Found #{ret.length} policies" }
  end
end

#register_folder_rule_finder(&block) ⇒ Object



19
20
21
# File 'app/lib/nexo/policy_service.rb', line 19

def register_folder_rule_finder(&block)
  @finders << block
end