Module: Bali::Objector::Statics

Defined in:
lib/bali/objector.rb

Overview

to allow class-level objection

Instance Method Summary collapse

Instance Method Details

#bali_translate_subtarget_roles(arg) ⇒ Object

get the proper roles for the subtarget, for any type of subtarget



46
47
48
49
# File 'lib/bali/objector.rb', line 46

def bali_translate_subtarget_roles(arg)
  role_extractor = Bali::RoleExtractor.new(arg)
  role_extractor.get_roles
end

#can!(subtarget_roles, operation, record = self, options = {}) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/bali/objector.rb', line 122

def can!(subtarget_roles, operation, record = self, options = {})
  can?(subtarget_roles, operation, record, options) do |original_subtarget, role, can_value|
    if !can_value
      auth_error = Bali::AuthorizationError.new
      auth_error.auth_level = :can
      auth_error.operation = operation
      auth_error.role = role
      auth_error.target = record
      auth_error.subtarget = original_subtarget

      if role
        auth_error.subtarget = original_subtarget if !(original_subtarget.is_a?(Symbol) || original_subtarget.is_a?(String) || original_subtarget.is_a?(Array))
      end

      raise auth_error
    else
      return can_value
    end # if cannot is false, means cannot
  end # can?
end

#can?(subtarget_roles, operation, record = self, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bali/objector.rb', line 51

def can?(subtarget_roles, operation, record = self, options = {})
  subs = bali_translate_subtarget_roles(subtarget_roles)
  # well, it is largely not used unless decider's is 2 arity
  original_subtarget = options[:original_subtarget].nil? ? subtarget_roles : options[:original_subtarget]

  judgement_value = false
  role = nil
  judger = nil

  subs.each do |subtarget|
    next if judgement_value == true

    judge = Bali::Judger::Judge.build(:can, {
      subtarget: subtarget,
      original_subtarget: original_subtarget,
      operation: operation,
      record: record
    })
    judgement_value = judge.judgement

    role = subtarget
  end

  if block_given? 
    yield original_subtarget, role, judgement_value 
  end

  judgement_value
rescue => e
  if e.is_a?(Bali::AuthorizationError) || e.is_a?(Bali::Error)
    raise e
  else
    raise Bali::ObjectionError, e.message, e.backtrace
  end
end

#cannot!(subtarget_roles, operation, record = self, options = {}) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/bali/objector.rb', line 153

def cannot!(subtarget_roles, operation, record = self, options = {})
  cannot?(subtarget_roles, operation, record, options) do |original_subtarget, role, cant_value|
    if cant_value == false
      auth_error = Bali::AuthorizationError.new
      auth_error.auth_level = :cannot
      auth_error.operation = operation
      auth_error.role = role
      auth_error.target = record
      auth_error.subtarget = original_subtarget

      if role
        auth_error.subtarget = original_subtarget if !(original_subtarget.is_a?(Symbol) || original_subtarget.is_a?(String) || original_subtarget.is_a?(Array))
      end

      raise auth_error
    else
      return cant_value
    end # if cannot is false, means can
  end # cannot?
end

#cannot?(subtarget_roles, operation, record = self, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/bali/objector.rb', line 87

def cannot?(subtarget_roles, operation, record = self, options = {})
  subs = bali_translate_subtarget_roles subtarget_roles
  original_subtarget = options[:original_subtarget].nil? ? subtarget_roles : options[:original_subtarget]

  judgement_value = true
  role = nil
  judger = nil

  subs.each do |subtarget|
    next if judgement_value == false

    judge = Bali::Judger::Judge.build(:cannot, {
      subtarget: subtarget,
      original_subtarget: original_subtarget,
      operation: operation,
      record: record
    })
    judgement_value = judge.judgement

    role = subtarget
  end

  if block_given?
    yield original_subtarget, role, judgement_value
  end

  judgement_value
rescue => e
  if e.is_a?(Bali::AuthorizationError)
    raise e
  else
    raise Bali::ObjectionError, e.message, e.backtrace
  end
end

#cant!(subtarget_roles, operation, record = self, options = {}) ⇒ Object



143
144
145
146
# File 'lib/bali/objector.rb', line 143

def cant!(subtarget_roles, operation, record = self, options = {})
  puts "Deprecation Warning: please use cannot! instead, cant! will be deprecated on major release 3.0"
  cannot!(subtarget_roles, operation, record, options)
end

#cant?(subtarget_roles, operation) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
151
# File 'lib/bali/objector.rb', line 148

def cant?(subtarget_roles, operation)
  puts "Deprecation Warning: please use cannot? instead, cant? will be deprecated on major release 3.0"
  cannot?(subtarget_roles, operation)
end