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

Prem Sichanugrist

June 20, 2014
Tweet

More Decks by Prem Sichanugrist

Other Decks in Programming

Transcript

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

    World" # Ruby code #=> Hello World # Return value
  2. # To use outside Rails $ gem install activesupport #

    in Ruby > require "activesupport/all"
  3. Callbacks • Helpers to define and run callbacks • Use

    in Active Record, Action Pack, etc. • before_action, after_action, before_save, etc.
  4. 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
  5. 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)
  6. 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
  7. 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"
  8. Array#from Array#to > array = [1, 2, 3, 4] >

    array.from(2) #=> [3, 4] > array.to(2) #=> [1,2,3]
  9. 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
  10. 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"
  11. 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] } }
  12. Hash#except > hash = { one: 1, two: 2 }

    > hash.except(:one) #=> { two: 2 }
  13. # In Controller > params[:id] #=> 1 > params['id'] #=>

    1 > params.class #=> ActiveSupport::HashWithIndifferentAccess
  14. 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 }
  15. 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' }
  16. Hash#slice > hash = { one: 1, two: 2, three:

    3 } > hash.slice(:one, :two) #=> { one: 1, two: 2 }
  17. 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
  18. Inflections Example > "man".pluralize #=> "men" > "octopi".singularize #=> "octopus"

    > "user_name".camelize #=> "UserName" > "hello world".titlize #=> "Hello World" > "full_name".humanize #=> "Full name"
  19. String#inquiry > color = "red".inquiry > color.red? #=> true >

    color.blue? #=> false > Rails.env.development?