Class: PickyGuard::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/picky_guard/validator.rb

Class Method Summary collapse

Class Method Details

.all_valid_actions?(actions) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
# File 'lib/picky_guard/validator.rb', line 24

def self.all_valid_actions?(actions)
  return false unless actions.is_a? Array
  actions.all? do |action|
    valid_action?(action)
  end
end

.all_valid_policy_classes?(policies) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/picky_guard/validator.rb', line 35

def self.all_valid_policy_classes?(policies)
  return false unless policies.is_a? Array
  policies.all? do |policy|
    valid_policy?(policy)
  end
end

.child_and_parent?(child_class, parent_class) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/picky_guard/validator.rb', line 46

def self.child_and_parent?(child_class, parent_class)
  child_class < parent_class
end

.valid_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/picky_guard/validator.rb', line 20

def self.valid_action?(action)
  action.is_a?(Symbol) || action.is_a?(String)
end

.valid_conditions?(conditions) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/picky_guard/validator.rb', line 42

def self.valid_conditions?(conditions)
  conditions.instance_of?(Hash) || conditions.instance_of?(Proc)
end

.valid_effect?(effect) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/picky_guard/validator.rb', line 50

def self.valid_effect?(effect)
  PickyGuard::Statement::EFFECTS.include? effect
end

.valid_policy?(policy) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/picky_guard/validator.rb', line 31

def self.valid_policy?(policy)
  child_and_parent?(policy, PickyGuard::Policy)
end

.valid_resource_class?(resource) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/picky_guard/validator.rb', line 8

def self.valid_resource_class?(resource)
  child_and_parent?(resource, ActiveRecord::Base)
end

.valid_role?(role) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/picky_guard/validator.rb', line 12

def self.valid_role?(role)
  role.is_a?(Symbol) || role.is_a?(String)
end

.valid_statement?(statement) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/picky_guard/validator.rb', line 16

def self.valid_statement?(statement)
  statement.is_a? PickyGuard::Statement
end

.validate_all_actions!(actions) ⇒ Object

Raises:

  • (ArgumentError)


64
65
66
67
# File 'lib/picky_guard/validator.rb', line 64

def self.validate_all_actions!(actions)
  raise ArgumentError, 'Invalid actions' unless Validator.all_valid_actions?(actions)
  actions
end

.validate_all_policy_classes!(policies) ⇒ Object

Raises:

  • (ArgumentError)


69
70
71
72
# File 'lib/picky_guard/validator.rb', line 69

def self.validate_all_policy_classes!(policies)
  raise ArgumentError, 'Invalid policies' unless Validator.all_valid_policy_classes?(policies)
  policies
end

.validate_conditions!(conditions) ⇒ Object

Raises:

  • (ArgumentError)


79
80
81
82
# File 'lib/picky_guard/validator.rb', line 79

def self.validate_conditions!(conditions)
  raise ArgumentError, 'Invalid conditions' unless Validator.valid_conditions?(conditions)
  conditions
end

.validate_effect!(effect) ⇒ Object

Raises:

  • (ArgumentError)


84
85
86
87
# File 'lib/picky_guard/validator.rb', line 84

def self.validate_effect!(effect)
  raise ArgumentError, 'Invalid effect' unless Validator.valid_effect?(effect)
  effect
end

.validate_resource_class!(resource) ⇒ Object

Raises:

  • (ArgumentError)


59
60
61
62
# File 'lib/picky_guard/validator.rb', line 59

def self.validate_resource_class!(resource)
  raise ArgumentError, 'Invalid Resource' unless Validator.valid_resource_class?(resource)
  resource
end

.validate_role!(role) ⇒ Object

Raises:

  • (ArgumentError)


74
75
76
77
# File 'lib/picky_guard/validator.rb', line 74

def self.validate_role!(role)
  raise ArgumentError, 'Invalid roles' unless Validator.valid_role?(role)
  role
end

.validate_statement!(statement) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
# File 'lib/picky_guard/validator.rb', line 54

def self.validate_statement!(statement)
  raise ArgumentError, 'Invalid Statement' unless Validator.valid_statement?(statement)
  statement
end