Hi Chris,
Trying to understand what you mean by avoiding a db-backed Source. Are you
saying that data is pulled from the URL or as a set of REST call, but not
stored locally? I see two sets of questions here (if I'm understanding
correctly)
1) How to store and retrieve the data in your model
2) How to access that data in a Ruby like way
Your code examples seem to focus on the data access, so I'm assuming that's
where your questions lie.
In your code examples the "find_by_url" would be a class method def
self.find_by_url(url)
That would instantiate a new Source object and return it. That source object
would then have the various attribute data internally and you'd access them
with methods on that object.
Example:
class Source
# attr_accessor defines getter and setter methods that deal with the instance
variables for you
# it is also worth noting that you don't have to set @short_name, etc to
anything in the initializer if you want them to be nil
attr_accessor :short_name, :account, :profile_name, :url
def initialize(args)
args.each do |key, value|
send("@#{key}=", value)
end
# Here is where I would pull data in from the data source and set the
instance variables (@variable).
# You can then access them from source.short_name etc or set them with
srouce.short_name =
# because we did attr_accessor
end
def self.find_by_url(url)
Source.new(:url => url) # This might be some sort of lookup before
instantiation... not clear from your description
end
end
Best,
Rob
On Nov 16, 2011, at 11:37 , Chris Radcliff wrote:
> I'm new to Ruby and Rails, but I'm diving in with both feet. Resources
> around the web have been great so far, but I can't seem to find the
> idiomatic way to write a plug-in style architecture. Specifically:
>
> Each Item in my model belongs to a Source identified by a shortname
> (like bigsource). Rather than have a db-backed Source, I need to have
> an arbitrary set of source types (bigsource, littlesource,
> specialsource, etc), each of which are developed separately. Each
> source type needs to do two things:
>
> 1. identify itself as the "answer" to a "question" in the form of a
> URL. Newbcode examples:
>>> Source.find_by_url("http://bigsource.com/foo").shortname
> => 'bigsource'
>>> Source.find_by_url("http://mrsmith.littlesource.com/").shortname
> => 'littlesource'
>>> Source.find_by_url("http://google.com/")
> => nil
>
> 2. answer a predefined set of questions about the URL it claims to
> know about, using whatever code it needs to get the answers. Newbcode
> examples:
>>> Source.find_by_url("http://bigsource.com/foo").account
> => "1234567890"
>>> Source.find_by_url("http://mrsmith.littlesource.com/").profile_name
> => "John Smith"
>
> I have *my* way to do it, but what's the idiomatic Ruby way to define
> these? Can you point me to any examples that work kinda like this?
>
> Thanks,
> ~chris
>
> --
> SD Ruby mailing list
> [email protected]
> http://groups.google.com/group/sdruby
--
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby