Class: ActiveRecord::NamedScope::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/named_scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy_scope, options, &block) ⇒ Scope

Returns a new instance of Scope.



113
114
115
116
117
# File 'lib/active_record/named_scope.rb', line 113

def initialize(proxy_scope, options, &block)
  [options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
  extend Module.new(&block) if block_given?
  @proxy_scope, @proxy_options = proxy_scope, options.except(:extend)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



153
154
155
156
157
158
159
160
161
# File 'lib/active_record/named_scope.rb', line 153

def method_missing(method, *args, &block)
  if scopes.include?(method)
    scopes[method].call(self, *args)
  else
    with_scope :find => proxy_options do
      proxy_scope.send(method, *args, &block)
    end
  end
end

Instance Attribute Details

#proxy_optionsObject (readonly)

Returns the value of attribute proxy_options.



103
104
105
# File 'lib/active_record/named_scope.rb', line 103

def proxy_options
  @proxy_options
end

#proxy_scopeObject (readonly)

Returns the value of attribute proxy_scope.



103
104
105
# File 'lib/active_record/named_scope.rb', line 103

def proxy_scope
  @proxy_scope
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/active_record/named_scope.rb', line 139

def empty?
  @found ? @found.empty? : count.zero?
end

#first(*args) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/active_record/named_scope.rb', line 123

def first(*args)
  if args.first.kind_of?(Integer) || (@found && !args.first.kind_of?(Hash))
    proxy_found.first(*args)
  else
    find(:first, *args)
  end
end

#last(*args) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/active_record/named_scope.rb', line 131

def last(*args)
  if args.first.kind_of?(Integer) || (@found && !args.first.kind_of?(Hash))
    proxy_found.last(*args)
  else
    find(:last, *args)
  end
end

#reloadObject



119
120
121
# File 'lib/active_record/named_scope.rb', line 119

def reload
  load_found; self
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/active_record/named_scope.rb', line 143

def respond_to?(method, include_private = false)
  super || @proxy_scope.respond_to?(method, include_private)
end