diff options
author | sersut <serdar@opscode.com> | 2014-01-14 16:46:16 -0800 |
---|---|---|
committer | sersut <serdar@opscode.com> | 2014-01-14 16:46:16 -0800 |
commit | a950e9ea474710185fc3d68866c0ebf1660070a6 (patch) | |
tree | a4f58e4f63d410930a503c57c65c805968d42786 | |
parent | d887bb350da5c8832849fe24ae194dc40463e698 (diff) | |
download | ohai-a950e9ea474710185fc3d68866c0ebf1660070a6.tar.gz |
Move constant clean up functions to its own mixin.
-rw-r--r-- | lib/ohai/mixin/constant_helper.rb | 53 | ||||
-rw-r--r-- | lib/ohai/system.rb | 23 | ||||
-rw-r--r-- | spec/spec_helper.rb | 27 | ||||
-rw-r--r-- | spec/unit/plugins/root_group_spec.rb | 2 |
4 files changed, 57 insertions, 48 deletions
diff --git a/lib/ohai/mixin/constant_helper.rb b/lib/ohai/mixin/constant_helper.rb new file mode 100644 index 00000000..63d5acb1 --- /dev/null +++ b/lib/ohai/mixin/constant_helper.rb @@ -0,0 +1,53 @@ +# +# Author:: Serdar Sutay (<serdar@opscode.com>) +# Copyright:: Copyright (c) 2014 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +module Ohai + module Mixin + module ConstantHelper + + def remove_constants + new_object_constants = Object.constants - @object_pristine.constants + new_object_constants.each do |constant| + Object.send(:remove_const, constant) unless Object.const_get(constant).is_a?(Module) + end + + recursive_remove_constants(Ohai::NamedPlugin) + end + + def recursive_remove_constants(object) + if object.respond_to?(:constants) + object.constants.each do |const| + next unless strict_const_defined?(object, const) + recursive_remove_constants(object.const_get(const)) + object.send(:remove_const, const) + end + end + end + + def strict_const_defined?(object, const) + if object.method(:const_defined?).arity == 1 + object.const_defined?(const) + else + object.const_defined?(const, false) + end + end + + end + end +end diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb index f5414cf8..f844504c 100644 --- a/lib/ohai/system.rb +++ b/lib/ohai/system.rb @@ -24,6 +24,7 @@ require 'ohai/dsl' require 'ohai/mixin/command' require 'ohai/mixin/os' require 'ohai/mixin/string' +require 'ohai/mixin/constant_helper' require 'ohai/provides_map' require 'ohai/hints' require 'mixlib/shellout' @@ -31,8 +32,9 @@ require 'mixlib/shellout' require 'yajl' module Ohai - class System + include Ohai::Mixin::ConstantHelper + attr_accessor :data attr_reader :provides_map attr_reader :v6_dependency_solver @@ -189,24 +191,5 @@ module Ohai raise ArgumentError, "I can only generate JSON for Hashes, Mashes, Arrays and Strings. You fed me a #{data.class}!" end end - - private - def recursive_remove_constants(object) - if object.respond_to?(:constants) - object.constants.each do |const| - next unless strict_const_defined?(object, const) - recursive_remove_constants(object.const_get(const)) - object.send(:remove_const, const) - end - end - end - - def strict_const_defined?(object, const) - if object.method(:const_defined?).arity == 1 - object.const_defined?(const) - else - object.const_defined?(const, false) - end - end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a3dc8c95..20572d6d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,32 +19,7 @@ RSpec.configure do |config| config.after(:each) { remove_constants } end -def remove_constants - new_object_constants = Object.constants - @object_pristine.constants - new_object_constants.each do |constant| - Object.send(:remove_const, constant) unless Object.const_get(constant).is_a?(Module) - end - - recursive_remove_constants(Ohai::NamedPlugin) -end - -def recursive_remove_constants(object) - if object.respond_to?(:constants) - object.constants.each do |const| - next unless strict_const_defined?(object, const) - recursive_remove_constants(object.const_get(const)) - object.send(:remove_const, const) - end - end -end - -def strict_const_defined?(object, const) - if object.method(:const_defined?).arity == 1 - object.const_defined?(const) - else - object.const_defined?(const, false) - end -end +include Ohai::Mixin::ConstantHelper if Ohai::Mixin::OS.collect_os == /mswin|mingw32|windows/ ENV["PATH"] = "" diff --git a/spec/unit/plugins/root_group_spec.rb b/spec/unit/plugins/root_group_spec.rb index 9e463dd9..8fac1a37 100644 --- a/spec/unit/plugins/root_group_spec.rb +++ b/spec/unit/plugins/root_group_spec.rb @@ -18,8 +18,6 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') -ORIGINAL_CONFIG_HOST_OS = ::RbConfig::CONFIG['host_os'] - describe Ohai::System, 'root_group' do before(:each) do @plugin = get_plugin("root_group") |