Sha256: b82948028ea8fdeb8341f09b080b41355842ac3b4c0e9466d92c8272c2a5c402

Contents?: true

Size: 1.3 KB

Versions: 9

Compression:

Stored size: 1.3 KB

Contents

module Ohm
  module Scope
    def self.included(model)
      unless model.const_defined?(:DefinedScopes)
        model.const_set(:DefinedScopes, Module.new)
      end

      model.extend ClassMethods
    end

    module ClassMethods
      def scope(scope = nil, &block)
        self::DefinedScopes.module_eval(&block) if block_given?
        self::DefinedScopes.send(:include, scope) if scope
      end
    end
  end

  # In Ohm v1.2, the Set and MultiSet initialize methods
  # are defined on themselves. Hence the trick of doing a
  # module OverloadedSet with an initialize method doesn't
  # work anymore.
  #
  # The simplest way to solve that as of now is to duplicate
  # and extend the #initialize method for each of these.
  #
  # Granted it's not the _ideal_ way, the drawbacks are
  # outweighed by the simplicity and performance of this
  # approach versus other monkey-patching techniques.
  class Set
    def initialize(key, namespace, model)
      @key = key
      @namespace = namespace
      @model = model

      extend model::DefinedScopes if defined?(model::DefinedScopes)
    end
  end

  class MultiSet
    def initialize(namespace, model, command)
      @namespace = namespace
      @model = model
      @command = command

      extend model::DefinedScopes if defined?(model::DefinedScopes)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ohm-contrib-2.0.1 lib/ohm/scope.rb
ohm-contrib-2.0.0 lib/ohm/scope.rb
ohm-contrib-2.0.0.rc2 lib/ohm/scope.rb
ohm-contrib-2.0.0.rc1 lib/ohm/scope.rb
ohm-contrib-2.0.0.alpha5 lib/ohm/scope.rb
ohm-contrib-2.0.0.alpha4 lib/ohm/scope.rb
ohm-contrib-2.0.0.alpha3 lib/ohm/scope.rb
ohm-contrib-2.0.0.alpha2 lib/ohm/scope.rb
ohm-contrib-1.2 lib/ohm/scope.rb