Sha256: 442f8ad0d74935d7f582f00fbbaaa6b198bf8fff89cad489a7179f876836be40

Contents?: true

Size: 876 Bytes

Versions: 19

Compression:

Stored size: 876 Bytes

Contents

module Friendly
  class ScopeProxy
    attr_reader :klass, :scope_klass, :scopes

    def initialize(klass, scope_klass = Scope)
      @klass       = klass
      @scope_klass = scope_klass
      @scopes      = {}
    end

    def add_named(name, parameters)
      scopes[name] = parameters
      add_scope_method_to_klass(name)
    end

    def get(name)
      scopes[name]
    end

    def get_instance(name)
      scope_klass.new(klass, get(name))
    end

    def ad_hoc(parameters)
      scope_klass.new(klass, parameters)
    end

    def has_named_scope?(name)
      scopes.has_key?(name)
    end

    protected
      def add_scope_method_to_klass(scope_name)
        klass.class_eval do
          eval <<-__END__
            def self.#{scope_name}
              scope_proxy.get_instance(:#{scope_name})
            end
          __END__
        end
      end
  end
end

Version data entries

19 entries across 19 versions & 7 rubygems

Version Path
ihoka-friendly-0.8.0.pre lib/friendly/scope_proxy.rb
ihoka-friendly-0.7.1.2 lib/friendly/scope_proxy.rb
ihoka-friendly-0.7.1.1 lib/friendly/scope_proxy.rb
ihoka-friendly-0.7.1 lib/friendly/scope_proxy.rb
ihoka-friendly-0.7.0 lib/friendly/scope_proxy.rb
ihoka-friendly-0.6.3 lib/friendly/scope_proxy.rb
ihoka-friendly-0.6.2 lib/friendly/scope_proxy.rb
friendly-0.6.0 lib/friendly/scope_proxy.rb
honkster-friendly-0.5.3 lib/friendly/scope_proxy.rb
honkster-friendly-0.5.2 lib/friendly/scope_proxy.rb
honkster-friendly-0.5.1 lib/friendly/scope_proxy.rb
wayne-friendly-0.5.1 lib/friendly/scope_proxy.rb
wego-friendly-0.5.1 lib/friendly/scope_proxy.rb
arunthampi-friendly-0.5.1 lib/friendly/scope_proxy.rb
friendly_postgres-0.5.1 lib/friendly/scope_proxy.rb
friendly-0.5.1 lib/friendly/scope_proxy.rb
friendly-0.5.0 lib/friendly/scope_proxy.rb
friendly_postgres-0.4.5 lib/friendly/scope_proxy.rb
friendly-0.4.5 lib/friendly/scope_proxy.rb