Class: ActiveRecord::NamedScope::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/gems/activerecord-2.2.2/lib/active_record/named_scope.rb

Constant Summary collapse

NON_DELEGATE_METHODS =
%w(nil? send object_id class extend find size count sum average maximum minimum paginate first last empty? any? respond_to?).to_set

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/gems/activerecord-2.2.2/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)



165
166
167
168
169
170
171
172
173
174
# File 'lib/gems/activerecord-2.2.2/lib/active_record/named_scope.rb', line 165

def method_missing(method, *args, &block)
  if scopes.include?(method)
    scopes[method].call(self, *args)
  else
    with_scope :find => proxy_options, :create => proxy_options[:conditions].is_a?(Hash) ?  proxy_options[:conditions] : {} do
      method = :new if method == :build
      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/gems/activerecord-2.2.2/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/gems/activerecord-2.2.2/lib/active_record/named_scope.rb', line 103

def proxy_scope
  @proxy_scope
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


151
152
153
154
155
156
157
# File 'lib/gems/activerecord-2.2.2/lib/active_record/named_scope.rb', line 151

def any?
  if block_given?
    proxy_found.any? { |*block_args| yield(*block_args) }
  else
    !empty?
  end
end

#empty?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/gems/activerecord-2.2.2/lib/active_record/named_scope.rb', line 143

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

#first(*args) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/gems/activerecord-2.2.2/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/gems/activerecord-2.2.2/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/gems/activerecord-2.2.2/lib/active_record/named_scope.rb', line 119

def reload
  load_found; self
end

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

Returns:

  • (Boolean)


147
148
149
# File 'lib/gems/activerecord-2.2.2/lib/active_record/named_scope.rb', line 147

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

#sizeObject



139
140
141
# File 'lib/gems/activerecord-2.2.2/lib/active_record/named_scope.rb', line 139

def size
  @found ? @found.length : count
end