Sha256: 8a604ef98d56d85d65673bf14eb5aa33301aa8ce9ddbd9702b4106e02fcb30b9

Contents?: true

Size: 946 Bytes

Versions: 29

Compression:

Stored size: 946 Bytes

Contents

class HashObject
  def initialize hash_obj, no_exception_on_missing_key = false
    @hash_obj = hash_obj
    @no_exception_on_missing_key = no_exception_on_missing_key
  end    
  
  def [] key
    @hash_obj[key]
  end
  
  def keys
    return @hash_obj.keys
  end
  
  def merge hash
    HashObject.new(@hash_obj.merge( hash ))
  end
  
  def merge! hash
    @hash_obj.merge!( hash )
    self
  end

  def method_missing method, *args
    key = method.to_s
    if @hash_obj.keys.include? key
      obj = @hash_obj[key]
      obj = HashObject.new(obj) if obj.is_a? Hash
      return obj
    elsif @hash_obj.keys.include? key.to_sym
      obj = @hash_obj[key.to_sym]
      obj = HashObject.new(obj) if obj.is_a? Hash
      return obj
    elsif matches = key.match( /(\w*)=/ )
      key = matches[1].to_sym
      @hash_obj[key]=args.first
    else
      raise "No field in Hash object: #{key}" unless @no_exception_on_missing_key
    end
  end
end

Version data entries

29 entries across 29 versions & 2 rubygems

Version Path
wdd-ruby-ext-0.6.3 lib/wdd-ruby-ext/utils/hash_object.rb
wdd-ruby-ext-0.6.2 lib/wdd-ruby-ext/utils/hash_object.rb
wdd-ruby-ext-0.6.0 lib/wdd-ruby-ext/utils/hash_object.rb
wdd-ruby-ext-0.5.1 lib/wdd-ruby-ext/utils/hash_object.rb
wdd-ruby-ext-0.5.0 lib/wdd-ruby-ext/utils/hash_object.rb
baby-bro-0.0.17 lib/baby-bro/hash_object.rb
baby-bro-0.0.16 lib/baby-bro/hash_object.rb
wdd-ruby-ext-0.4.4 lib/wdd-ruby-ext/utils/hash_object.rb
wdd-ruby-ext-0.4.3 lib/wdd-ruby-ext/utils/hash_object.rb
wdd-ruby-ext-0.4.2 lib/wdd-ruby-ext/utils/hash_object.rb
baby-bro-0.0.15 lib/baby-bro/hash_object.rb
baby-bro-0.0.14 lib/baby-bro/hash_object.rb
wdd-ruby-ext-0.4.1 lib/wdd-ruby-ext/utils/hash_object.rb
wdd-ruby-ext-0.4.0 lib/wdd-ruby-ext/utils/hash_object.rb
wdd-ruby-ext-0.3.3 lib/wdd-ruby-ext/utils/hash_object.rb
wdd-ruby-ext-0.3.2 lib/wdd-ruby-ext/utils/hash_object.rb
wdd-ruby-ext-0.3.1 lib/wdd-ruby-ext/utils/hash_object.rb
wdd-ruby-ext-0.3.0 lib/wdd-ruby-ext/utils/hash_object.rb
wdd-ruby-ext-0.2.3 lib/wdd-ruby-ext/utils/hash_object.rb
wdd-ruby-ext-0.2.2 lib/wdd-ruby-ext/utils/hash_object.rb