Permalink
Browse files

Move constant clean up functions to its own mixin.

  • Loading branch information...
1 parent d887bb3 commit a950e9ea474710185fc3d68866c0ebf1660070a6 sersut committed Jan 15, 2014
Showing with 57 additions and 48 deletions.
  1. +53 −0 lib/ohai/mixin/constant_helper.rb
  2. +3 −20 lib/ohai/system.rb
  3. +1 −26 spec/spec_helper.rb
  4. +0 −2 spec/unit/plugins/root_group_spec.rb
View
53 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
View
23 lib/ohai/system.rb
@@ -24,15 +24,17 @@
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'
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 @@ def attributes_print(a)
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
View
27 spec/spec_helper.rb
@@ -19,32 +19,7 @@
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"] = ""
View
2 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")

0 comments on commit a950e9e

Please sign in to comment.