Created
October 6, 2014 14:28
-
-
Save adz/ee96eea854fa316bef42 to your computer and use it in GitHub Desktop.
Simple null object
This file contains 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
the next common error is likely from side effects: "How did the value of this variable change?!" | |
immutability... |
This file contains 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 CoursesController < ApplicationController | |
expose(:course, attributes: :course_params) | |
expose(:current_course) { current_user.courses.find_by_id(params[:id] || params[:course_id]) } | |
expose(:first_of_each_month_of_course) { (current_course.start_date.change(day: 1)..current_course.end_date).select{ |d| d.day == 1 } } | |
# ... | |
end | |
# Orginal code gives: | |
# line 4 of [PROJECT_ROOT]/app/controllers/courses_controller.rb: block in <class:CoursesController> | |
NoMethodError - undefined method `start_date' for nil:NilClass |
This file contains 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 CoursesController < ApplicationController | |
expose(:course, attributes: :course_params) | |
expose(:current_course) { id = params.fetch(:id) { params.fetch(:course_id) } | |
current_user.courses.find_by_id(id) } | |
expose(:first_of_each_month_of_course) { (current_course.start_date.change(day: 1)..current_course.end_date).select{ |d| d.day == 1 } } | |
end | |
# Now code gives: | |
# line 3 of [PROJECT_ROOT]/app/controllers/courses_controller.rb: block in <class:CoursesController> | |
KeyError: key not found: :course_id | |
This file contains 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
...looup gary's sentinel object | |
Sentinel Values still blow up when you hit them, they just make it easy to understand why |
This file contains 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 Client < ActiveRecord::Base | |
belongs_to :pricing_level | |
end | |
class PricingLevel | |
def self.find_default | |
#... | |
end | |
end | |
class Guest | |
def name | |
'Guest' | |
end | |
def pricing_level | |
PricingLevel.find_default | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment