Class: Shark::Permissions::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/shark/permissions/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Rule

Returns a new instance of Rule.



11
12
13
14
15
16
17
18
19
20
# File 'lib/shark/permissions/rule.rb', line 11

def initialize(args)
  symbol_args = args.symbolize_keys

  @resource = symbol_args.fetch(:resource)
  @privileges = symbol_args[:privileges] || {}
  normalize_privileges(@privileges)
  @effect = symbol_args[:effect] || 'ALLOW'
  @title = symbol_args[:title]
  @changes = Changes.new
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



7
8
9
# File 'lib/shark/permissions/rule.rb', line 7

def changes
  @changes
end

#effectObject

Returns the value of attribute effect.



6
7
8
# File 'lib/shark/permissions/rule.rb', line 6

def effect
  @effect
end

#privilegesObject

Returns the value of attribute privileges.



6
7
8
# File 'lib/shark/permissions/rule.rb', line 6

def privileges
  @privileges
end

#resourceObject

Returns the value of attribute resource.



6
7
8
# File 'lib/shark/permissions/rule.rb', line 6

def resource
  @resource
end

#titleObject

Returns the value of attribute title.



6
7
8
# File 'lib/shark/permissions/rule.rb', line 6

def title
  @title
end

Instance Method Details

#==(other) ⇒ Object

Returns Boolean.

Returns:

  • Boolean



64
65
66
# File 'lib/shark/permissions/rule.rb', line 64

def ==(other)
  resource == other.resource && effect == other.effect && privileges == other.privileges
end

#as_json(*_args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/shark/permissions/rule.rb', line 68

def as_json(*_args)
  json = {
    'resource' => resource,
    'privileges' => privileges,
    'effect' => effect,
    'parent' => parent
  }
  json['title'] = title if title.present?

  json
end

#changed?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/shark/permissions/rule.rb', line 42

def changed?
  changes.present?
end

#cloneObject



46
47
48
# File 'lib/shark/permissions/rule.rb', line 46

def clone
  self.class.new(as_json)
end

#empty?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/shark/permissions/rule.rb', line 50

def empty?
  privileges.blank?
end

#privileges_as_arrayObject



58
59
60
# File 'lib/shark/permissions/rule.rb', line 58

def privileges_as_array
  privileges.select { |_, v| v == true }.keys
end

#resource_modelObject



54
55
56
# File 'lib/shark/permissions/rule.rb', line 54

def resource_model
  Resource.new(resource)
end

#update(other) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/shark/permissions/rule.rb', line 22

def update(other)
  if resource != other.resource
    raise ArgumentError, "Trying to update different resource: got #{other.resource}, " \
      "but expected #{resource}"
  end

  other.privileges.each do |k, v|
    next if privileges[k] == v

    old = privileges[k]
    privileges[k] = v

    next if old == 'inherited'

    changes.add_privilege(k, old || false, v)
  end

  self
end