[Pkg-puppet-devel] [facter] 174/352: (FACT-237) Implement deep_freeze for composed facts

Stig Sandbeck Mathisen ssm at debian.org
Sun Apr 6 22:21:43 UTC 2014


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository facter.

commit c5a0ecd9ffca2f31a2f0fc7aa4913eba082913ce
Author: Adrien Thebo <git at somethingsinistral.net>
Date:   Mon Jan 6 11:07:48 2014 -0800

    (FACT-237) Implement deep_freeze for composed facts
---
 lib/facter/util/values.rb     | 25 +++++++++++++++++++++++++
 spec/unit/util/values_spec.rb | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)

diff --git a/lib/facter/util/values.rb b/lib/facter/util/values.rb
index 938f6fe..f1e667c 100644
--- a/lib/facter/util/values.rb
+++ b/lib/facter/util/values.rb
@@ -4,6 +4,31 @@ module Facter
     module Values
       module_function
 
+      # Duplicate and deeply freeze a given data structure
+      #
+      # @param value [Object] The structure to freeze
+      # @return [void]
+      def deep_freeze(value)
+        case value
+        when Numeric, Symbol, TrueClass, FalseClass, NilClass
+          # These are immutable values, we can safely ignore them
+          value
+        when String
+          value.dup.freeze
+        when Array
+          value.map do |entry|
+            deep_freeze(entry)
+          end.freeze
+        when Hash
+          value.inject({}) do |hash, (key, value)|
+            hash[deep_freeze(key)] = deep_freeze(value)
+            hash
+          end.freeze
+        else
+          raise ArgumentError, "Cannot deep freeze #{value}:#{value.class}"
+        end
+      end
+
       # Perform a deep merge of two nested data structures.
       #
       # @param left [Object]
diff --git a/spec/unit/util/values_spec.rb b/spec/unit/util/values_spec.rb
index 570386a..f3bb7f9 100644
--- a/spec/unit/util/values_spec.rb
+++ b/spec/unit/util/values_spec.rb
@@ -2,6 +2,47 @@ require 'spec_helper'
 require 'facter/util/values'
 
 describe Facter::Util::Values do
+  describe 'deep_freeze' do
+    it "it dups and freezes strings" do
+      input = "hi"
+      output = described_class.deep_freeze(input)
+      expect(input).to_not be_frozen
+      expect(output).to be_frozen
+    end
+
+    it "freezes arrays and each element in the array" do
+      input = %w[one two three]
+      output = described_class.deep_freeze(input)
+
+      input.each { |entry| expect(entry).to_not be_frozen }
+      output.each { |entry| expect(entry).to be_frozen }
+
+      expect(input).to_not be_frozen
+      expect(output).to be_frozen
+    end
+
+    it "freezes hashes and each key and value in the hash" do
+      input = {'one' => 'two', 'three' => 'four'}
+
+      output = described_class.deep_freeze(input)
+
+      input.each_pair do |key, val|
+        # Ruby freezes all string keys, so these will always be frozen
+        expect(key).to be_frozen
+        expect(val).to_not be_frozen
+      end
+
+      output.each_pair do |key, val|
+        expect(key).to be_frozen
+        expect(val).to be_frozen
+      end
+
+      expect(input).to_not be_frozen
+      expect(output).to be_frozen
+    end
+
+  end
+
   describe 'deep_merge' do
     it "non-destructively concatenates arrays" do
       first = %w[foo bar]

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git



More information about the Pkg-puppet-devel mailing list