SephirothSpawn
Sponsor
Hash Struct
Version: 0.1
Introduction
This script is purely a scripting tool. It works similar to the Struct class, but now when you create your struct object class, you can define defaults to your attributes. When you create your objects, you only have to pass attributes you wish to change. It is missing methods from the original Struct class, but I will be adding those in the future.
Script
Instructions
Please read the script heading.
Terms and Conditions
Free for non-commercial and commercial use.
Author's Notes
If you need any help using this, just ask.
Version: 0.1
Introduction
This script is purely a scripting tool. It works similar to the Struct class, but now when you create your struct object class, you can define defaults to your attributes. When you create your objects, you only have to pass attributes you wish to change. It is missing methods from the original Struct class, but I will be adding those in the future.
Script
Code:
#==============================================================================
# ** Hash_Struct
#------------------------------------------------------------------------------
# SephirothSpawn
# Version 0.1
# 2008-10-14
#------------------------------------------------------------------------------
# This class imitates the Struct class however, it allows you to create a
# struct class with defaults so when creating a struct object, all attributes
# do not need to be passed.
#------------------------------------------------------------------------------
# Examples:
#
# This creates a new class "Test_Class" with 2 attributes: test1, test2
# - Hash_Struct.new('Test_Class', {'test1' => 'a', 'test2' => 'b'})
#
# This creates a Test_Class object, passing no arguments
# - object1 = Hash_Struct::Test_Class.new
#
# Now lets inspect
# - p object1 => #<Hash_Struct::TestClass:0x3e7d0e8 @test2="b", @test1="a">
#
# This creates a Test_Class object, passing 500 as the test1 value
# - object2 = Hash_Struct::Test_Class.new('test1' => 500)
#
# Now lets inspect
# - p object2 => #<Hash_Struct::TestClass:0x3e7d0e8 @test2="b", @test1=500>
#==============================================================================
class Hash_Struct
#--------------------------------------------------------------------------
# * Object Initialization
#--------------------------------------------------------------------------
def initialize(class_name, defaults_hash = {})
# Start with class name
s = "class #{class_name};"
# Save defaults hash as class variable
s += " @@defaults_hash = #{string(defaults_hash)};"
# Create accessor methods
defaults_hash.keys.each do |key|
s += " attr_accessor :#{key};"
end
# Object Initialization
s += ' def initialize(hash = {});'
# Passes through defaults
s += ' @@defaults_hash.each do |key, value|;'
# If value is defined
s += ' if hash.has_key?(key);'
# Define value
s += ' eval ("@#{key} = hash[key]");'
# If value not defined
s += ' else;'
# Set to default
s += ' eval ("@#{key} = value");'
s += ' end;'
s += ' end;'
s += ' end;'
s += 'end;'
eval s
end
#--------------------------------------------------------------------------
# * String - Returns object in evaluatable format
#--------------------------------------------------------------------------
def string(object)
case object
when Array
object.collect! {|x| string(x)}
return "[#{object.join(',')}]"
when Hash
str = '{'
object.each do |key, value|
str += "#{string(key)} => #{string(value)},"
end
str += '}'
return str
when String
return "'#{object}'"
when Symbol
return ":#{object}"
else
return object.to_s
end
end
end
Instructions
Please read the script heading.
Terms and Conditions
Free for non-commercial and commercial use.
Author's Notes
If you need any help using this, just ask.