lib/snfoil/context/structure.rb in snfoil-context-0.0.2 vs lib/snfoil/context/structure.rb in snfoil-context-0.0.4
- old
+ new
@@ -26,31 +26,41 @@
# @since 0.1.0
module Structure
extend ActiveSupport::Concern
class_methods do
- attr_reader :i_authorizations
+ attr_accessor :snfoil_authorizations
def authorize(action_name = nil, with: nil, &block)
- @i_authorizations ||= {}
+ @snfoil_authorizations ||= {}
action_name = action_name&.to_sym
- raise SnFoil::Context::Error, "#{name} already has authorize defined for #{action_name || ':default'}" if @i_authorizations[action_name]
+ if @snfoil_authorizations[action_name]
+ raise SnFoil::Context::Error, "#{name} already has authorize defined for #{action_name || ':default'}"
+ end
- @i_authorizations[action_name] = { method: with, block: block }
+ @snfoil_authorizations[action_name] = { method: with, block: block }
end
+
+ def inherited(subclass)
+ super
+
+ instance_variables.grep(/@snfoil_.+/).each do |i|
+ subclass.instance_variable_set(i, instance_variable_get(i).dup)
+ end
+ end
end
attr_reader :entity
def initialize(entity = nil)
@entity = entity
end
def authorize(name, **options)
- configured_call = self.class.i_authorizations&.fetch(name.to_sym, nil)
- configured_call ||= self.class.i_authorizations&.fetch(nil, nil)
+ 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)
@@ -63,15 +73,15 @@
def run_hook(hook, **options)
return options unless hook && hook_valid?(hook, **options)
return send(hook[:method], **options) if hook[:method]
- instance_exec options, &hook[:block]
+ 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
+ return false if !hook[:if].nil? && hook[:if].call(**options) == false
+ return false if !hook[:unless].nil? && hook[:unless].call(**options) == true
true
end
end
end