Sha256: 63533905b2f7a87fcc6d0f66a63c04af15df501c67cf147ed50687fd2fdf513e

Contents?: true

Size: 419 Bytes

Versions: 2

Compression:

Stored size: 419 Bytes

Contents

# Creates dynamic struct based on arguments

class CleanHash
  CH_STRUCTS ||= {}

  def self.create_struct hash
    raise ArgumentError.new('Not a hash') unless hash.is_a?(Hash)

    name = 'DynStruct_' + hash.keys.join('_')
    CH_STRUCTS[name] ||= ::Struct.new(name, *hash.keys.sort)

    CH_STRUCTS[name].new.tap do |o|
      hash.each do |k, v|
        o.send('%s=' % k, v) unless v.nil?
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
clean-hash-0.6.0 ./lib/clean-hash/types/struct_type.rb
clean-hash-0.5.11 ./lib/clean-hash/types/struct_type.rb