Skip to content

Instantly share code, notes, and snippets.

@chancancode
Created September 3, 2010 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chancancode/563455 to your computer and use it in GitHub Desktop.
Save chancancode/563455 to your computer and use it in GitHub Desktop.
# 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