Sha256: 677b7d4eb72b3a8322df6bde5332dbfe1bcd7300bd43b7108b684245465e551f

Contents?: true

Size: 639 Bytes

Versions: 1

Compression:

Stored size: 639 Bytes

Contents

# Similar to Ruby's OpenStruct but uses hash as its parent class providing
# all of the typical Hash methods. Useful for storing settings on objects.
module BBLib
  class HashStruct < Hash

    protected

    def method_missing(method, *args, &block)
      if args.empty?
        define_singleton_method(method) do
          self[method]
        end
        self[method]
      elsif method.to_s.end_with?('=')
        define_singleton_method(method) do |arg|
          self[method[0..-2].to_sym] = arg
        end
        self[method[0..-2].to_sym] = args.first
      else
        super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bblib-0.4.1 lib/hash/hash_struct.rb