Class: Paradocs::BasePolicy

Inherits:
Object show all
Defined in:
lib/paradocs/base_policy.rb

Direct Known Subclasses

Policies::Format, Policies::Split

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ BasePolicy

Returns a new instance of BasePolicy.



71
72
73
# File 'lib/paradocs/base_policy.rb', line 71

def initialize(*args)
  @init_params = args
end

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



70
71
72
# File 'lib/paradocs/base_policy.rb', line 70

def environment
  @environment
end

Class Method Details

.build(name, meth, &block) ⇒ Object



3
4
5
6
7
8
# File 'lib/paradocs/base_policy.rb', line 3

def self.build(name, meth, &block)
  klass = Class.new(self)
  klass.public_send(meth, &block)
  klass.policy_name = name
  klass
end

.coerce(&coerce_block) ⇒ Object



20
21
22
23
# File 'lib/paradocs/base_policy.rb', line 20

def self.coerce(&coerce_block)
  @coerce_block = coerce_block if block_given?
  @coerce_block
end

.eligible(&block) ⇒ Object



25
26
27
28
# File 'lib/paradocs/base_policy.rb', line 25

def self.eligible(&block)
  @eligible_block = block if block_given?
  @eligible_block
end

.message(&block) ⇒ Object



10
11
12
13
# File 'lib/paradocs/base_policy.rb', line 10

def self.message(&block)
  @message_block = block if block_given?
  @message_block
end

.meta_data(&block) ⇒ Object



30
31
32
33
# File 'lib/paradocs/base_policy.rb', line 30

def self.(&block)
  @meta_data_block = block if block_given?
  @meta_data_block
end

.policy_nameObject



66
67
68
# File 'lib/paradocs/base_policy.rb', line 66

def self.policy_name
  @policy_name || self.name.split("::").last.downcase.to_sym
end

.policy_name=(name) ⇒ Object



62
63
64
# File 'lib/paradocs/base_policy.rb', line 62

def self.policy_name=(name)
  @policy_name = name
end

.validate(&validate_block) ⇒ Object



15
16
17
18
# File 'lib/paradocs/base_policy.rb', line 15

def self.validate(&validate_block)
  @validate_block = validate_block if block_given?
  @validate_block
end

Instance Method Details

#coerce(value, key, context) ⇒ Object



80
81
82
# File 'lib/paradocs/base_policy.rb', line 80

def coerce(value, key, context)
  (self.class.coerce || ->(v, *_) { v }).call(value, key, context)
end

#eligible?(value, key, payload) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/paradocs/base_policy.rb', line 75

def eligible?(value, key, payload)
  args = (init_params + [value, key, payload])
  (self.class.eligible || ->(*) { true }).call(*args)
end

#messageObject



103
104
105
# File 'lib/paradocs/base_policy.rb', line 103

def message
  @message ||= 'is invalid'
end

#meta_dataObject



90
91
92
93
# File 'lib/paradocs/base_policy.rb', line 90

def 
  return self.class..call(*init_params) if self.class.
  meta
end

#policy_nameObject



99
100
101
# File 'lib/paradocs/base_policy.rb', line 99

def policy_name
  (self.class.policy_name || self.to_s.demodulize.underscore).to_sym
end

#valid?(value, key, payload) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/paradocs/base_policy.rb', line 84

def valid?(value, key, payload)
  args = (init_params + [value, key, payload])
  @message = self.class.message.call(*args) if self.class.message
  validate(*args)
end