Created
          March 8, 2014 07:22 
        
      - 
      
- 
        Save stephenaument/9426713 to your computer and use it in GitHub Desktop. 
    The Null Object Pattern
  
        
  
    
      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 CreateDucks < ActiveRecord::Migration | |
| def change | |
| t.string name | |
| t.string status | |
| t.integer hunger | |
| t.string quack_style | |
| t.string color | |
| t.migratory boolean | |
| 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 CreateDucks < ActiveRecord::Migration | |
| def change | |
| t.string name | |
| t.string status | |
| t.integer hunger | |
| t.string quack_style | |
| t.string color | |
| t.string secondary_color | |
| t.migratory boolean | |
| 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 Duck < ActiveRecord::Base | |
| def awake? | |
| status == 'awake' | |
| end | |
| def quack | |
| puts quack_style | |
| 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 Duck < ActiveRecord::Base | |
| def awake? | |
| status == 'awake' | |
| end | |
| def quack | |
| puts quack_style | |
| end | |
| def multi_colored? | |
| !secondary_color.nil? | |
| 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
    
  
  
    
  | //javascript | |
| { multiColored: #{duck.multi_colored?} } | 
  
    
      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
    
  
  
    
  | // javascript | |
| { multiColored: } // !!! Not a valid JSON object, buddy! | 
  
    
      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
    
  
  
    
  | // javascript | |
| { multiColored: false } | 
  
    
      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 NilDuck | |
| attr_writer :name, :status, :hunger, :quack_style, :color, :migratory | |
| def name | |
| 'Demo Duck' | |
| end | |
| def status | |
| 'sleeping' | |
| end | |
| def hunger | |
| nil | |
| end | |
| def quack | |
| end | |
| def quack_style | |
| nil | |
| end | |
| def color | |
| 'gray' | |
| end | |
| def awake? | |
| false | |
| end | |
| def migratory | |
| nil | |
| end | |
| def migratory? | |
| true | |
| 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 NilDuck | |
| def name | |
| 'Demo Duck' | |
| end | |
| def status | |
| 'sleeping' | |
| end | |
| def color | |
| 'gray' | |
| end | |
| def migratory? | |
| true | |
| end | |
| def method_missing(name, *args) | |
| Duck.new.respond_to?(name) ? nil : 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 NilDuck | |
| def name | |
| 'Demo Duck' | |
| end | |
| def status | |
| 'sleeping' | |
| end | |
| def color | |
| 'gray' | |
| end | |
| def migratory? | |
| true | |
| end | |
| def method_missing(name, *args) | |
| super unless Duck.new.respond_to?(name) | |
| name.ends_with?('?') ? false : nil | |
| end | |
| end | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment