Class: Hubbado::Policy::Base

Inherits:
Object
  • Object
show all
Includes:
PolicyDSL
Defined in:
lib/hubbado/policy/base.rb

Defined Under Namespace

Modules: PolicyDSL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PolicyDSL

included

Constructor Details

#initialize(user, record) ⇒ Base

Returns a new instance of Base.



65
66
67
68
69
70
# File 'lib/hubbado/policy/base.rb', line 65

def initialize(user, record)
  raise "User not provided" unless user

  @user = user
  @record = record
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



40
41
42
# File 'lib/hubbado/policy/base.rb', line 40

def record
  @record
end

#userObject (readonly)

Returns the value of attribute user.



40
41
42
# File 'lib/hubbado/policy/base.rb', line 40

def user
  @user
end

Class Method Details

.build(user, record) ⇒ Object



42
43
44
45
46
# File 'lib/hubbado/policy/base.rb', line 42

def self.build(user, record)
  instance = new(user, record)
  instance.configure
  instance
end

.denied(reason = nil, data: nil, i18n_scope: nil) ⇒ Object



51
52
53
54
55
# File 'lib/hubbado/policy/base.rb', line 51

def self.denied(reason = nil, data: nil, i18n_scope: nil)
  i18n_scope ||= self.i18n_scope
  reason ||= :denied
  Result.new(false, reason, i18n_scope: i18n_scope, data: data)
end

.i18n_scopeObject



61
62
63
# File 'lib/hubbado/policy/base.rb', line 61

def self.i18n_scope
  @i18n_scope ||= Casing::Underscore::String.(name).gsub('/', '.')
end

.permittedObject



57
58
59
# File 'lib/hubbado/policy/base.rb', line 57

def self.permitted
  Result.new(true, :permitted)
end

Instance Method Details

#==(other) ⇒ Object



72
73
74
# File 'lib/hubbado/policy/base.rb', line 72

def ==(other)
  self.class == other.class && user == other.user && record == other.record
end

#denied(reason = nil, data: nil, i18n_scope: nil) ⇒ Object



76
77
78
# File 'lib/hubbado/policy/base.rb', line 76

def denied(reason = nil, data: nil, i18n_scope: nil)
  self.class.denied(reason, data: data, i18n_scope: i18n_scope)
end

#permittedObject



80
81
82
# File 'lib/hubbado/policy/base.rb', line 80

def permitted
  self.class.permitted
end