lib/snfoil/context/structure.rb in snfoil-context-0.0.4 vs lib/snfoil/context/structure.rb in snfoil-context-1.0.0

- old
+ new

@@ -48,42 +48,44 @@ subclass.instance_variable_set(i, instance_variable_get(i).dup) end end end - attr_reader :entity + included do + attr_reader :entity - def initialize(entity = nil) - @entity = entity - end + def initialize(**keywords) + @entity = keywords[:entity] + end - def authorize(name, **options) - configured_call = self.class.snfoil_authorizations&.fetch(name.to_sym, nil) - configured_call ||= self.class.snfoil_authorizations&.fetch(nil, nil) + def authorize(name, **options) + configured_call = self.class.snfoil_authorizations&.fetch(name.to_sym, nil) + configured_call ||= self.class.snfoil_authorizations&.fetch(nil, nil) - if configured_call - run_hook(configured_call, **options) - else - SnFoil.logger.info "No configuration for #{name} in #{self.class.name}. Authorize not called" if SnFoil.respond_to?(:logger) - true + if configured_call + run_hook(configured_call, **options) + else + SnFoil.logger.info "No configuration for #{name} in #{self.class.name}. Authorize not called" if SnFoil.respond_to?(:logger) + true + end end - end - private + private - def run_hook(hook, **options) - return options unless hook && hook_valid?(hook, **options) + def run_hook(hook, **options) + return options unless hook && hook_valid?(hook, **options) - return send(hook[:method], **options) if hook[:method] + return send(hook[:method], **options) if hook[:method] - instance_exec(**options, &hook[:block]) - end + instance_exec(**options, &hook[:block]) + end - def hook_valid?(hook, **options) - return false if !hook[:if].nil? && hook[:if].call(**options) == false - return false if !hook[:unless].nil? && hook[:unless].call(**options) == true + def hook_valid?(hook, **options) + return false if !hook[:if].nil? && hook[:if].call(**options) == false + return false if !hook[:unless].nil? && hook[:unless].call(**options) == true - true + true + end end end end end