Sha256: 0aa68f3a6abad878c83cf025f061f17d328c7f91b14681f369d83113cb033ebf

Contents?: true

Size: 870 Bytes

Versions: 10

Compression:

Stored size: 870 Bytes

Contents

require 'active_support/concern'

module ActiveHouse
  module Scoping
    extend ActiveSupport::Concern

    included do
      class_attribute :_default_scope, instance_accessor: false
      class_attribute :_scopes, instance_accessor: false
      self._scopes = {}
    end

    class_methods do
      def default_scope(name)
        self._default_scope = name.to_sym
      end

      def scope(name, block)
        self._scopes = _scopes.merge(name.to_sym => block)
      end

      def respond_to_missing?(method_name, *_args)
        _scopes.key?(method_name.to_sym)
      end

      def method_missing(method_name, *args, &_block)
        method_name = method_name.to_sym
        if _scopes.key?(method_name)
          scope = _scopes.fetch(method_name)
          all.instance_exec(*args, &scope)
        else
          super
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
active_house-0.4.0 lib/active_house/scoping.rb
active_house-0.3.2 lib/active_house/scoping.rb
active_house-0.3.1 lib/active_house/scoping.rb
active_house-0.3.0 lib/active_house/scoping.rb
active_house-0.2.2 lib/active_house/scoping.rb
active_house-0.2.1 lib/active_house/scoping.rb
active_house-0.2.0 lib/active_house/scoping.rb
active_house-0.1.5 lib/active_house/scoping.rb
active_house-0.1.4 lib/active_house/scoping.rb
active_house-0.1.3 lib/active_house/scoping.rb