lib/teapot/environment/flatten.rb in teapot-0.9.10 vs lib/teapot/environment/flatten.rb in teapot-1.0.0.pre.rc1

- old
+ new

@@ -16,13 +16,19 @@ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. +require 'digest/md5' + module Teapot class Environment - def flatten + def to_h + @values + end + + def to_hash hash = {} # Flatten this chain of environments: flatten_to_hash(hash) @@ -31,11 +37,46 @@ # Evaluate all the individual environment values so that they are flat: Hash[hash.map{|key, value| [key, evaluator.object_value(value)]}] end + def flatten + self.class.new(nil, self.to_hash) + end + + def defined + @values.select{|name,value| Define === value} + end + + def inspect(output = $stdout, indent = "") + @values.each do |(key, value)| + output.puts "#{indent}#{key}: #{value}" + end + + @parent.inspect(output, indent + "\t") if @parent + end + + # This should be stable within environments that produce the same results. + def checksum + digester = Digest::MD5.new + + checksum_recursively(digester) + + return digester.hexdigest + end + protected + def checksum_recursively(digester) + @values.each do |(key, value)| + digester.update(key.to_s) + digester.update(value.to_s) + end + + @parent.checksum_recursively(digester) if @parent + end + + # We fold in the ancestors one at a time from oldest to youngest. def flatten_to_hash(hash) if @parent @parent.flatten_to_hash(hash) end