Sha256: 7655a2dd9b2a46d7768a2a677f4a94b86386271158481b9bdad098cd4e72e646

Contents?: true

Size: 850 Bytes

Versions: 2

Compression:

Stored size: 850 Bytes

Contents

module ActiveEnumerable
  module Scopes
    include ScopeMethod
    def method_missing(meth, *args, &block)
      if create_scope_method(meth)
        send(meth, *args, &block)
      else
        super
      end
    end

    def respond_to_missing?(meth, _include_private = false)
      create_scope_method(meth)
    end

    def create_scope_method(meth)
      if (scope = self.class.__scoped_methods__.find { |a| a.first == meth })
        self.define_singleton_method(scope.first) do
          scope(&scope.last)
        end
      end
    end

    private :create_scope_method

    module ClassMethods
      def scope(name, block)
        __scoped_methods__ << [name, block]
      end

      def __scoped_methods__
        @__scoped_methods__ ||= []
      end
    end

    def self.included(base)
      base.extend(ClassMethods)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_enumerable-1.1.0 lib/active_enumerable/scopes.rb
active_enumerable-1.0.0 lib/active_enumerable/scopes.rb