Created
September 3, 2010 05:19
-
-
Save chancancode/563455 to your computer and use it in GitHub Desktop.
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
# This is what I wanted to do... | |
class MyObject < SomeSuperClass | |
include SomeWebService::DataSource | |
data_source_id '123456' # data_source_id() is a class method added by the module | |
def initialize(name) | |
@name = name | |
end | |
def name | |
data_source[:prefix] + @name # data_source() is an instance method added by the module | |
end | |
def some_other_attr | |
{ :some => data_source[:some], :other => 'other', :attr => 'attribute' } | |
end | |
# ... | |
end | |
# This is how I think I should implement it... | |
module SomeWebService | |
module DataSource | |
module ClassMethod | |
def data_source_id(id) | |
@@id = id # Store the key somewhere... but how? instance var? class var? ...? | |
end | |
def get_data | |
@@data ||= SomeWebService::Request.request_data(@@id) # Again, how to cache this and how to access the id? | |
end | |
end | |
def self.included(base) | |
base.extend(ClassMethod) | |
end | |
def data_source | |
get_data # How to call the class method from here? | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment