Sha256: a763e5480f1f2b9e7592dd3e6f534fc03e211798be306acaf6ae69f95c07e810

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

module Dottie
  module Methods
    
    ##
    # Reads from the Hash or Array with special handling for Dottie-style keys.
    
    def [](key)
      if Dottie.dottie_key?(key)
        Dottie.get(wrapped_object_or_self, key)
      else
        super
      end
    end
    
    ##
    # Writes to the Hash or Array with special handling for Dottie-style keys,
    # adding missing Hash nodes or Array elements where necessary.
    
    def []=(key, value)
      if Dottie.dottie_key?(key)
        Dottie.set(wrapped_object_or_self, key, value)
      else
        super
      end
    end
    
    ##
    # Checks whether the Hash has the specified key with special handling for
    # Dottie-style keys.
    
    def has_key?(key)
      if Dottie.dottie_key?(key)
        Dottie.has_key?(wrapped_object_or_self, key)
      else
        super
      end
    end
    
    ##
    # Fetches a value from the Hash with special handling for Dottie-style keys.
    # Handles the optional default value and block the same as Hash#fetch.
    
    def fetch(key, default = :_fetch_default_, &block)
      if Dottie.dottie_key?(key)
        if default != :_fetch_default_
          Dottie.fetch(wrapped_object_or_self, key, default, &block)
        else
          Dottie.fetch(wrapped_object_or_self, key, &block)
        end
      else
        if default != :_fetch_default_
          wrapped_object_or_self.fetch(key, default, &block)
        else
          wrapped_object_or_self.fetch(key, &block)
        end
      end
    end
    
    private
      
      ##
      # Gets the Hash or Array, whether it is a wrapped object (a
      # Dottie::Freckle) or this object (self).
      
      def wrapped_object_or_self
        if is_a?(Hash) || is_a?(Array)
          self
        elsif respond_to?(:wrapped_object)
          wrapped_object || self
        else
          self
        end
      end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dottie-0.0.1 lib/dottie/methods.rb