diff options
author | Thom May <thom@chef.io> | 2017-01-25 15:51:46 +0000 |
---|---|---|
committer | Thom May <thom@chef.io> | 2017-02-14 11:45:44 -0800 |
commit | 46f5722ef83d3d3603e23ac9525c49f4ed43621a (patch) | |
tree | 84724dc91d23b3ef7e5da8da14ffccb4cbc13709 /lib/chef | |
parent | d4b87e5e0f73d92836b4523907e17b1a88be9317 (diff) | |
download | chef-46f5722ef83d3d3603e23ac9525c49f4ed43621a.tar.gz |
Make it easier to have a versioned factorytm/versioned_api
classes providing an API should include VersionedAPI, whilst the factory
class includes VersionedAPIFactory.
Signed-off-by: Thom May <thom@may.lt>
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/mixin/versioned_api.rb | 48 | ||||
-rw-r--r-- | lib/chef/server_api_versions.rb | 4 |
2 files changed, 38 insertions, 14 deletions
diff --git a/lib/chef/mixin/versioned_api.rb b/lib/chef/mixin/versioned_api.rb index b8ec61c5f0..9c2f2f4cdb 100644 --- a/lib/chef/mixin/versioned_api.rb +++ b/lib/chef/mixin/versioned_api.rb @@ -19,31 +19,51 @@ class Chef module Mixin module VersionedAPI - def self.included(base) - # When this file is mixed in, make sure we also add the class methods - base.send :extend, ClassMethods + def minimum_api_version(version = nil) + if version + @minimum_api_version = version + else + @minimum_api_version + end end - module ClassMethods - def versioned_interfaces - @versioned_interfaces ||= [] - end + end - def add_api_version(klass) - versioned_interfaces << klass - end + module VersionedAPIFactory + + def versioned_interfaces + @versioned_interfaces ||= [] end - def select_api_version - self.class.versioned_interfaces.select do |klass| - version = klass.send(:supported_api_version) + def add_versioned_api_class(klass) + versioned_interfaces << klass + end + + def versioned_api_class + versioned_interfaces.select do |klass| + version = klass.send(:minimum_api_version) # min and max versions will be nil if we've not made a request to the server yet, # in which case we'll just start with the highest version and see what happens ServerAPIVersions.instance.min_server_version.nil? || (version >= ServerAPIVersions.instance.min_server_version && version <= ServerAPIVersions.instance.max_server_version) end - .sort { |a, b| a.send(:supported_api_version) <=> b.send(:supported_api_version) } + .sort { |a, b| a.send(:minimum_api_version) <=> b.send(:minimum_api_version) } .last end + + def def_versioned_delegator(method) + line_no = __LINE__; str = %{ + def self.#{method}(*args, &block) + versioned_api_class.__send__(:#{method}, *args, &block) + end + } + module_eval(str, __FILE__, line_no) + end + + def new(*args) + object = versioned_api_class.allocate + object.send(:initialize, *args) + object + end end end end diff --git a/lib/chef/server_api_versions.rb b/lib/chef/server_api_versions.rb index 91591875a4..68dfd5ac90 100644 --- a/lib/chef/server_api_versions.rb +++ b/lib/chef/server_api_versions.rb @@ -32,5 +32,9 @@ class Chef def max_server_version !@versions.nil? ? @versions["max_version"] : nil end + + def reset! + @versions = nil + end end end |