Skip to content

Instantly share code, notes, and snippets.

@hardbap
Forked from tcocca/custom_getters.rb
Created May 3, 2010 19:14
Show Gist options
  • Select an option

  • Save hardbap/388474 to your computer and use it in GitHub Desktop.

Select an option

Save hardbap/388474 to your computer and use it in GitHub Desktop.

Revisions

  1. hardbap revised this gist May 3, 2010. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions custom_getters.rb
    Original file line number Diff line number Diff line change
    @@ -4,16 +4,15 @@ class Post < ActiveRecord::Base

    attr_accessor :post_authors_cache

    def post_authors
    process_post_authors if post_authors_cache.nil?
    post_authors_cache
    def post_authors_cache
    @post_authors_cache ||= process_post_authors
    end

    private

    def process_post_authors
    #do some crazy stuff here and set the post_authors_cache
    self.post_authors_cache = {} #some crazy hash of stuff based on some inane business logic
    #some crazy hash of stuff based on some inane business logic
    end

    end
  2. @tcocca tcocca created this gist May 3, 2010.
    19 changes: 19 additions & 0 deletions custom_getters.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    class Post < ActiveRecord::Base

    has_many :authors

    attr_accessor :post_authors_cache

    def post_authors
    process_post_authors if post_authors_cache.nil?
    post_authors_cache
    end

    private

    def process_post_authors
    #do some crazy stuff here and set the post_authors_cache
    self.post_authors_cache = {} #some crazy hash of stuff based on some inane business logic
    end

    end