Your SlideShare is downloading. ×
Active SupportCore Extensions (2)ROR lab. DD-1- The 2nd round -April 13, 2013Hyoseong Choi
Ext. to Module• alias_method_chainactive_support/core_ext/module/aliasing.rb• “alias chaining”? ➧ wrappinghttp://erniemill...
Ext. to Module• alias_attributeactive_support/core_ext/module/aliasing.rbclass User < ActiveRecord::Base  # let me refer t...
Ext. to Module• attr_accessor_with_defaultactive_support/core_ext/module/attr_accessor_with_default.rbclass Url  attr_acce...
Ext. to Module• attr_internal or attr_internal_accessoractive_support/core_ext/module/attr_internal.rb# libraryclass Third...
Ext. to Module• attr_internal or attr_internal_accessoractive_support/core_ext/module/attr_internal.rbmodule ActionView  c...
Ext. to Module• mattr_accessoractive_support/core_ext/module/attr_accessors.rbmodule ActiveSupport  module Dependencies   ...
Ext. to Module• Parentsactive_support/core_ext/module/introspection.rb•parent•parent_name•parents
Ext. to Module• Parentsactive_support/core_ext/module/introspection.rbmodule X  module Y    module Z    end  endendM = X::...
Ext. to Module• Parentsactive_support/core_ext/module/introspection.rb• parent_namemodule X  module Y    module Z    end  ...
Ext. to Module• Parentsactive_support/core_ext/module/introspection.rb• parentsmodule X  module Y    module Z    end  ende...
Ext. to Module• local_constantsmodule X  X1 = 1  X2 = 2  module Y    Y1 = :y1    X1 = :overrides_X1_above  endend X.local_...
Ext. to Module• const_defined? /_get /_setMath.const_defined? "PI" #=> trueIO.const_defined? :SYNC #=> trueIO.const_defined...
Ext. to Module• const_defined? /_get /_setmodule Fooclass BarVAL = 10endclass Baz < Bar; endendObject.const_get Foo::Baz::V...
Ext. to Module• qualified_const_defined? /_get /_setmodule M  X = 1end module N  class C    include M  endendactive_support/...
Ext. to Module• synchronizeclass Counter  @@mutex = Mutex.new  attr_reader :value   def initialize    @value = 0  end   de...
Ext. to Module• reachable?module Mend M.reachable? # => trueorphan = Object.send(:remove_const, :M) # The module object is...
Ext. to Module• anonymous?module MendM.name # => "M" N = Module.newN.name # => "N" Module.new.name # => "" in 1.8, nil in ...
Ext. to Module• delegateclass User < ActiveRecord::Base  has_one :profileendclass User < ActiveRecord::Base  has_one :prof...
Ext. to Module• redefine_methodredefine_method("#{reflection.name}=") do |new_value|  association = association_instance_ge...
ROR Lab.감사합니다.
Upcoming SlideShare
Loading in...5
×

Active Support Core Extension (2)

184

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
184
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Active Support Core Extension (2)"

  1. 1. Active SupportCore Extensions (2)ROR lab. DD-1- The 2nd round -April 13, 2013Hyoseong Choi
  2. 2. Ext. to Module• alias_method_chainactive_support/core_ext/module/aliasing.rb• “alias chaining”? ➧ wrappinghttp://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
  3. 3. Ext. to Module• alias_attributeactive_support/core_ext/module/aliasing.rbclass User < ActiveRecord::Base  # let me refer to the email column as "login",  # possibly meaningful for authentication code  alias_attribute :login, :emailend
  4. 4. Ext. to Module• attr_accessor_with_defaultactive_support/core_ext/module/attr_accessor_with_default.rbclass Url  attr_accessor_with_default :port, 80end Url.new.port # => 80class User  attr_accessor :name, :surname  attr_accessor_with_default(:full_name) do    [name, surname].compact.join(" ")  endend u = User.newu.name = Xavieru.surname = Noriau.full_name # => "Xavier Noria"notcached
  5. 5. Ext. to Module• attr_internal or attr_internal_accessoractive_support/core_ext/module/attr_internal.rb# libraryclass ThirdPartyLibrary::Crawler  attr_internal :log_levelend # client codeclass MyCrawler < ThirdPartyLibrary::Crawler  attr_accessor :log_levelend@_log_levelModule.attr_internal_naming_format = “@_%s”sprintf-like format
  6. 6. Ext. to Module• attr_internal or attr_internal_accessoractive_support/core_ext/module/attr_internal.rbmodule ActionView  class Base    attr_internal :captures    attr_internal :request, :layout    attr_internal :controller, :template  endend
  7. 7. Ext. to Module• mattr_accessoractive_support/core_ext/module/attr_accessors.rbmodule 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
  8. 8. Ext. to Module• Parentsactive_support/core_ext/module/introspection.rb•parent•parent_name•parents
  9. 9. Ext. to Module• Parentsactive_support/core_ext/module/introspection.rbmodule X  module Y    module Z    end  endendM = X::Y::Z X::Y::Z.parent # => X::YM.parent       # => X::Y• parent
  10. 10. Ext. to Module• Parentsactive_support/core_ext/module/introspection.rb• parent_namemodule X  module Y    module Z    end  endendM = X::Y::Z X::Y::Z.parent_name # => "X::Y"M.parent_name       # => "X::Y"
  11. 11. Ext. to Module• Parentsactive_support/core_ext/module/introspection.rb• parentsmodule X  module Y    module Z    end  endendM = X::Y::Z X::Y::Z.parents # => [X::Y, X, Object]M.parents       # => [X::Y, X, Object]
  12. 12. Ext. to Module• local_constantsmodule X  X1 = 1  X2 = 2  module Y    Y1 = :y1    X1 = :overrides_X1_above  endend X.local_constants    # => ["X2", "X1", "Y"], assumes Ruby 1.8X::Y.local_constants # => ["X1", "Y1"], assumes Ruby 1.8active_support/core_ext/module/introspection.rb• local_constant_names ⟶ always, stringsas symbols in Ruby 1.9deprecated!!!
  13. 13. Ext. to Module• const_defined? /_get /_setMath.const_defined? "PI" #=> trueIO.const_defined? :SYNC #=> trueIO.const_defined? :SYNC, false #=> falseactive_support/core_ext/module/introspection.rbMath.const_get(:PI) #=> 3.14159265358979Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0)#=> 3.14285714285714Math::HIGH_SCHOOL_PI - Math::PI#=> 0.00126448926734968standard ruby methodsbare constant names
  14. 14. Ext. to Module• const_defined? /_get /_setmodule Fooclass BarVAL = 10endclass Baz < Bar; endendObject.const_get Foo::Baz::VAL # => 10Object.const_get Foo::Baz::VAL, false # => NameErroractive_support/core_ext/module/introspection.rbstandard ruby methodsbare constant namesflag for inherit
  15. 15. Ext. to Module• qualified_const_defined? /_get /_setmodule M  X = 1end module N  class C    include M  endendactive_support/core_ext/module/qualified_const.rbnew extended methodsN.qualified_const_defined?("C::X", false)N.qualified_const_defined?("C::X", true) N.qualified_const_defined?("C::X")       relative to their receiverqualified constant names
  16. 16. Ext. to Module• synchronizeclass Counter  @@mutex = Mutex.new  attr_reader :value   def initialize    @value = 0  end   def incr    @value += 1 # non-atomic  end  synchronize :incr, :with => @@mutexendactive_support/core_ext/module/synchronization.rbDEPRECATION WARNING: synchronizeis deprecated and will beremoved from Rails 4.0.
  17. 17. Ext. to Module• reachable?module Mend M.reachable? # => trueorphan = 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 # Lets 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? # => falseactive_support/core_ext/module/reachable.rbThe constant M exists now,and it stores a module object called "M"
  18. 18. Ext. to Module• anonymous?module MendM.name # => "M" N = Module.newN.name # => "N" Module.new.name # => "" in 1.8, nil in 1.9M.anonymous? # => false Module.new.anonymous? # => truem = Object.send(:remove_const, :M) m.reachable? # => falsem.anonymous? # => falseactive_support/core_ext/module/anonymous.rb
  19. 19. Ext. to Module• delegateclass User < ActiveRecord::Base  has_one :profileendclass User < ActiveRecord::Base  has_one :profile   def name    profile.name  endendclass User < ActiveRecord::Base  has_one :profile   delegate :name, :to => :profileenddelegate :name, :age, :address, :twitter, :to => :profiledelegate :name, :to => :profile, :allow_nil => truedelegate :street, :to => :address, :prefix => truedelegate :size, :to => :attachment, :prefix => :avataractive_support/core_ext/module/delegation.rb
  20. 20. Ext. to Module• redefine_methodredefine_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)endactive_support/core_ext/module/remove_method.rb
  21. 21. ROR Lab.감사합니다.
  22. 22.  

×