Active Support Core Extension (3)
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Active Support Core Extension (3)

on

  • 672 views

 

Statistics

Views

Total Views
672
Views on SlideShare
415
Embed Views
257

Actions

Likes
0
Downloads
1
Comments
0

3 Embeds 257

http://rorlab.org 216
http://localhost 40
https://twitter.com 1

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Active Support Core Extension (3) Presentation Transcript

  • 1. Active SupportCore Extensions (3)ROR lab. DD-1- The 3rd round -April 13, 2013Hyoseong Choi
  • 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. 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. 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. 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. 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. 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. Ext. to String• squish, squish!active_support/core_ext/string/filters.rb" n  foonr t bar n".squish # => "foo bar"
  • 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. Ext. to String• Output safety - inquiryactive_support/core_ext/string/inquiry.rb"production".inquiry.production? # => true"active".inquiry.inactive?       # => falsea StringInquirerobject
  • 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. 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. 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. 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. 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. 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. 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. 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. ROR Lab.감사합니다.
  • 20.