From 25d7544103f0467a811124da68b2c653561d7985 Mon Sep 17 00:00:00 2001 From: Will Koehler Date: Thu, 2 May 2013 16:39:28 -0400 Subject: [PATCH] Add [] operator and has_key? to DataStore to make DataStore act like a hash. The main benefit of this is to add the ability to gracefully handle the case when data is not present. --- .../lib/middleman-core/core_extensions/data.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/middleman-core/lib/middleman-core/core_extensions/data.rb b/middleman-core/lib/middleman-core/core_extensions/data.rb index e0bb1075..68af37d3 100644 --- a/middleman-core/lib/middleman-core/core_extensions/data.rb +++ b/middleman-core/lib/middleman-core/core_extensions/data.rb @@ -179,7 +179,20 @@ module Middleman # Needed so that method_missing makes sense def respond_to?(method, include_private = false) - super || @local_data.has_key?(method.to_s) || !!(data_for_path(method)) + super || has_key?(method) + end + + # Make DataStore act like a hash. Return requested data, or + # nil if data does not exist + # + # @param [String, Symbol] key The name of the data namespace + # @return [Hash, nil] + def [](key) + __send__(key) if has_key?(key) + end + + def has_key?(key) + @local_data.has_key?(key.to_s) || !!(data_for_path(key)) end # Convert all the data into a static hash