Skip to content

Commit

Permalink
implement enforce_callback to run an attribute callback
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Mar 16, 2023
1 parent c83687e commit e4979ff
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/goo/validators/enforce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ def enforce(inst,attr,value)
errors_by_opt.length > 0 ? errors_by_opt : nil
end

def enforce_callback(inst, attr)
callbacks = Array(inst.class.update_callbacks(attr))
callbacks.each do |proc|
if instance_proc?(inst, proc)
call_proc(inst.method(proc), inst, attr)
elsif proc.is_a?(Proc)
call_proc(proc, inst, attr)
end
end
end

private

def object_type(opt)
Expand Down Expand Up @@ -115,6 +126,10 @@ def add_error(opt, err)
def self.enforce(inst,attr,value)
EnforceInstance.new.enforce(inst,attr,value)
end

def self.enforce_callbacks(inst, attr)
EnforceInstance.new.enforce_callback(inst, attr)
end
end
end
end

0 comments on commit e4979ff

Please sign in to comment.