Sha256: 5f202f5800e72582b8a0a77ef6c71022f644bf07b03a9fcf07c3e8d21f01ccfa

Contents?: true

Size: 1.59 KB

Versions: 35

Compression:

Stored size: 1.59 KB

Contents

require 'singleton'

class SafeHash < BlankSlate
  attr_reader :hash
  
  def initialize hash = {}
    reinitialize hash
  end
  
  def []= key, value
    value = SafeHash.new value if value.is_a? Hash
    @hash[key.to_s] = value
  end
  
  def include? key
    @hash.include? key.to_s
  end
  
  def [] key, *args
    key = key.to_s
    if key.last == '!'
      key = key[0..key.size-2]
      if (result = @hash[key]).eql? nil       
        raise "No key #{key}"
      else
        result
      end
    elsif key.last == '?'
      key = key[0..key.size-2]
      @hash.include? key
    elsif (result = @hash[key]).eql? nil
      if args.empty?
        SafeNil.instance
      else
        return *args
      end
    else
      result
    end
  end
  
  def reinitialize hash
    @hash = {}
    hash.each do |k, v|
      v = SafeHash.new v if v.is_a? Hash 
      @hash[k.to_s] = v
    end
    # @hash.freeze
  end
  
  def method_missing m, *args
    self[m, *args]
  end
  
  def to_yaml *args
    @hash.to_yaml *args
  end
  
  def inspect
    @hash.inspect
  end
  
  def to_h
    @hash
  end
  
  class SafeNil < BlankSlate
    include Singleton

    def [] key, *args
      if key.to_s.last == '!'
        raise "No key #{key}"
      elsif args.empty?
        SafeNil.instance
      else
        return *args
      end
    end

    def method_missing m, *args
      self[m, *args]
    end
    
    def include? key
      false
    end
    
    def to_b
      false
    end
    
    def to_yaml *args
      nil.to_yaml *args
    end
    
    def to_h
      {}
    end
    
    def inspect
      nil.inspect
    end
  end
end

Version data entries

35 entries across 35 versions & 2 rubygems

Version Path
crystal-ext-0.0.3 lib/rails_ext/micelaneous/safe_hash.rb
crystal-ext-0.0.2 lib/rails_ext/micelaneous/safe_hash.rb
crystal-ext-0.0.1 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.29 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.28 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.27 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.25 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.24 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.23 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.22 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.21 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.20 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.19 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.18 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.17 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.16 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.15 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.14 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.13 lib/rails_ext/micelaneous/safe_hash.rb
rails-ext-0.3.12 lib/rails_ext/micelaneous/safe_hash.rb