- Ruby Delegate.rb Secrets
- Simple presenters to clean up views
- Ruby Forwardable in Depth
- how it works
- SingleForwardable
- identifying responsibility leaks with if
- enforcing encapsulation
- seeing the westward flow and heading east
- ActiveSupport::Delegation, the Rails approach to forwarding
- The Casting gem and why it's different from SimpleDelegator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'resolv' | |
| class EmailValidator < ActiveModel::EachValidator | |
| def validate_each(record, attribute, value) | |
| if Resolv::DNS.new.getresources(value.split("@").last, Resolv::DNS::Resource::IN::MX).empty? | |
| record.errors[attribute] << (options[:message] || "does not have a valid domain") | |
| end | |
| rescue Resolv::ResolvError, Resolv::ResolvTimeout | |
| record.errors[attribute] << (options[:message] || "does not have a valid domain") | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| mod = Module.new | |
| mod.module_eval %{ | |
| def fill_out_#{name}(value) | |
| checkbox = find(:#{name}) | |
| if (#{name}_checked_values.include?(value) && !checkbox.selected?) || | |
| (#{name}_unchecked_values.include?(value) && checkbox.selected?) | |
| checkbox.click | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def with_conditions(method_name, computed_value) | |
| if method_name.to_s == 'address' && !computed_value.nil? | |
| address.renderable? | |
| else | |
| super | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Exception | |
| def cause_message | |
| cause && cause.message | |
| end | |
| def cause_backtrace | |
| cause && cause.backtrace | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Introduction .....................................................................................................6 | |
| What This Is ..................................................................................................................8 | |
| What This Is Not ...........................................................................................................8 | |
| Lost In Translation...........................................................................................9 | |
| Our First Bugs..............................................................................................................10 | |
| Object Orientation ........................................................................................12 | |
| Where We Are..............................................................................................................12 | |
| Where We’ve Been .......................................................................................................20 | |
| Where We’re Going .................... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'delegate' | |
| # Public: This class initializes a presenter object. | |
| # The presenter wraps a given object and a view object and will forward | |
| # method calls to each. By default, any unknown method will be passed | |
| # to the first object. | |
| # Any methods that need to be sent to the view, may be done explicitly. | |
| # | |
| # Examples: | |
| # | |
| # # Inside a controller you create a helper method to access this object |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| seed = [[0,0],[0,1],[0,2]] | |
| puts "First generation:" | |
| puts seed.to_s | |
| # Point surrounding a given point | |
| perimeter = ->(x, y){ | |
| [ | |
| [x - 1, y + 1], [x, y + 1], [x + 1, y + 1], | |
| [x - 1, y ], [x + 1, y ], | |
| [x - 1, y - 1], [x, y - 1], [x + 1, y - 1] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'delegate' | |
| class SpecialFormatter < SimpleDelegator | |
| def initialize(user, profile) | |
| super(user) | |
| @profile = profile | |
| end | |
| attr_reader :profile | |
| def self.maybe(*names) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def custom_mail_to(email, link_text, i18n_subject_key) | |
| mail_to(email, link_text, :subject => I18n.t(i18n_subject_key, :email => email)) | |
| end |