Sha256: 3148b4fec1ef34d1b4815498273ca07e1c3aaf7b02d16963422c63aa8a9bbe17

Contents?: true

Size: 1.12 KB

Versions: 46

Compression:

Stored size: 1.12 KB

Contents

#
# = Ruby Whois
#
# An intelligent pure Ruby WHOIS client and parser.
#
#
# Category::    Net
# Package::     Whois
# Author::      Simone Carletti <weppos@weppos.net>
# License::     MIT License
#
#--
#
#++


require 'ostruct'


#
# = SuperStruct
#
# SuperStruct is an enhanced version of the Ruby Standar library <tt>Struct</tt>.
#
# Compared with the original version, it provides the following additional features:
# * ability to initialize an instance from Hash
# * ability to pass a block on creation
#
class SuperStruct < Struct

  # Overwrites the standard Struct initializer
  # to add the ability to create an instance from a Hash of parameters.
  #
  #   attributes = { :foo => 1, :bar => "baz" }
  #   Struct.new(attributes)
  #   # => #<Struct foo=1, bar="baz">
  #
  # If block is given, the block is called on self.
  #
  def initialize(*args, &block)
    if args.first.is_a? Hash
      initialize_with_hash(args.first)
    else
      super
    end
    yield(self) if block_given?
  end

  private

    def initialize_with_hash(attributes = {})
      attributes.each do |key, value|
        self[key] = value
      end
    end
  
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
whois-1.6.6 lib/whois/answer/super_struct.rb
whois-1.6.5 lib/whois/answer/super_struct.rb
whois-1.6.4 lib/whois/answer/super_struct.rb
whois-1.6.3 lib/whois/answer/super_struct.rb
whois-1.6.2 lib/whois/answer/super_struct.rb
whois-1.6.1 lib/whois/answer/super_struct.rb
whois-1.6.0 lib/whois/answer/super_struct.rb
whois-1.5.1 lib/whois/answer/super_struct.rb
whois-1.5.0 lib/whois/answer/super_struct.rb
whois-1.3.11 lib/whois/answer/super_struct.rb
whois-1.3.10 lib/whois/answer/super_struct.rb
whois-1.3.9 lib/whois/answer/super_struct.rb
whois-1.3.8 lib/whois/answer/super_struct.rb
whois-1.3.7 lib/whois/answer/super_struct.rb
whois-1.3.6 lib/whois/answer/super_struct.rb
whois-1.3.5 lib/whois/answer/super_struct.rb
whois-1.3.4 lib/whois/answer/super_struct.rb
whois-1.3.3 lib/whois/answer/super_struct.rb
whois-1.3.2 lib/whois/answer/super_struct.rb
whois-1.3.1 lib/whois/answer/super_struct.rb