Browse Source

Further improve rc file methods

 + Add method to adapt easier code from the other modules. These method
 need to be simplified/removed/renamed in the future.
 + Entries become class to create/change them from external code.
Leo 8 years ago
parent
commit
fce36ff34b
2 changed files with 37 additions and 20 deletions
  1. 26 15
      src/rc.ml
  2. 11 5
      src/rc.mli

+ 26 - 15
src/rc.ml

@@ -136,19 +136,30 @@ let entry_of_object entry =
 
 (* Now transformed objects *)
 
-(* Create class for tags to extend it easier *)
+(* Tags may be extend by plugins *)
 class tag name arg = object
   val name : string = name
   val arguments : string = arg
 end;;
 
+class entry ?(tags=[]) command = object
+  val command : string = command
+  val tags : tag list = tags
+
+  method command = command
+  method tags = tags
+
+  method change_command new_command = {< command = new_command >}
+  method change_tags new_tags = {< tags = new_tags >}
+end;;
+
 (* Types of objects, exposed *)
-type entry = < command : string; tags : tag list >;;
 type setting = < key : string; value : string >;;
 type t = <
   add_common_tags : tag list -> t;
   add_entries : entry list -> t;
   add_settings : setting list -> t;
+  change_entries : entry list -> t;
   change_entry : int -> f:(entry -> entry) -> t;
   change_name : string -> t;
   common_tags : tag list;
@@ -177,12 +188,9 @@ let object_of_tag tag = failwith "Not implemented"
 let object_of_tag_list =
   List.map ~f:object_of_tag
 ;;
-let object_of_entry object_common_tags entry = object
-  val command = entry.command;
-  val tags = (object_of_tag_list entry.tags) @ object_common_tags
-  method command = command
-  method tags = tags
-end;;
+let object_of_entry object_common_tags entry =
+  new entry ~tags:((object_of_tag_list entry.tags) @ object_common_tags) entry.command
+;;
 let object_of_setting setting = failwith "Not implemented";;
 
 let init ?(rc=(!Const.rc_file)) () =
@@ -197,6 +205,9 @@ let init ?(rc=(!Const.rc_file)) () =
     method common_tags = common_tags
     method settings = settings
 
+    (* Change entry list *)
+    method change_entries new_entries = {< entries = new_entries >}
+
     (* Get entry number n *)
     method entry ~n = List.nth entries n
 
@@ -253,16 +264,16 @@ let init ?(rc=(!Const.rc_file)) () =
     initializer self#write
   end;;
 
-let create_entry ~cmd ?(tags=[]) = object
-    val command = cmd
-    val tags = tags
-    method command = command
-    method tags = tags
-end;;
-let create_tag = failwith "Not implemented"
 let create_setting = failwith "Not implemented"
 (*
    (* Tools to create new tag *)
    class enpty_tag = object
    val
 *)
+
+let change_setting = failwith "Not implemented"
+
+(* Empty entry *)
+let empty_entry () =
+  new entry ~tags:[] ""
+;;

+ 11 - 5
src/rc.mli

@@ -36,8 +36,6 @@
 
 open Core.Std;;
 
-(* TODO Add all but basic_template, create_basic, get_basic, rc_header *)
-
 module Test :
   sig
     type basic_rc_t
@@ -50,12 +48,21 @@ val welcome_msg : string
 (* TODO Improve documentation *)
 class tag :
   string -> string -> object val arguments : string val name : string end
-type entry = < command : string; tags : tag list >
+class entry : ?tags:tag list -> string ->
+object ('a)
+  val command : string
+  val tags : tag list
+  method command : string
+  method tags : tag list
+  method change_command : string -> 'a
+  method change_tags : tag list -> 'a
+end
 type setting = < key : string; value : string >;;
 type t = <
   entries : entry list;
   common_tags : tag list;
   settings : setting list;
+  change_entries : entry list -> t;
   add_common_tags : tag list -> t;
   add_entries : entry list -> t;
   add_settings : setting list -> t;
@@ -77,5 +84,4 @@ type t = <
 
 val init : ?rc:string lazy_t -> unit -> t
 
-(* Create corresponding object *)
-val create_entry : cmd:string -> ?tags:tag list -> entry
+val empty_entry : unit -> entry