Class Object
In: railties/lib/rails_generator/lookup.rb
actionpack/lib/action_controller/routing.rb
activesupport/lib/active_support/dependencies.rb
activesupport/lib/active_support/core_ext/hash/conversions.rb
activesupport/lib/active_support/core_ext/kernel/agnostics.rb
activesupport/lib/active_support/core_ext/object/instance_variables.rb
activesupport/lib/active_support/core_ext/object/misc.rb
activesupport/lib/active_support/core_ext/object/extending.rb
activesupport/lib/active_support/core_ext/blank.rb
activesupport/lib/active_support/vendor/builder/blankslate.rb
activesupport/lib/active_support/json/encoders/object.rb
Parent: Object

Extensions needed for Hash#to_query

Methods

Included Modules

InstanceExecMethods

External Aliases

load -> load_without_new_constant_marking
send -> send!
  Anticipating Ruby 1.9 neutering send
method_added -> blank_slate_method_added

Public Class methods

Lookup missing generators using const_missing. This allows any generator to reference another without having to know its location: RubyGems, ~/.rails/generators, and RAILS_ROOT/generators.

[Source]

# File railties/lib/rails_generator/lookup.rb, line 8
    def lookup_missing_generator(class_id)
      if md = /(.+)Generator$/.match(class_id.to_s)
        name = md.captures.first.demodulize.underscore
        Rails::Generator::Base.lookup(name).klass
      else
        const_missing_before_generators(class_id)
      end
    end

Public Instance methods

A duck-type assistant method. For example, ActiveSupport extends Date to define an acts_like_date? method, and extends Time to define acts_like_time?. As a result, we can do "x.acts_like?(:time)" and "x.acts_like?(:date)" to do duck-type-safe comparisons, since classes that we want to act like Time simply need to define an acts_like_time? method.

[Source]

# File activesupport/lib/active_support/core_ext/object/misc.rb, line 56
  def acts_like?(duck)
    respond_to? "acts_like_#{duck}?"
  end

An object is blank if it’s nil, empty, or a whitespace string. For example, "", " ", nil, [], and {} are blank.

This simplifies

  if !address.nil? && !address.empty?

to

  if !address.blank?

[Source]

# File activesupport/lib/active_support/core_ext/blank.rb, line 9
  def blank?
    if respond_to?(:empty?) && respond_to?(:strip)
      empty? or strip.empty?
    elsif respond_to?(:empty?)
      empty?
    else
      !self
    end
  end

Evaluate the block with the given arguments within the context of this object, so self is set to the method receiver.

From Mauricio’s eigenclass.org/hiki/bounded+space+instance_exec

[Source]

# File activesupport/lib/active_support/core_ext/object/extending.rb, line 41
    def instance_exec(*args, &block)
      begin
        old_critical, Thread.critical = Thread.critical, true
        n = 0
        n += 1 while respond_to?(method_name = "__instance_exec#{n}")
        InstanceExecMethods.module_eval { define_method(method_name, &block) }
      ensure
        Thread.critical = old_critical
      end

      begin
        send(method_name, *args)
      ensure
        InstanceExecMethods.module_eval { remove_method(method_name) } rescue nil
      end
    end

[Source]

# File activesupport/lib/active_support/core_ext/object/instance_variables.rb, line 4
    def instance_variable_defined?(variable)
      instance_variables.include?(variable.to_s)
    end

A Ruby-ized realization of the K combinator, courtesy of Mikael Brockman.

  def foo
    returning values = [] do
      values << 'bar'
      values << 'baz'
    end
  end

  foo # => ['bar', 'baz']

  def foo
    returning [] do |values|
      values << 'bar'
      values << 'baz'
    end
  end

  foo # => ['bar', 'baz']

[Source]

# File activesupport/lib/active_support/core_ext/object/misc.rb, line 27
  def returning(value)
    yield(value)
    value
  end

Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.

  Account.find(1).to_json
  => "{attributes: {username: \"foo\", id: \"1\", password: \"bar\"}}"

[Source]

# File activesupport/lib/active_support/json/encoders/object.rb, line 7
  def to_json
    ActiveSupport::JSON.encode(instance_values)
  end

[Source]

# File actionpack/lib/action_controller/routing.rb, line 7
  def to_param
    to_s
  end

Mark the given constant as unloadable. Unloadable constants are removed each time dependencies are cleared.

Note that marking a constant for unloading need only be done once. Setup or init scripts may list each unloadable constant that may need unloading; each constant will be removed for every subsequent clear, as opposed to for the first clear.

The provided constant descriptor may be a (non-anonymous) module or class, or a qualified constant name as a string or symbol.

Returns true if the constant was not previously marked for unloading, false otherwise.

[Source]

# File activesupport/lib/active_support/dependencies.rb, line 515
  def unloadable(const_desc)
    Dependencies.mark_for_unload const_desc
  end

An elegant way to refactor out common options

  with_options :order => 'created_at', :class_name => 'Comment' do |post|
    post.has_many :comments, :conditions => ['approved = ?', true], :dependent => :delete_all
    post.has_many :unapproved_comments, :conditions => ['approved = ?', false]
    post.has_many :all_comments
  end

Can also be used with an explicit receiver:

  map.with_options :controller => "people" do |people|
    people.connect "/people",     :action => "index"
    people.connect "/people/:id", :action => "show"
  end

[Source]

# File activesupport/lib/active_support/core_ext/object/misc.rb, line 47
  def with_options(options)
    yield ActiveSupport::OptionMerger.new(self, options)
  end

[Validate]