Skip to content

Commit

Permalink
initial model code. Person find 'id = 1' totally works.
Browse files Browse the repository at this point in the history
  • Loading branch information
jneen committed Mar 20, 2011
1 parent 35d21e3 commit 0173ee3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/balls.sh
Expand Up @@ -13,10 +13,9 @@
[[ -z "$BALLS_VIEWS" ]] && BALLS_VIEWS=$BALLS_ROOT/views
[[ -z "$BALLS_ACTIONS" ]] && BALLS_ACTIONS=$BALLS_ROOT/actions


. $BALLS_LIB/util.sh
. $BALLS_LIB/http.sh
. $BALLS_LIB/router.sh
. $BALLS_LIB/server.sh
. $BALLS_LIB/view.sh

. $BALLS_LIB/model.sh
37 changes: 37 additions & 0 deletions lib/model.sh
@@ -0,0 +1,37 @@
#!/bin/bash

[[ -z "$BALLS_DB_CREDENTIALS" ]] && BALLS_DB_CREDENTIALS=''
[[ -z "$BALLS_DB" ]] && BALLS_DB='balls'

balls::model::impl() {
local model="$1"; shift

local mode="$1"; shift

if exists "$model.$mode"; then
"$model.$mode" "$@"
elif exists "balls::model.$mode"; then
"balls::model.$mode" "$@"
else
stderr "oh no! couldn't find \`$model.$mode\`."
fi
}

balls::model() {
alias "$1"="balls::model::impl $1"
}

balls::model.find() {
mysql $BALLS_DB_CREDENTIALS "$BALLS_DB" -e "SELECT * from $(balls::model.table_name) WHERE $@" | cat
}

balls::model.table_name() {
local table_name_var="${model}_table_name"
if [[ -n "$1" ]]; then
export "$table_name_var"="$1"
elif [[ -n "${!table_name_var}" ]]; then
echo "${!table_name_var}"
else
echo "$model" | tr '[A-Z]' '[a-z]'
fi
}

0 comments on commit 0173ee3

Please sign in to comment.