Sha256: 23bfd427694a05e0cb6fee7dd32705e34747d5768b5e96261d37a7f7f4119022

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

module Nyanko
  module Invoker
    class FunctionFinder
      UnitNotFound     = Class.new(StandardError)
      FunctionNotFound = Class.new(StandardError)

      def self.find(*args)
        new(*args).find or raise FunctionNotFound
      end

      def initialize(context, options)
        @context = context
        @options = options
      end

      def find
        @options[:functions].find do |unit_name, label|
          unit       = find_unit(unit_name)
          identifier = @options[:as] || @context.class
          next unless unit.active?(@context, @options[:active_if_options])
          function = unit.find_function(identifier, label)
          break function if function
        end
      end

      def find_with_dependencies
        find_without_dependencies unless has_inactive_dependent_unit?
      end
      alias_method_chain :find, :dependencies

      def find_unit(name)
        Loader.load(name) or raise UnitNotFound
      end

      def has_inactive_dependent_unit?
        dependent_units.any? {|unit| !unit.active?(@context, @options[:active_if_options]) }
      end

      def dependent_units
        dependent_unit_names.map {|name| find_unit(name) }
      end

      def dependent_unit_names
        Array.wrap(@options[:dependencies])
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nyanko-0.0.1 lib/nyanko/invoker/function_finder.rb