Module: Fortitude::Widget::Staticization

Extended by:
ActiveSupport::Concern
Included in:
Fortitude::Widget
Defined in:
lib/fortitude/widget/staticization.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

METHODS_TO_DISABLE_WHEN_STATIC =
[ :assigns, :shared_variables ]

Instance Method Summary collapse

Instance Method Details

#with_staticness_enforced(static_method_name, &block) ⇒ Object

INTERNAL USE ONLY



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fortitude/widget/staticization.rb', line 27

def with_staticness_enforced(static_method_name, &block)
  methods_to_disable = METHODS_TO_DISABLE_WHEN_STATIC + self.class.needs_as_hash.keys
  metaclass = (class << self; self; end)

  methods_to_disable.each do |method_name|
    metaclass.class_eval do
      alias_method "_static_disabled_#{method_name}", method_name
      define_method(method_name) { raise Fortitude::Errors::DynamicAccessFromStaticMethod.new(self, static_method_name, method_name) }
    end
  end

  begin
    block.call
  ensure
    methods_to_disable.each do |method_name|
      metaclass.class_eval do
        alias_method method_name, "_static_disabled_#{method_name}"
      end
    end
  end
end