Sha256: ed258aa0bb46cd89cdb02e76e9bc7ccebd1634d88f73d6c17650fb5eccf47646

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

class Hash
  
  # Methodize this hash
  def methodize!(recursive = true)
    self.extend(Methodize)
    if recursive
      self.values.each do |val|
        case val
        when Hash
          val.methodize!(recursive)
        when Array
          val.methodize_hashes!(recursive)
        end
      end
    end
  end
  
  # Makes a fully recursive merge
  def noe_merge(right)
    self.merge(right) do |key,oldval,newval|
      if oldval.nil? or newval.nil?
        newval
      elsif oldval.class != newval.class
        raise Noe::Error, "Conflict on #{key} has to be resolved manually, sorry.\n"\
                          "#{oldval} (#{oldval.class}) vs. #{newval} (#{newval.class})"
      elsif oldval.respond_to?(:noe_merge)
        oldval.noe_merge(newval)
      else
        newval
      end
    end
  end
  
  module Methodize
  
    # Allows using hash.key as a synonym for hash[:key] and
    # hash['key']
    def method_missing(name, *args, &block)
      if args.empty? and block.nil?
        self[name] || self[name.to_s]
      else
        super(name, *args, &block)
      end
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
noe-1.2.0 ./lib/noe/ext/hash.rb