Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Active Support

Introduction to Active Support

Presented for Metis students a thoughtbot office

F1c4a3bb1606cc4a61711e61e2fe6146?s=128

Prem Sichanugrist

June 20, 2014
Tweet

More Decks by Prem Sichanugrist

Other Decks in Programming

Transcript

  1. Introduction to
 Active Support

  2. Notations $ irb # Shell command > str = "Hello

    World" # Ruby code #=> Hello World # Return value
  3. Notations Object.method <= Class method Object#method <= Instance method

  4. What is Active Support?

  5. "Utility classes and Ruby extensions from Rails" (From https://github.com/rails/rails/tree/master/activesupport)

  6. Utility Classes • ActiveSupport::Callbacks • ActiveSupport::HashWithIndifferentAccess • ActiveSupport::MessageEncrypter • ActiveSupport::MessageVerifier

    • ... etc ...
  7. Ruby Extensions • object.present? / object.blank? • date.yesterday / date.tomorrow

    • 1.day.ago / 2.months.from_now • ... etc ...
  8. Part of Rails framework Available to be used inside Rails

  9. # To use outside Rails $ gem install activesupport #

    in Ruby > require "activesupport/all"
  10. Digging Deeper

  11. Callbacks

  12. Callbacks • Helpers to define and run callbacks • Use

    in Active Record, Action Pack, etc. • before_action, after_action, before_save, etc.
  13. Callback Example class User < ActiveRecord::Base before_save :do_something def do_something

    # ... end end
  14. Callback Example class Account include ActiveSupport::Callbacks define_callbacks :save set_callback :save,

    :before, :do_something def save run_callbacks :save do # ... end end def do_something # ... end end
  15. MessageEncrypter Encrypts message with a key

  16. MessageEncrypter > salt = SecureRandom.random_bytes(64) > key = ActiveSupport::KeyGenerator. new('password').generate_key(salt)

    > crypt = ActiveSupport::MessageEncryptor.new(key) > encrypted_data = crypt. encrypt_and_sign('my secret data') > crypt.decrypt_and_verify(encrypted_data)
  17. Notifications

  18. Notifications • Uses for logging purposes • Executer instrument an

    event that should be subscribed to: • Action View's "render" • Active Record's "execute SQL" • etc. • Listeners subscribe to those events from another part of the application
  19. TimeZone

  20. TimeZone • Contains full mapping of time zones • Perform

    time zone conversions
  21. TimeZone > ActiveSupport::TimeZone.all > ActiveSupport::TimeZone.us_zones > Time.zone = "America/New_York" >

    time = Time.zone.now #=> "Fri, 20 Jun 2014 14:35:00 EDT -04:00" > time.in_time_zone("America/Los_Angeles") #=> "Fri, 20 Jun 2014 11:35:00 PDT -07:00"
  22. Core Extensions

  23. Array

  24. Array#from Array#to

  25. Array#from Array#to > array = [1, 2, 3, 4] >

    array.from(2) #=> [3, 4] > array.to(2) #=> [1,2,3]
  26. Array#second Array#third Array#fourth Array#fifth Array#forty_two

  27. Array Access > array = (1..100).to_a > array.first #=> 1

    > array.second #=> 2 > array.third #=> 3 > array.fourth #=> 4 > array.fifth #=> 5 > array.forty_two #=> 42
  28. Array#to_sentence

  29. Array#to_sentence > fruits = %w(banana strawberry kiwi) > fruits.to_sentence #=>

    "banana, strawberry, and kiwi > sports = %w(football baseball) > sports.to_sentence #=> "football and baseball"
  30. Array#in_groups_of Array#in_groups

  31. Array#in_group_of > array = (1..10).to_a > array.in_group_of(3) # => [[1,

    2, 3], [4, 5, 6], [7, 8, 9], [10, nil, nil]]
  32. Array#in_groups > array = (1..10).to_a > array.in_groups(3) #=> [[1, 2,

    3, 4], [5, 6, 7, nil], [8, 9, 10, nil]]
  33. Date, Time, DateTime

  34. Date.current Date.yesterday Date.tomorrow

  35. Time#beginning_of_day Time#middle_of_day Time#end_of_day

  36. Time#beginning_of_hour Time#end_of_hour

  37. Time#all_day Date#all_week Date#all_month Date#all_year

  38. Time#today? Time#past? Time#future?

  39. Hash

  40. Hash#deep_merge

  41. Hash#deep_merge > h1 = { a: true, b: { c:

    [1, 2, 3] } } > h2 = { a: false, b: { x: [3, 4, 5] } } > h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
  42. Hash#except

  43. Hash#except > hash = { one: 1, two: 2 }

    > hash.except(:one) #=> { two: 2 }
  44. Hash#with_indifferent_access

  45. # In Controller > params[:id] #=> 1 > params['id'] #=>

    1 > params.class #=> ActiveSupport::HashWithIndifferentAccess
  46. Hash#with_indifferent_access > hash = { one: 1 }.with_indifferent_access > hash[:one]

    #=> 1 > hash['one'] #=> 1
  47. Hash#stringify_keys Hash#symbolize_keys

  48. Hash#stringify_keys Hash#symbolize_keys > hash = { one: 1, 'two' =>

    2 } > hash.stringify_keys #=> { 'one' => 1, 'two' => 2 } > hash.symbolize_keys #=> { one: 1, two: 2 }
  49. Hash#reverse_merge

  50. Hash#reverse_merge > h1 = { one: 'one' } > h2

    = { one: 'uno', two: 'dos' } > h1.merge(h2) #=> { one: 'uno', two: 'dos' } > h1.reverse_merge(h2) #=> { one: 'one', two: 'dos' }
  51. Hash#slice

  52. Hash#slice > hash = { one: 1, two: 2, three:

    3 } > hash.slice(:one, :two) #=> { one: 1, two: 2 }
  53. Integer

  54. Integer#ordinalize

  55. Integer#ordinalize > 1.ordinalize #=> "1st" > 2.ordinalize #=> "2nd"

  56. Integer#ordinal

  57. Integer#ordinalize > 1.ordinal #=> "st" > 2.ordinalize #=> "nd"

  58. Integer#days Integer#months Integers#years

  59. Examples > 1.month.ago > 1.month.from_now > 1.month.since(time) > 1.year.from(time)

  60. Object

  61. Object#present? Object#blank?

  62. Object#try

  63. Object#try > user = nil > user.name #=> NoMethodError >

    user.try(:name) #=> nil
  64. Object#presence

  65. Object#presence > name = "John" > puts "Hello #{name}" #=>

    "Hello John"
  66. Object#presence > name = "" > puts "Hello #{name}" #=>

    "Hello "
  67. Object#presence > name = "" > puts "Hello #{name.present? ?

    name : "Guest"}" #=> "Hello Guest"
  68. Object#presence > name = "" > puts "Hello #{name.presence ||

    "Guest"}" #=> "Hello Guest"
  69. String

  70. String#to_time String#to_date String#to_datetime

  71. String#to_time String#to_date String#to_datetime > "13-12-2012".to_time #=> 2012-12-13 00:00:00 -0500 >

    "1-1-2012".to_date #=> Sun, 01 Jan 2012 > "1-1-2012".to_datetime #=> Sun, 01 Jan 2012 00:00:00 +0000
  72. String#truncate

  73. String#truncate > "Hello World".truncate(8) #=> "Hello Wo..."

  74. String#singularize String#pluralize String#camelize String#titleize String#humanize

  75. Inflections Example > "man".pluralize #=> "men" > "octopi".singularize #=> "octopus"

    > "user_name".camelize #=> "UserName" > "hello world".titlize #=> "Hello World" > "full_name".humanize #=> "Full name"
  76. String#inquiry

  77. String#inquiry > color = "red".inquiry > color.red? #=> true >

    color.blue? #=> false > Rails.env.development?
  78. http://guides.rubyonrails.org/active_support_core_extensions.html