active support core extension (2)

21
Active Support Core Extensions (2) ROR lab. DD-1 - The 2nd round - April 13, 2013 Hyoseong Choi

Upload: ror-lab

Post on 19-Jan-2015

154 views

Category:

Education


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Active Support Core Extension (2)

Active SupportCore Extensions (2)

ROR lab. DD-1- The 2nd round -

April 13, 2013

Hyoseong Choi

Page 2: Active Support Core Extension (2)

Ext. to Module• alias_method_chain

active_support/core_ext/module/aliasing.rb

• “alias chaining”? ➧ wrapping

http://erniemiller.org/2011/02/03/when-to-use-alias_method_chain/

ActionController::TestCase.class_eval do  def process_with_stringified_params(...)    params = Hash[*params.map {|k, v| [k, v.to_s]}.flatten]    process_without_stringified_params(action, params, session, flash, http_method)  end  alias_method_chain :process, :stringified_paramsend

Page 7: Active Support Core Extension (2)

Ext. to Module• mattr_accessor

active_support/core_ext/module/attr_accessors.rb

module ActiveSupport  module Dependencies    mattr_accessor :warnings_on_first_load    mattr_accessor :history    mattr_accessor :loaded    mattr_accessor :mechanism    mattr_accessor :load_paths    mattr_accessor :load_once_paths    mattr_accessor :autoloaded_constants    mattr_accessor :explicitly_unloadable_constants    mattr_accessor :logger    mattr_accessor :log_activity    mattr_accessor :constant_watch_stack    mattr_accessor :constant_watch_stack_mutex  endend

• cattr_accessor

Page 17: Active Support Core Extension (2)

Ext. to Module• reachable?

module Mend M.reachable? # => true

orphan = Object.send(:remove_const, :M) # The module object is orphan now but it still has a name.orphan.name # => "M" # You cannot reach it via the constant M because it does not even exist.orphan.reachable? # => false # Let's define a module called "M" again.module Mend # The constant M exists now again, and it stores a module# object called "M", but it is a new instance.orphan.reachable? # => false

active_support/core_ext/module/reachable.rb

The constant M exists now, and it stores a module object called "M"

Page 19: Active Support Core Extension (2)

Ext. to Module• delegate

class User < ActiveRecord::Base  has_one :profileend

class User < ActiveRecord::Base  has_one :profile   def name    profile.name  endend

class User < ActiveRecord::Base  has_one :profile   delegate :name, :to => :profileend

delegate :name, :age, :address, :twitter, :to => :profiledelegate :name, :to => :profile, :allow_nil => truedelegate :street, :to => :address, :prefix => truedelegate :size, :to => :attachment, :prefix => :avatar

active_support/core_ext/module/delegation.rb

Page 20: Active Support Core Extension (2)

Ext. to Module• redefine_method

redefine_method("#{reflection.name}=") do |new_value|  association = association_instance_get(reflection.name)   if association.nil? || association.target != new_value    association = association_proxy_class.new(self, reflection)  end   association.replace(new_value)  association_instance_set(reflection.name, new_value.nil? ? nil : association)end

active_support/core_ext/module/remove_method.rb

Page 21: Active Support Core Extension (2)

ROR Lab.

감사합니다.����������� ������������������