Class: DeclarativePolicy::Condition

Inherits:
Object
  • Object
show all
Defined in:
lib/declarative_policy/condition.rb

Overview

A Condition is the data structure that is created by the ‘condition` declaration on DeclarativePolicy::Base. It is more or less just a struct of the data passed to that declaration. It holds on to the block to be instance_eval’d on a context (instance of Base) later, via #compute.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}, &compute) ⇒ Condition

Returns a new instance of Condition.



12
13
14
15
16
17
18
19
# File 'lib/declarative_policy/condition.rb', line 12

def initialize(name, opts = {}, &compute)
  @name = name
  @compute = compute
  @scope = opts.fetch(:scope, :normal)
  @description = opts.delete(:description)
  @context_key = opts[:context_key]
  @manual_score = opts.fetch(:score, nil)
end

Instance Attribute Details

#context_keyObject (readonly)

Returns the value of attribute context_key.



10
11
12
# File 'lib/declarative_policy/condition.rb', line 10

def context_key
  @context_key
end

#descriptionObject (readonly)

Returns the value of attribute description.



10
11
12
# File 'lib/declarative_policy/condition.rb', line 10

def description
  @description
end

#manual_scoreObject (readonly)

Returns the value of attribute manual_score.



10
11
12
# File 'lib/declarative_policy/condition.rb', line 10

def manual_score
  @manual_score
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/declarative_policy/condition.rb', line 10

def name
  @name
end

#scopeObject (readonly)

Returns the value of attribute scope.



10
11
12
# File 'lib/declarative_policy/condition.rb', line 10

def scope
  @scope
end

Instance Method Details

#compute(context) ⇒ Object



21
22
23
# File 'lib/declarative_policy/condition.rb', line 21

def compute(context)
  !!context.instance_eval(&@compute)
end

#keyObject



25
26
27
# File 'lib/declarative_policy/condition.rb', line 25

def key
  "#{@context_key}/#{@name}"
end