module SnFoil::Context::Structure

Basic structure needed to support a SnFoil context

@author Matthew Howes

@since 0.1.0

Attributes

entity[R]

Public Class Methods

new(entity = nil) click to toggle source
# File lib/snfoil/context/structure.rb, line 55
def initialize(entity = nil)
  @entity = entity
end

Public Instance Methods

authorize(action_name = nil, with: nil, &block) click to toggle source
# File lib/snfoil/context/structure.rb, line 33
def authorize(action_name = nil, with: nil, &block)
  @snfoil_authorizations ||= {}
  action_name = action_name&.to_sym

  if @snfoil_authorizations[action_name]
    raise SnFoil::Context::Error, "#{name} already has authorize defined for #{action_name || ':default'}"
  end

  @snfoil_authorizations[action_name] = { method: with, block: block }
end
inherited(subclass) click to toggle source
Calls superclass method
# File lib/snfoil/context/structure.rb, line 44
def inherited(subclass)
  super

  instance_variables.grep(/@snfoil_.+/).each do |i|
    subclass.instance_variable_set(i, instance_variable_get(i).dup)
  end
end

Private Instance Methods

hook_valid?(hook, **options) click to toggle source
# File lib/snfoil/context/structure.rb, line 81
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
end
run_hook(hook, **options) click to toggle source
# File lib/snfoil/context/structure.rb, line 73
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])
end