Sha256: 5161911136a0e928525cef051c4bc0e409dd5fa2ed2bc69106d8f9766d129487

Contents?: true

Size: 1.53 KB

Versions: 269

Compression:

Stored size: 1.53 KB

Contents

require 'active_support/option_merger'

class Object
  # An elegant way to factor duplication out of options passed to a series of
  # method calls. Each method called in the block, with the block variable as
  # the receiver, will have its options merged with the default +options+ hash
  # provided. Each method called on the block variable must take an options
  # hash as its final argument.
  #
  # Without <tt>with_options></tt>, this code contains duplication:
  #
  #   class Account < ActiveRecord::Base
  #     has_many :customers, :dependent => :destroy
  #     has_many :products,  :dependent => :destroy
  #     has_many :invoices,  :dependent => :destroy
  #     has_many :expenses,  :dependent => :destroy
  #   end
  #
  # Using <tt>with_options</tt>, we can remove the duplication:
  #
  #   class Account < ActiveRecord::Base
  #     with_options :dependent => :destroy do |assoc|
  #       assoc.has_many :customers
  #       assoc.has_many :products
  #       assoc.has_many :invoices
  #       assoc.has_many :expenses
  #     end
  #   end
  #
  # It can also be used with an explicit receiver:
  #
  #   I18n.with_options :locale => user.locale, :scope => "newsletter" do |i18n|
  #     subject i18n.t :subject
  #     body    i18n.t :body, :user_name => user.name
  #   end
  #
  # <tt>with_options</tt> can also be nested since the call is forwarded to its receiver.
  # Each nesting level will merge inherited defaults in addition to their own.
  #
  def with_options(options)
    yield ActiveSupport::OptionMerger.new(self, options)
  end
end

Version data entries

269 entries across 225 versions & 25 rubygems

Version Path
font-awesome-rails-3.1.1.2 vendor/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/object/with_options.rb
font-awesome-rails-3.1.1.2 vendor/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/core_ext/object/with_options.rb
font-awesome-rails-3.1.1.1 vendor/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/core_ext/object/with_options.rb
font-awesome-rails-3.1.1.1 vendor/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/object/with_options.rb
font-awesome-rails-3.1.1.1 vendor/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/core_ext/object/with_options.rb
challah-1.0.0.beta3 vendor/bundle/gems/activesupport-3.2.13/lib/active_support/core_ext/object/with_options.rb
sidekiq-statsd-0.1.1 vendor/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/object/with_options.rb
sidekiq-statsd-0.1.0 vendor/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/object/with_options.rb
fc-webicons-0.0.4 vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/object/with_options.rb
challah-1.0.0.beta2 vendor/bundle/gems/activesupport-3.2.13/lib/active_support/core_ext/object/with_options.rb
challah-1.0.0.beta vendor/bundle/gems/activesupport-3.2.13/lib/active_support/core_ext/object/with_options.rb
challah-1.0.0.beta vendor/bundle/gems/activesupport-3.2.11/lib/active_support/core_ext/object/with_options.rb
fc-webicons-0.0.3 vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/object/with_options.rb
fc-webicons-0.0.2 vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/object/with_options.rb
fc-webicons-0.0.1 vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/object/with_options.rb
activesupport-3.2.13 lib/active_support/core_ext/object/with_options.rb
activesupport-3.1.12 lib/active_support/core_ext/object/with_options.rb
activesupport-3.2.13.rc2 lib/active_support/core_ext/object/with_options.rb
activesupport-3.2.13.rc1 lib/active_support/core_ext/object/with_options.rb
active_mailer-0.0.5 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/core_ext/object/with_options.rb