A tool to complement extension services and foster active ...
Active Support Core Extension (3)
description
Transcript of Active Support Core Extension (3)
![Page 1: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/1.jpg)
Active SupportCore Extensions (3)
ROR lab. DD-1- The 3rd round -
April 13, 2013
Hyoseong Choi
![Page 2: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/2.jpg)
Ext. to Class• class_attribute
active_support/core_ext/class/attribute.rb
class A class_attribute :xend class B < A; end class C < B; end A.x = :aB.x # => :aC.x # => :a
B.x = :bA.x # => :aC.x # => :b C.x = :cA.x # => :aB.x # => :b
one or more inheritable class attributes that can be overridden at any level down the hierarchy
![Page 3: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/3.jpg)
Ext. to Class• class_inheritable_accessor, _reader, _writer
active_support/core_ext/class/inheritable_attributes.rb
module 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 instead
accessors for class-level data which is inherited but not shared with children
![Page 4: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/4.jpg)
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• subclasses
active_support/core_ext/class/subclasses.rb
C
B
A
D
![Page 5: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/5.jpg)
Ext. to Class• descendants
active_support/core_ext/class/subclasses.rb
class C; endC.descendants # => [] class B < C; endC.descendants # => [B] class A < B; endC.descendants # => [B, A] class D < C; endC.descendants # => [B, A, D]
C
B
A
D
![Page 6: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/6.jpg)
Ext. to String• Strings “(html) unsafe” by default since Rails3
"".html_safe? # => false
s = "".html_safes.html_safe? # => true
• html_safe no escaping
s = "<script>...</script>".html_safes.html_safe? # => trues # => "<script>...</script>"
active_support/core_ext/string/output_safety.rb
![Page 7: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/7.jpg)
Ext. to String• html_safe
active_support/core_ext/string/output_safety.rb
<%= raw @cms.current_template %>
<%== @cms.current_template %>
def raw(stringish) stringish.to_s.html_safeend
![Page 8: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/8.jpg)
Ext. to String• squish, squish!
active_support/core_ext/string/filters.rb
" \n foo\n\r \t bar \n".squish # => "foo bar"
![Page 9: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/9.jpg)
Ext. to String• truncate
active_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 be late!".truncate(20, :omission => '…')# => "Oh dear! Oh …" …
"Oh dear! Oh dear! I shall be late!".truncate(18)# => "Oh dear! Oh dea...""Oh dear! Oh dear! I shall be late!".truncate(18, :separator => ' ')# => "Oh dear! Oh..."
![Page 10: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/10.jpg)
Ext. to String• Output safety - inquiry
active_support/core_ext/string/inquiry.rb
"production".inquiry.production? # => true"active".inquiry.inactive? # => false
a StringInquirer object
![Page 11: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/11.jpg)
Ext. to String• Output safety - Key-based interpolation
active_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
![Page 12: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/12.jpg)
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
![Page 13: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/13.jpg)
Ext. to String• Output safety - strip_heredoc
active_support/core_ext/string/strip.rb
if options[:usage] puts <<-USAGE.strip_heredoc This command does such and such. Supported options are: -h This message ... USAGEend
![Page 14: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/14.jpg)
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
![Page 15: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/15.jpg)
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
![Page 16: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/16.jpg)
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"
![Page 17: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/17.jpg)
Ext. to String• Access - first/last
active_support/core_ext/string/access.rb
str.first(n) or str.to(n-1)
str.last(n) or str.from(-n)
![Page 18: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/18.jpg)
Ext. to String• Inflections
active_support/core_ext/string/inflections.rb
• pluralize
• singularize
• camerlize
• underscore
• titleize
• dasherize
• demodulize
• deconstantize
• parameterize
• tableize
• classify
• constantize
• humanize
• foreign_key
![Page 19: Active Support Core Extension (3)](https://reader036.fdocuments.us/reader036/viewer/2022081821/54bd18a34a7959ff308b4677/html5/thumbnails/19.jpg)
ROR Lab.
감사합니다.����������� ������������������