Sha256: 1df3cbe17a84a08e1f0028044cc658378cc84db820d69b35363b6f4e379350fe

Contents?: true

Size: 976 Bytes

Versions: 11

Compression:

Stored size: 976 Bytes

Contents

require 'active_support/concern'

module ActiveHouse
  module Modeling
    module Scope
      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)
          scope?(method_name) || super
        end

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

        def scope?(name)
          _scopes.key?(name.to_sym)
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
active_house-0.8.0 lib/active_house/modeling/scope.rb
active_house-0.7.0 lib/active_house/modeling/scope.rb
active_house-0.6.6 lib/active_house/modeling/scope.rb
active_house-0.6.5 lib/active_house/modeling/scope.rb
active_house-0.6.4 lib/active_house/modeling/scope.rb
active_house-0.6.3 lib/active_house/modeling/scope.rb
active_house-0.6.2 lib/active_house/modeling/scope.rb
active_house-0.6.1 lib/active_house/modeling/scope.rb
active_house-0.6.0 lib/active_house/modeling/scope.rb
active_house-0.5.1 lib/active_house/modeling/scope.rb
active_house-0.5.0 lib/active_house/modeling/scope.rb