Active Support Core Extension (3)

747 views

Published on

Published in: Education, Technology, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
747
On SlideShare
0
From Embeds
0
Number of Embeds
156
Actions
Shares
0
Downloads
5
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Active Support Core Extension (3)

  1. 1. Active SupportCore Extensions (3)ROR lab. DD-1- The 3rd round -April 13, 2013Hyoseong Choi
  2. 2. Ext. to Class• class_attributeactive_support/core_ext/class/attribute.rbclass A  class_attribute :xend class B < A; end class C < B; end A.x = :aB.x # => :aC.x # => :aB.x = :bA.x # => :aC.x # => :b C.x = :cA.x # => :aB.x # => :bone or more inheritable class attributes that can be overridden at any level down the hierarchy
  3. 3. Ext. to Class• class_inheritable_accessor, _reader, _writeractive_support/core_ext/class/inheritable_attributes.rbmodule ActionController  class Base    # FIXME: REVISE/SIMPLIFY THIS COMMENT.    # The value of allow_forgery_protection is inherited,    # but its value in a particular class does not affect    # the value in the rest of the controllers hierarchy.    class_inheritable_accessor :allow_forgery_protection  endend: deprecated ➜ class_attribute insteadaccessors for class-level data which is inherited but not shared with children
  4. 4. class C; endC.subclasses # => [] class B < C; endC.subclasses # => [B] class A < B; endC.subclasses # => [B] class D < C; endC.subclasses # => [B, D]Ext. to Class• subclassesactive_support/core_ext/class/subclasses.rbCBAD
  5. 5. Ext. to Class• descendantsactive_support/core_ext/class/subclasses.rbclass C; endC.descendants # => [] class B < C; endC.descendants # => [B] class A < B; endC.descendants # => [B, A] class D < C; endC.descendants # => [B, A, D]CBAD
  6. 6. Ext. to String• Strings “(html) unsafe” by default since Rails3"".html_safe? # => falses = "".html_safes.html_safe? # => true• html_safe no escapings = "<script>...</script>".html_safes.html_safe? # => trues            # => "<script>...</script>"active_support/core_ext/string/output_safety.rb
  7. 7. Ext. to String• html_safeactive_support/core_ext/string/output_safety.rb<%= raw @cms.current_template %><%== @cms.current_template %>def raw(stringish)  stringish.to_s.html_safeend
  8. 8. Ext. to String• squish, squish!active_support/core_ext/string/filters.rb" n  foonr t bar n".squish # => "foo bar"
  9. 9. Ext. to String• truncateactive_support/core_ext/string/filters.rb"Oh dear! Oh dear! I shall be late!".truncate(20)# => "Oh dear! Oh dear!...""Oh dear! Oh dear! I shall belate!".truncate(20, :omission => &hellip;)# => "Oh dear! Oh &hellip;" …"Oh dear! Oh dear! I shall be late!".truncate(18)# => "Oh dear! Oh dea...""Oh dear! Oh dear! I shall belate!".truncate(18, :separator => )# => "Oh dear! Oh..."
  10. 10. Ext. to String• Output safety - inquiryactive_support/core_ext/string/inquiry.rb"production".inquiry.production? # => true"active".inquiry.inactive?       # => falsea StringInquirerobject
  11. 11. Ext. to String• Output safety - Key-based interpolationactive_support/core_ext/string/interpolation.rb"Total is %<total>.02f" % {:total => 43.1} # => Total is 43.10"I say %{foo}" % {:foo => "wadus"}         # => "I say wadus""I say %{woo}" % {:foo => "wadus"}         # => KeyError
  12. 12. Ext. to String• Output safety - start_with? / ends_with?active_support/core_ext/string/starts_ends_with.rb"foo".starts_with?("f") # => true"foo".ends_with?("o")   # => true
  13. 13. Ext. to String• Output safety - strip_heredocactive_support/core_ext/string/strip.rbif options[:usage]  puts <<-USAGE.strip_heredoc    This command does such and such.     Supported options are:      -h         This message      ...  USAGEend
  14. 14. Ext. to String• Access - at(position)active_support/core_ext/string/access.rb"hello".at(0)  # => "h""hello".at(4)  # => "o""hello".at(-1) # => "o""hello".at(10) # => ERROR if < 1.9, nil in 1.9
  15. 15. Ext. to String• Access - from(position)active_support/core_ext/string/access.rb"hello".from(0)  # => "hello""hello".from(2)  # => "llo""hello".from(-2) # => "lo""hello".from(10) # => "" if < 1.9, nil in 1.9
  16. 16. Ext. to String• Access - to(position)active_support/core_ext/string/access.rb"hello".to(0)  # => "h""hello".to(2)  # => "hel""hello".to(-2) # => "hell""hello".to(10) # => "hello"
  17. 17. Ext. to String• Access - first/lastactive_support/core_ext/string/access.rbstr.first(n) or str.to(n-1)str.last(n) or str.from(-n)
  18. 18. Ext. to String• Inflectionsactive_support/core_ext/string/inflections.rb• pluralize• singularize• camerlize• underscore• titleize• dasherize• demodulize• deconstantize• parameterize• tableize• classify• constantize• humanize• foreign_key
  19. 19. ROR Lab.감사합니다.
  20. 20.  

×