Sha256: 1404560af93fe438282abdfa6ae75196695f2855c98472443bca65a06034513a

Contents?: true

Size: 408 Bytes

Versions: 10

Compression:

Stored size: 408 Bytes

Contents

# Creates dynamic struct based on arguments

class CleanHash
  STRUCTS = {}

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

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

    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

10 entries across 10 versions & 1 rubygems

Version Path
clean-hash-0.5.10 ./lib/clean-hash/types/struct_type.rb
clean-hash-0.5.9 ./lib/clean-hash/types/struct_type.rb
clean-hash-0.5.8 ./lib/clean-hash/types/struct_type.rb
clean-hash-0.5.6 ./lib/clean-hash/types/struct_type.rb
clean-hash-0.5.5 ./lib/clean-hash/types/struct_type.rb
clean-hash-0.5.4 ./lib/clean-hash/types/struct_type.rb
clean-hash-0.5.3 ./lib/clean-hash/types/struct_type.rb
clean-hash-0.5.2 ./lib/clean-hash/types/struct_type.rb
clean-hash-0.5.1 ./lib/clean-hash/types/struct_type.rb
clean-hash-0.5.0 ./lib/clean-hash/types/struct_type.rb