Created
July 21, 2015 16:51
-
-
Save jmatsu/83f438d0f1c6c9cbd93d to your computer and use it in GitHub Desktop.
Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function main() | |
{ | |
Object::class.new Test object1 | |
object1.test_echo | |
} | |
function Object::class.new() { | |
local class=$1 | |
local self=$2 | |
shift 2 | |
if type -t ${self}.initialize > /dev/null; then | |
"${self}.initialize" "$@" | |
fi | |
local m | |
for m in $(find_instance_methods "${class}::instance.*"); do | |
# delegate | |
m=${m#${class}::instance.} | |
eval "function ${self}.${m}() { | |
${class}::instance.${m} \"$self\" \"\$@\" | |
}" | |
done | |
} | |
function Test::instance.test_echo() { | |
echo "test" | |
} | |
function find_instance_methods() { | |
local pattern=$1 | |
local function_ line | |
typeset -F | while read line; do | |
function_=${line##* } | |
[[ "$function_" == ${pattern} ]] && echo $function_ | |
done | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment