module Pakyow::Application::Behavior::Isolating

Helps manage isolated classes for an app.

Public Instance Methods

isolate(class_to_isolate, &block) click to toggle source

Creates a subclass within the app's namespace.

@example

class MyApplication < Pakyow::Application
  isolate Pakyow::Controller do
    def self.special_behavior
      puts "it works"
    end
  end
end

MyApplication::Controller.special_behavior
=> it works

Pakyow::Controller
=> NoMethodError (undefined method `special_behavior' for Pakyow::Controller:Class)
# File lib/pakyow/application/behavior/isolating.rb, line 32
def isolate(class_to_isolate, &block)
  isolated_class_name = Support.inflector.demodulize(class_to_isolate.to_s).to_sym

  unless const_defined?(isolated_class_name, false)
    const_set(isolated_class_name, Class.new(class_to_isolate))
  end

  isolated(isolated_class_name).tap do |defined_subclass|
    defined_subclass.class_eval(&block) if block_given?
  end
end
isolated(class_name, &block) click to toggle source

Returns the isolated class, evaluating the block (if provided).

# File lib/pakyow/application/behavior/isolating.rb, line 52
def isolated(class_name, &block)
  if isolated?(class_name)
    const_get(class_name).tap do |isolated_class|
      isolated_class.class_eval(&block) if block_given?
    end
  else
    nil
  end
end
isolated?(class_name) click to toggle source

Returns true if the class name is isolated.

# File lib/pakyow/application/behavior/isolating.rb, line 46
def isolated?(class_name)
  const_defined?(class_name)
end