updated the API some.
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 6 Jan 2006 16:15:57 +0000 (16:15 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 6 Jan 2006 16:15:57 +0000 (16:15 +0000)
now there is a generic insert method to handle scalars, hashes, arrays, and FM objects

git-svn-id: svn://svn.open-ils.org/ILS/trunk@2636 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Utils/ScriptRunner.pm
Open-ILS/src/perlmods/OpenILS/Utils/SpiderMonkey.pm

index 596c798..f4a8639 100644 (file)
@@ -23,19 +23,21 @@ sub new {
 }
 
 sub init {$logger->error("METHOD NOT DEFINED"); }
+
+# the script context
 sub context {$logger->error("METHOD NOT DEFINED"); }
-# generic insertion method
+
+# generic insertion method - should work on arrays, 
+# hashes, fieldmapper objects, and scalars
 sub insert {$logger->error("METHOD NOT DEFINED"); }
-# generic retrieval method
-sub retrieve {$logger->error("METHOD NOT DEFINED"); }
-sub insert_fm { $logger->error("METHOD NOT DEFINED"); }
-sub insert_hash { $logger->error("METHOD NOT DEFINED"); }
+
 # loads an external script
 sub load { $logger->error("METHOD NOT DEFINED"); }
 
 # Runs an external script.
 # @return 1 on success, 0 on failure
 sub run { $logger->error("METHOD NOT DEFINED"); }
+
 # load an external library
 sub load_lib { $logger->error("METHOD NOT DEFINED"); }
 
index 7839b1e..b70b0e3 100644 (file)
@@ -4,7 +4,6 @@ use OpenSRF::Utils::Logger qw(:logger);
 use OpenILS::Utils::ScriptRunner;
 use base 'OpenILS::Utils::ScriptRunner';
 use JavaScript::SpiderMonkey;
-use JSON;
 
 sub new {
        my ( $class, %params ) = @_;
@@ -73,35 +72,26 @@ sub _js_prop_name {
 
 sub insert {
        my( $self, $key, $val ) = @_;
-       my $str = JSON->perl2JSON($val);
-       warn "Inserting string: $str\n";
-       my $js = $self->context;
-       $js->object_by_path($key);
-       if( ! $js->eval("$key = JSON2js('$str')")) {
-               $logger->error("Error inserting value with key $key: $@");  
+
+       if (ref($val) =~ /^Fieldmapper/o) {
+               $self->insert_fm($key, $val);
+       } elsif (ref($val) and $val =~ /ARRAY/o) {
+               $self->insert_array($key, $val);
+       } elsif (ref($val) and $val =~ /HASH/o) {
+               $self->insert_hash($key, $val);
+       } elsif (!ref($val)) {
+               $self->context->property_by_path(
+                       $key, $val,
+                       sub { $val },
+                       sub { my( $k, $v ) = @_; $val = $v; }
+               );
+       } else {
                return 0;
        }
-       return 1;
-}
 
-sub retrieve {
-       my( $self, $key ) = @_;
-       my $val;
-       my $js = $self->context;
-
-       $js->object_by_path("obj");
-       $js->property_by_path("obj.out");
-
-       if( ! $js->eval("obj.out = js2JSON($key);")) {
-               $logger->error("Error retrieving value with $key: $@");  
-               return undef;
-       }
-       my $str = $js->property_get("obj.out");
-       warn "Retrieving [$key] string: $str\n";
-       return JSON->JSON2perl($str);
+       return 1;
 }
 
-
 sub insert_fm {
 
        my( $self, $key, $fm ) = @_;
@@ -110,19 +100,25 @@ sub insert_fm {
        my $o = $ctx->object_by_path($key);
        
        for my $f ( $fm->properties ) {
-               $ctx->property_by_path("$key.$f", $fm->$f(),
-
-                       sub {
-                               my $k = _js_prop_name(shift());
-                               $fm->$k();
-                       }, 
-
-                       sub {
-                               my $k = _js_prop_name(shift());
-                               $fm->ischanged(1);
-                               $fm->$k(@_);
-                       }
-               );
+               my $val = $fm->$f();
+               if (ref $val) {
+                       $self->insert("$key.$f", $val);
+               } else {
+                       $ctx->property_by_path(
+                               "$key.$f",
+                               $val,
+                               sub {
+                                       my $k = _js_prop_name(shift());
+                                       $fm->$k();
+                               }, 
+
+                               sub {
+                                       my $k = _js_prop_name(shift());
+                                       $fm->ischanged(1);
+                                       $fm->$k(@_);
+                               }
+                       );
+               }
        }
 }
 
@@ -134,13 +130,39 @@ sub insert_hash {
        $ctx->object_by_path($key);
        
        for my $k ( keys %$hash ) {
-               $ctx->property_by_path(
-                       "$key.$k", $hash->{$k},
-                       sub { $hash->{_js_prop_name(shift())} },
-                       sub { 
-                               my( $key, $val ) = @_;
-                               $hash->{_js_prop_name($key)} = $val; }
-               );
+               my $v = $hash->{$k};
+               if (ref $v) {
+                       $self->insert("$key.$k", $v);
+               } else {
+                       $ctx->property_by_path(
+                               "$key.$k", $v,
+                               sub { $hash->{_js_prop_name(shift())} },
+                               sub { 
+                                       my( $key, $val ) = @_;
+                                       $hash->{_js_prop_name($key)} = $val; }
+                       );
+               }
+       }
+}
+
+sub insert_array {
+
+       my( $self, $key, $array ) = @_;
+       my $ctx = $self->context;
+       return undef unless ($ctx and $key and $array);
+
+       my $a = $ctx->array_by_path($key);
+       
+       my $ind = 0;
+       for my $v ( @$array ) {
+               if (ref $v) {
+                       my $elobj = $ctx->object_by_path('__tmp_arr_el');
+                       $self->insert('__tmp_arr_el', $v);
+                       $ctx->array_set_element_as_object( $a, $ind, $elobj );
+               } else {
+                       $ctx->array_set_element( $a, $ind, $v );
+               }
+               $ind++;
        }
 }