$30 off During Our Annual Pro Sale. View Details »

Introduction to Active Support

Introduction to Active Support

Presented for Metis students a thoughtbot office

Prem Sichanugrist

June 20, 2014
Tweet

More Decks by Prem Sichanugrist

Other Decks in Programming

Transcript

  1. Introduction to

    Active Support

    View Slide

  2. Notations
    $ irb # Shell command
    > str = "Hello World" # Ruby code
    #=> Hello World # Return value

    View Slide

  3. Notations
    Object.method <= Class method
    Object#method <= Instance method

    View Slide

  4. What is Active Support?

    View Slide

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

    View Slide

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

    View Slide

  7. Ruby Extensions
    • object.present? / object.blank?
    • date.yesterday / date.tomorrow
    • 1.day.ago / 2.months.from_now
    • ... etc ...

    View Slide

  8. Part of Rails framework
    Available to be used inside Rails

    View Slide

  9. # To use outside Rails
    $ gem install activesupport
    # in Ruby
    > require "activesupport/all"

    View Slide

  10. Digging Deeper

    View Slide

  11. Callbacks

    View Slide

  12. Callbacks
    • Helpers to define and run callbacks
    • Use in Active Record, Action Pack, etc.
    • before_action, after_action, before_save, etc.

    View Slide

  13. Callback Example
    class User < ActiveRecord::Base
    before_save :do_something
    def do_something
    # ...
    end
    end

    View Slide

  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

    View Slide

  15. MessageEncrypter
    Encrypts message with a key

    View Slide

  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)

    View Slide

  17. Notifications

    View Slide

  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

    View Slide

  19. TimeZone

    View Slide

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

    View Slide

  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"

    View Slide

  22. Core Extensions

    View Slide

  23. Array

    View Slide

  24. Array#from
    Array#to

    View Slide

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

    View Slide

  26. Array#second
    Array#third
    Array#fourth
    Array#fifth
    Array#forty_two

    View Slide

  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

    View Slide

  28. Array#to_sentence

    View Slide

  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"

    View Slide

  30. Array#in_groups_of
    Array#in_groups

    View Slide

  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]]

    View Slide

  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]]

    View Slide

  33. Date, Time, DateTime

    View Slide

  34. Date.current
    Date.yesterday
    Date.tomorrow

    View Slide

  35. Time#beginning_of_day
    Time#middle_of_day
    Time#end_of_day

    View Slide

  36. Time#beginning_of_hour
    Time#end_of_hour

    View Slide

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

    View Slide

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

    View Slide

  39. Hash

    View Slide

  40. Hash#deep_merge

    View Slide

  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] } }

    View Slide

  42. Hash#except

    View Slide

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

    View Slide

  44. Hash#with_indifferent_access

    View Slide

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

    View Slide

  46. Hash#with_indifferent_access
    > hash = { one: 1 }.with_indifferent_access
    > hash[:one] #=> 1
    > hash['one'] #=> 1

    View Slide

  47. Hash#stringify_keys
    Hash#symbolize_keys

    View Slide

  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 }

    View Slide

  49. Hash#reverse_merge

    View Slide

  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' }

    View Slide

  51. Hash#slice

    View Slide

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

    View Slide

  53. Integer

    View Slide

  54. Integer#ordinalize

    View Slide

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

    View Slide

  56. Integer#ordinal

    View Slide

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

    View Slide

  58. Integer#days
    Integer#months
    Integers#years

    View Slide

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

    View Slide

  60. Object

    View Slide

  61. Object#present?
    Object#blank?

    View Slide

  62. Object#try

    View Slide

  63. Object#try
    > user = nil
    > user.name #=> NoMethodError
    > user.try(:name) #=> nil

    View Slide

  64. Object#presence

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  69. String

    View Slide

  70. String#to_time
    String#to_date
    String#to_datetime

    View Slide

  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

    View Slide

  72. String#truncate

    View Slide

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

    View Slide

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

    View Slide

  75. Inflections Example
    > "man".pluralize #=> "men"
    > "octopi".singularize #=> "octopus"
    > "user_name".camelize #=> "UserName"
    > "hello world".titlize #=> "Hello World"
    > "full_name".humanize #=> "Full name"

    View Slide

  76. String#inquiry

    View Slide

  77. String#inquiry
    > color = "red".inquiry
    > color.red? #=> true
    > color.blue? #=> false
    > Rails.env.development?

    View Slide

  78. http://guides.rubyonrails.org/active_support_core_extensions.html

    View Slide