Created
December 2, 2011 02:04
-
-
Save danmcclain/1421387 to your computer and use it in GitHub Desktop.
Metaprogramming instance methods
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
| module Rubyoverflow | |
| class Client | |
| Rubyoverflow.constants.select {|c| Class === Rubyoverflow.const_get(c) and Rubyoverflow.const_get(c) < Rubyoverflow::Base}.each do |class_sym| | |
| send :attr_reader, class_sym.downcase | |
| send "define_method", class_sym.downcase do | |
| instance_variable_set("@#{class_sym.downcase}", Rubyoverflow.const_get(class_sym).new(self)) unless instance_variable_get("@#{class_sym.downcase}") | |
| instance_variable_get "@#{class_sym.downcase}" | |
| end | |
| end | |
| 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
| module Rubyoverflow | |
| class Client | |
| @users = nil | |
| def users | |
| @user ||= Users.new(self) | |
| end | |
| #Repeated for all the other routes | |
| 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
| module Rubyoverflow | |
| class Users < Rubyoverflow::Base | |
| #... | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment