Sha256: f4b9add16f5abdc2380fa7dab0a35a3ca28abb52f71e0098650df58e929c6ef8

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module ActiveEnumerable
  module Scopes
    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)
      if create_scope_method(meth)
        true
      else
        super
      end
    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

    def scope(&block)
      result = instance_exec(&block)
      if result.is_a? Array
        __new_relation__(result)
      else
        result
      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

1 entries across 1 versions & 1 rubygems

Version Path
active_enumerable-0.1.1 lib/active_enumerable/scopes.rb