Sha256: 04041a7492d07dad8d63f9e4fd4cdf71ee1ab82b66f1cd712c41ab5c29dc97e2

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 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(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 to_key
        name.underscore.to_sym
      end

      def to_prefix
        UnitProxy.generate_prefix(name)
      end

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

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

      def functions
        scopes[current_scope]
      end

      def scopes
        @scopes ||= {}
      end

      def shared_methods
        @shared_methods ||= {}
      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.5 lib/nyanko/unit.rb