From: Intransition Date: 2012-08-03T05:08:30+09:00 Subject: Re: Supporting multiple versions of an API ------=_Part_112_20796321.1343938107797 Content-Type: multipart/alternative; boundary="----=_Part_113_20811638.1343938107797" ------=_Part_113_20811638.1343938107797 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On Thursday, August 2, 2012 12:14:27 PM UTC-4, Robert Klemme wrote: > > On Thu, Aug 2, 2012 at 4:47 PM, Intransition wrote: > > > Yes, that's the traditional factory approach. I actually would not want > to > > use "MainFactory", as don't want it to be explicit. But it occurs to me > that > > I could have redefined `Main.new` as a factory method and done it that > way. > > And the more I think about it the more that seems like a better > approach. It > > would work well for Main as well as other classes within it. > > It seems that in your case the caller decides on the version. Is that > correct? > Yes, but it is actually via YAML file. It will read in, eg. --- revision: 1 ... Then when it does a `Main.load` on the file, it will look for the `revision` field and determine which version of the API to use. > What about this? > > file mylib.rb: > > module MyLib > # manual > autoload :V0 'mylib/v0' > autoload :V1 'mylib/v1' > > # generated > %w{V0 V1}.each do |ver| > autoload ver.to_sym "mylib/#{ver.downcase}" > end > > # the default > Default = V1 > > # to make access to the current version easy: > def self.const_missing(c) > Default.const_get(c) > end > end > > and in mylib/v0 etc. > > module MyLib > module V0 > class Main; end > end > end > > Usage > > # current > m = MyLib::Main.new(...) > > # explicit > m = MyLib::V0::Main.new(...) > > The const_missing trick works only if there are no name conflicts, of > course. > Adding some convenience, nice. I like autoload too, but word has it, it is being deprecated (albeit in the distant future). ------=_Part_113_20811638.1343938107797 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit

On Thursday, August 2, 2012 12:14:27 PM UTC-4, Robert Klemme wrote:
On Thu, Aug 2, 2012 at 4:47 PM, Intransition <transfire@gmail.com> wrote:

> Yes, that's the traditional factory approach. I actually would not want to
> use "MainFactory", as don't want it to be explicit. But it occurs to me that
> I could have redefined `Main.new` as a factory method and done it that way.
> And the more I think about it the more that seems like a better approach. It
> would work well for Main as well as other classes within it.

It seems that in your case the caller decides on the version.  Is that correct?

Yes, but it is actually via YAML file. It will read in, eg.

  ---
  revision: 1
  ...

Then when it does a `Main.load` on the file, it will look for the `revision` field and determine which version of the API to use.

 
What about this?

file mylib.rb:

module MyLib
  # manual
  autoload :V0 'mylib/v0'
  autoload :V1 'mylib/v1'

  # generated
  %w{V0 V1}.each do |ver|
    autoload ver.to_sym "mylib/#{ver.downcase}"
  end

  # the default
  Default = V1

  # to make access to the current version easy:
  def self.const_missing(c)
    Default.const_get(c)
  end
end

and in mylib/v0 etc.

module MyLib
  module V0
    class Main; end
  end
end

Usage

# current
m = MyLib::Main.new(...)

# explicit
m = MyLib::V0::Main.new(...)

The const_missing trick works only if there are no name conflicts, of course.

Adding some convenience, nice. I like autoload too, but word has it, it is being deprecated (albeit in the distant future).

------=_Part_113_20811638.1343938107797-- ------=_Part_112_20796321.1343938107797--