Sha256: f6ad4e986bdc4e2a33ad53604c19cc8e0984f6e38993ac99333b1e37d07c03ae

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

require "nyanko/unit/extender"
require "nyanko/unit/scope_finder"

module Nyanko
  module Unit
    extend ActiveSupport::Concern

    included do
      active_if { true }
    end

    module ClassMethods
      attr_accessor :current_scope

      def scope(identifier)
        self.current_scope = ScopeFinder.find(identifier)
        scopes[current_scope] ||= {}
        yield
      ensure
        self.current_scope = nil
      end

      def function(label, &block)
        functions[label] = Function.new(self, label, &block)
      end
      alias_method :callback, :function

      def shared(label, &block)
        shared_methods[label] = block
      end

      def helpers(&block)
        Helper.define(unit_name, &block)
      end

      def models(&block)
        extender.instance_eval(&block)
      end

      def active_if(*conditions, &block)
        @active_if = ActiveIf.new(*conditions, &block)
      end

      def active?(context, options = {})
        @active_if.active?(context, options.merge(:unit => self))
      end

      def any(*labels)
        ActiveIf::Any.new(*labels)
      end

      def unit_name
        @unit_name ||= name.underscore.to_sym
      end

      def to_prefix
        UnitProxy.generate_prefix(unit_name)
      end

      def view_path
        "#{Config.units_directory_path}/#{unit_name}/views"
      end

      def find_function(identifier, label)
        scope     = ScopeFinder.find(identifier)
        target    = scope.ancestors.find {|klass| scopes[klass] }
        functions = scopes[target]
        functions[label] if functions
      end

      def functions
        scopes[current_scope]
      end

      def scopes
        @scopes ||= {}
      end

      def shared_methods
        @shared_methods ||= {}
      end

      def resolver
        @resolver ||= ActionView::OptimizedFileSystemResolver.new(view_path)
      end

      def extender
        @extender ||= Extender.new(to_prefix)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nyanko-0.0.7 lib/nyanko/unit.rb