ACTIVESUPPORT




Sunday, September 12, 2010
WHOAMI




Sunday, September 12, 2010
HOW WE WRITE MODULES




Sunday, September 12, 2010
module M
                              def self.included(base)
                               base.extend, ClassMethods
  ...
previously wrote

                             HOW WE WRITE MODULES




Sunday, September 12, 2010
ACTIVESUPPORT::CONCERN




Sunday, September 12, 2010
module M
                              extend ActiveSupport::Concern

                              included do
          ...
Why use it?




Sunday, September 12, 2010
...it handles dependencies to other
                                            modules!



Sunday, September 12, 2010
module Bar
                              extend ActiveSupport::Concern
                              include Foo

        ...
ActiveSupport::Concern in the Wild




Sunday, September 12, 2010
module Mongoid #:nodoc
                              module Indexes #:nodoc
                               extend ActiveSu...
PLUGGABLE JSON BACKENDS




Sunday, September 12, 2010
#to_json calls ActiveSupport::JSON.encode()




Sunday, September 12, 2010
Rails used to only have YAML-based JSON support




Sunday, September 12, 2010
ActiveSupport::JSON.backend = 'Yajl'




Sunday, September 12, 2010
YAJL BENCHMARKS
                                Yajl::Encoder#encode: 3.59s
                                      JSON#to_...
PRESENCE




Sunday, September 12, 2010
BEFORE


                             name = params[:name] unless params[:name].blank?




Sunday, September 12, 2010
NOW


                             name = params[:name] if params[: name].present?




Sunday, September 12, 2010
BEFORE

                             state = params[:state] unless params[:state].blank?
                             coun...
NOW


        region = params[:state].presence || params[:country].presence || 'US'




Sunday, September 12, 2010
ARRAY#UNIQ_BY




Sunday, September 12, 2010
[1, 2, 3, 4].uniq_by(&:odd?)
                                       #=> [1, 2]




Sunday, September 12, 2010
CLASS_ATTRIBUTE




Sunday, September 12, 2010
Declare one or more class-level attribute
                             whose value is inheritable and over-writable by
   ...
>> Base.setting = "foo" => "foo"
                             >> Subclass.setting => "foo"
                             >>...
QUESTIONS?




Sunday, September 12, 2010
Upcoming SlideShare
Loading in …5
×

ActiveSupport

1,395 views

Published on

A lightning talk about some changes to ActiveSupport in Ruby on Rails 3 given at the Toronto Ruby Brigade

Published in: Technology
  • Be the first to comment

  • Be the first to like this

ActiveSupport

  1. 1. ACTIVESUPPORT Sunday, September 12, 2010
  2. 2. WHOAMI Sunday, September 12, 2010
  3. 3. HOW WE WRITE MODULES Sunday, September 12, 2010
  4. 4. module M def self.included(base) base.extend, ClassMethods base.send(:include, InstanceMethods) scope :disabled, where(:disabled => true) end module ClassMethods ... end module InstanceMethods ... end end Sunday, September 12, 2010
  5. 5. previously wrote HOW WE WRITE MODULES Sunday, September 12, 2010
  6. 6. ACTIVESUPPORT::CONCERN Sunday, September 12, 2010
  7. 7. module M extend ActiveSupport::Concern included do scope :disabled, where(:disabled => true) end module ClassMethods ... end module InstanceMethods ... end end Sunday, September 12, 2010
  8. 8. Why use it? Sunday, September 12, 2010
  9. 9. ...it handles dependencies to other modules! Sunday, September 12, 2010
  10. 10. module Bar extend ActiveSupport::Concern include Foo included do self.method_injected_by_foo end end class Host include Bar end Sunday, September 12, 2010
  11. 11. ActiveSupport::Concern in the Wild Sunday, September 12, 2010
  12. 12. module Mongoid #:nodoc module Indexes #:nodoc extend ActiveSupport::Concern included do cattr_accessor :index_options self.index_options = {} end module ClassMethods #:nodoc # Send the actual index creation comments to the MongoDB driver def create_indexes return unless index_options current_collection = self._collection || set_collection index_options.each do |name, options| current_collection.create_index(name, options) end end ... end end end Sunday, September 12, 2010
  13. 13. PLUGGABLE JSON BACKENDS Sunday, September 12, 2010
  14. 14. #to_json calls ActiveSupport::JSON.encode() Sunday, September 12, 2010
  15. 15. Rails used to only have YAML-based JSON support Sunday, September 12, 2010
  16. 16. ActiveSupport::JSON.backend = 'Yajl' Sunday, September 12, 2010
  17. 17. YAJL BENCHMARKS Yajl::Encoder#encode: 3.59s JSON#to_json: 6.2s ActiveSupport::JSON.encode: 45.58s Yajl::Parser#parse: 4.54s JSON.parse: 5.47s ActiveSupport::JSON.decode: 64.42s Sunday, September 12, 2010
  18. 18. PRESENCE Sunday, September 12, 2010
  19. 19. BEFORE name = params[:name] unless params[:name].blank? Sunday, September 12, 2010
  20. 20. NOW name = params[:name] if params[: name].present? Sunday, September 12, 2010
  21. 21. BEFORE state = params[:state] unless params[:state].blank? country = params[:country] unless params[:country].blank? region = state || country || 'US' Sunday, September 12, 2010
  22. 22. NOW region = params[:state].presence || params[:country].presence || 'US' Sunday, September 12, 2010
  23. 23. ARRAY#UNIQ_BY Sunday, September 12, 2010
  24. 24. [1, 2, 3, 4].uniq_by(&:odd?) #=> [1, 2] Sunday, September 12, 2010
  25. 25. CLASS_ATTRIBUTE Sunday, September 12, 2010
  26. 26. Declare one or more class-level attribute whose value is inheritable and over-writable by subclasses and instances Sunday, September 12, 2010
  27. 27. >> Base.setting = "foo" => "foo" >> Subclass.setting => "foo" >> Subclass.setting = "bar" => "bar" >> Base.setting => "foo" Sunday, September 12, 2010
  28. 28. QUESTIONS? Sunday, September 12, 2010

×