choices_selector.anubis 3.21 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 06/04/2016
 * Time: 22:54
 * © Calexium 
 */

read hayamiki_lib/types/hayamiki.anubis
read tools/basis.anubis
read tools/streams.anubis
read system/string.anubis
read fields.anubis
read columns.anubis
read html.anubis

define Maybe(One) 
  _generate_choices_selector
  (
    Stream                stream,
    String                table_name,
    String                column_name,
    List(HK_Text_Choice)  choices
  )=
  //generate the type of the table that contain all components 
  if write_string(stream, 
    "\n\n"+
    "public define List((List(CoreAttrs), WebArgValue, String))\n"+
    "  get_choices_selector_"+table_name+"_"+column_name+"\n"+
    "  (\n"+
    "    SQLite3DataBase  db,\n"+
    "  )=\n"+
    "  [\n"+
    concat(map((HK_Text_Choice hk_txt_choice) |-> 
      since hk_txt_choice is hk_text_choice(value, visible),
      "    ([], wav(\""+value+"\"), \""+visible+"\"),\n", choices))+
    "  ]."
     ) is                                                           //end of the function
  {
    failure     then  println("can't write generate_choices_selector_"+table_name+"_"+column_name); failure,
    success(_)  then  success(unique)
  }.  


define Maybe(One) 
  _generate_choices_int_selector
  (
    Stream                stream,
    String                table_name,
    String                column_name,
    List(HK_Int_Choice)   choices
  )=
  //generate the type of the table that contain all components 
  if write_string(stream, 
    "\n\n"+
    "public define List((List(CoreAttrs), WebArgValue, String))\n"+
    "  get_choices_selector_"+table_name+"_"+column_name+"\n"+
    "  (\n"+
    "    SQLite3DataBase  db,\n"+
    "  )=\n"+
    "  [\n"+
    concat(map((HK_Int_Choice hk_int_choice) |-> 
      since hk_int_choice is hk_int_choice(value, visible),
      "    ([], wav(\""+to_String(value)+"\"), \""+visible+"\"),\n", choices))+
    "  ]."
     ) is                                                           //end of the function
  {
    failure     then  println("can't write generate_choices_selector_"+table_name+"_"+column_name); failure,
    success(_)  then  success(unique)
  }.  
  
public define Maybe(One)
  generate_choices_selector
  (
    Stream    stream,
    HK_Table  table
  )=
  since table is hk_table(table_name, short_name, model, _),
  since model is hk_model( columns, show_string),
  
  map_forget((HK_Model_Column column) |->
    since column is hk_column(column_name, field_type, _, _),
    if field_type is char_field(choices, _) then
      if choices is 
      {
        no_choice                   then success(unique),
        text_choices(choices_list)  then
          _generate_choices_selector(stream, table_name, column_name, choices_list)
      }
    else if field_type is integer_field(choices) then
      if choices is 
      {
        no_choice                  then success(unique),
        int_choices(choices_list)  then
          _generate_choices_int_selector(stream, table_name, column_name, choices_list)
      }
    else
      success(unique), columns);
  success(unique).
      
    
  with type_name = to_upper(name, 1), //make the first character to upper case to able to be a Anubis Type.