Sha256: f34f593d0b8c3a742869d5f315860776edb2c8aa4f16ebebb742d6539245153c

Contents?: true

Size: 1.48 KB

Versions: 15

Compression:

Stored size: 1.48 KB

Contents

#--
# Ruby Whois
#
# An intelligent pure Ruby WHOIS client and parser.
#
# Copyright (c) 2009-2014 Simone Carletti <weppos@weppos.net>
#++


require 'ostruct'


# SuperStruct is an enhanced version of the Ruby Standard library {Struct}.
#
# 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.
  #
  # @overload initialize({ Symbol => Object })
  #   Initializes the object with a key/value hash.
  #   @param [{ Symbol => Object }] values
  #   @return [SuperStruct]
  # @overload initialize([ value1, value1, ... ])
  #   Initializes the object with given values.
  #   @param [Array] values
  #   @return [SuperStruct]
  # @overload initialize(value1, value1, ...)
  #   Initializes the object with given values.
  #   @return [SuperStruct]
  #
  # @yield  self
  #
  # @example
  #   attributes = { :foo => 1, :bar => "baz" }
  #   Struct.new(attributes)
  #   # => #<Struct foo=1, bar="baz">
  #
  def initialize(*args)
    if args.first.is_a? Hash
      initialize_with_hash(args.first)
    elsif args.size == 0
      super
    else
      raise ArgumentError
    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

15 entries across 15 versions & 1 rubygems

Version Path
whois-3.6.2 lib/whois/record/super_struct.rb
whois-3.6.1 lib/whois/record/super_struct.rb
whois-3.6.0 lib/whois/record/super_struct.rb
whois-3.5.9 lib/whois/record/super_struct.rb
whois-3.5.8 lib/whois/record/super_struct.rb
whois-3.5.7 lib/whois/record/super_struct.rb
whois-3.5.6 lib/whois/record/super_struct.rb
whois-3.5.5 lib/whois/record/super_struct.rb
whois-3.5.4 lib/whois/record/super_struct.rb
whois-3.5.3 lib/whois/record/super_struct.rb
whois-3.5.2 lib/whois/record/super_struct.rb
whois-3.5.1 lib/whois/record/super_struct.rb
whois-3.5.0 lib/whois/record/super_struct.rb
whois-3.4.5 lib/whois/record/super_struct.rb
whois-3.4.4 lib/whois/record/super_struct.rb