<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <html><head> <title>Class: P4Record</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link rel=StyleSheet href=".././rdoc-style.css" type="text/css" media="screen" /> <script type="text/javascript" language="JavaScript"> <!-- function popCode(url) { window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400") } //--> </script> </head> <body bgcolor="white"> <table summary="Information on class" width="100%" border="0" cellspacing="0"> <tr class="title-row"> <td class="big-title-font"> <sup><font color="aqua">Class</font></sup> P4Record </td> <td align="right"> <table summary="layout" cellspacing="0" cellpadding="2"> <tr valign="top"> <td class="small-title-font">In:</td> <td class="small-title-font"> <a href="../files/P4table_rb.html" class="aqua"> P4table.rb </a> <br /> </td> </tr> <tr> <td class="small-title-font">Parent:</td> <td class="small-title-font"> Object </td> </tr> </table> </td> </tr> </table> <!-- banner header --> <div class="description"><p> Main record manipulation class. Handles the loading and saving of records as both jobs and files. </p> </div> <table summary="Methods" cellpadding="5" width="100%"> <tr><td class="tablesubtitle">Methods</td></tr> </table> <div class="name-list"> <a href="#M000045">abandon</a> <a href="#M000043">add_file</a> <a href="#M000035">create</a> <a href="#M000048">delete</a> <a href="#M000042">each_file</a> <a href="#M000039">exists=</a> <a href="#M000038">exists?</a> <a href="#M000036">exists?</a> <a href="#M000046">file_list</a> <a href="#M000040">get_file</a> <a href="#M000033">load</a> <a href="#M000041">metafile</a> <a href="#M000050">method_missing</a> <a href="#M000037">new</a> <a href="#M000049">obliterate</a> <a href="#M000034">query</a> <a href="#M000044">rm_file</a> <a href="#M000047">save</a> </div> <table summary="Attributes" cellpadding="5" width="100%"> <tr><td class="tablesubtitle">Attributes</td></tr> </table> <table summary="Attribute details" cellspacing="5"> <tr valign="top"> <td class="attr-name">:files</td> <td align="center" class="attr-rw"> [R] </td> <td></td> </tr> <tr valign="top"> <td class="attr-name">:id</td> <td align="center" class="attr-rw"> [RW] </td> <td></td> </tr> <tr valign="top"> <td class="attr-name">:meta</td> <td align="center" class="attr-rw"> [RW] </td> <td></td> </tr> <tr valign="top"> <td class="attr-name">:seq</td> <td align="center" class="attr-rw"> [RW] </td> <td></td> </tr> <tr valign="top"> <td class="attr-name">:spec</td> <td align="center" class="attr-rw"> [RW] </td> <td></td> </tr> <tr valign="top"> <td class="attr-name">:table</td> <td align="center" class="attr-rw"> [RW] </td> <td></td> </tr> </table> <table summary="Method list" cellpadding="5" width="100%"> <tr><td class="tablesubtitle">Public Class methods</td></tr> </table> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000033"></a> <b>load</b>( id ) </td></tr> </table> <div class="description"> <p> Load a record from its associated job. Use to load existing records rather than constructing new ones. Raises a RuntimeError if the record does not exist. </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 427</span> <span class="kw">def</span> P4Record.load( id ) <span class="kw">if</span> ( ! P4Record.exists?( id ) ) raise( RuntimeError, "Record #{id.to_s} does not exist", caller ) <span class="kw">end</span> rec = P4Record.new( id ) rec.spec = P4Global.tagged.fetch_job( id.to_s ) <span class="kw">if</span> rec.spec.has_key?( <span class="str">"files"</span> ) rec.spec[ <span class="str">"files"</span> ].each <span class="kw">do</span> |file| name = file.sub( <span class="str">".*/"</span>, <span class="str">""</span> ).chomp f = P4RecFile.new( name, id ) f.exists = <span class="kw">true</span> f.depot_path = file.chomp rec.files[ name ] = f <span class="kw">end</span> <span class="kw">end</span> rec.exists = <span class="kw">true</span> <span class="kw">return</span> rec <span class="kw">end</span> </pre> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000034"></a> <b>query</b>( table, expr ) </td></tr> </table> <div class="description"> <p> Load all records matching a query expression. Uses a dynamically built "p4 jobs -e" expression to identify the matching records and then loads each record. Returns an array of <a href="P4Record.html">P4Record</a> objects. </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 454</span> <span class="kw">def</span> P4Record.query( table, expr ) expr = "job=#{table} & ( " + expr + <span class="str">" )"</span> P4Global.tagged.run_jobs( <span class="str">"-e"</span>, expr ).collect <span class="kw">do</span> |job| rec = P4Record.new( P4RecId.new_from_job( job[ <span class="str">"job"</span> ] ) ) rec.spec = job rec.spec[ <span class="str">"files"</span> ].each <span class="kw">do</span> |file| name = file.sub( <span class="str">".*/"</span>, <span class="str">""</span> ).chomp f = P4RecFile.new( name, id ) f.exists = <span class="kw">true</span> f.depot_path = file rec.files[ name ] = f <span class="kw">end</span> rec.exists = <span class="kw">true</span> rec <span class="kw">end</span> <span class="kw">end</span> </pre> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000035"></a> <b>create</b>( table ) </td></tr> </table> <div class="description"> <p> Create a new record in the given table. Returns the skeletal record populated with the default values from the jobspec for editing. </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 477</span> <span class="kw">def</span> P4Record.create( table ) id = P4RecId.next( table ) rec = P4Record.new( id ) rec.spec = P4Global.tagged.fetch_job( id.to_s ) <span class="kw">return</span> rec <span class="kw">end</span> </pre> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000036"></a> <b>exists?</b>( id ) </td></tr> </table> <div class="description"> <p> Test for record existence </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 487</span> <span class="kw">def</span> P4Record.exists?( id ) jobs = P4Global.tagged.run_jobs( <span class="str">"-e"</span>, <span class="str">"job="</span> + id.to_s ) <span class="kw">return</span> jobs.length > 0 <span class="kw">end</span> </pre> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000037"></a> <b>new</b>( id ) </td></tr> </table> <div class="description"> <p> Constructor: DON'T USE NEW DIRECTLY, CALL create/load. </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 495</span> <span class="kw">def</span> initialize( id ) @id = id @table = id.table @seq = id.seq @spec = <span class="kw">nil</span> @exists = <span class="kw">false</span> @files = Hash.new @files[ <span class="str">"meta"</span> ] = P4RecFile.new( <span class="str">"meta"</span>, @id ) <span class="kw">end</span> </pre> <table summary="Method list" cellpadding="5" width="100%"> <tr><td class="tablesubtitle">Public Instance methods</td></tr> </table> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000038"></a> <b>exists?</b>() </td></tr> </table> <div class="description"> <p> Test for record existence </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 512</span> <span class="kw">def</span> exists? <span class="kw">return</span> @exists <span class="kw">end</span> </pre> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000039"></a> <b>exists=</b>( bool ) </td></tr> </table> <div class="description"> <p> Explicitly set record existence (or otherwise) </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 519</span> <span class="kw">def</span> exists=( bool ) @exists = bool <span class="kw">end</span> </pre> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000040"></a> <b>get_file</b>( name ) </td></tr> </table> <div class="description"> <p> Get a file handle by name (not the depot path). Returns a <a href="P4RecFile.html">P4RecFile</a> object. </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 528</span> <span class="kw">def</span> get_file( name ) <span class="kw">return</span> @files[ name ] <span class="kw">if</span> @files.has_key?( name ) raise( RuntimeError, "Record contains no file called #{name}", caller ) <span class="kw">end</span> </pre> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000041"></a> <b>metafile</b>() </td></tr> </table> <div class="description"> <p> Get the meta file specifically. Just shorthand. </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 536</span> <span class="kw">def</span> metafile get_file( <span class="str">"meta"</span> ) <span class="kw">end</span> </pre> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000042"></a> <b>each_file</b>() {| f | ...} </td></tr> </table> <div class="description"> <p> Iterate over the files in the record </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 543</span> <span class="kw">def</span> each_file @files.each_value { |f| <span class="kw">yield</span>( f ) } <span class="kw">end</span> </pre> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000043"></a> <b>add_file</b>( name ) </td></tr> </table> <div class="description"> <p> Add a new file attachment to this record </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 550</span> <span class="kw">def</span> add_file( name ) <span class="kw">if</span> ( name == <span class="str">"meta"</span> ) raise( RuntimeError, <span class="str">"The meta file already exists."</span>, caller ) <span class="kw">end</span> nfile = P4RecFile.new( name, @id ) nfile.add @files[ <span class="str">"name"</span> ] = nfile <span class="kw">end</span> </pre> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000044"></a> <b>rm_file</b>( file ) </td></tr> </table> <div class="description"> <p> Remove a file attachment </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 563</span> <span class="kw">def</span> rm_file( file ) <span class="kw">if</span> ( file == <span class="str">"meta"</span> ) raise( RuntimeError, <span class="str">"You can't delete the meta file"</span>, caller ) <span class="kw">end</span> f = get_file( file ) f.delete @files.delete( file ) <span class="kw">end</span> </pre> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000045"></a> <b>abandon</b>() </td></tr> </table> <div class="description"> <p> Abandon all edits to this record. Use with care: it reverts the files that are open for add/edit/delete etc. but it doesn't reload the record from the job - it may be a new record. You should either discard the record or reload it yourself after calling abandon(). </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 580</span> <span class="kw">def</span> abandon p4 = P4Global.tagged each_file <span class="kw">do</span> |f| fs = p4.run_fstat( f.ws_path ) <span class="kw">if</span> ( fs && fs[ <span class="str">"action"</span> ] == <span class="str">"add"</span> ) File.unlink( f.ws_path ) <span class="kw">end</span> p4.run_revert( f.depot_path ) <span class="kw">end</span> <span class="kw">end</span> </pre> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000046"></a> <b>file_list</b>() </td></tr> </table> <div class="description"> <p> Get the list of files attached to this record in depot syntax </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 595</span> <span class="kw">def</span> file_list @files.values.collect <span class="kw">do</span> |f| f.depot_path <span class="kw">end</span>.compact.join( <span class="str">"\n"</span> ) <span class="kw">end</span> </pre> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000047"></a> <b>save</b>( desc ) </td></tr> </table> <div class="description"> <p> Update the record. Updates the job and then archives the job into the meta file and saves any attached files. Provide the description you'd like to see attached to the change </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 608</span> <span class="kw">def</span> save( desc ) <span class="cmt"># First rewrite the files list in case it's been modified</span> @spec[ <span class="str">"files"</span> ] = file_list <span class="cmt"># Next update the job with the values in the spec</span> P4Global.tagged.save_job( @spec ) <span class="cmt"># Now update the metafile with the job -o output</span> <span class="kw">if</span> ( exists? ) metafile.edit <span class="kw">else</span> metafile.add <span class="kw">end</span> metafile.mkdir metafile.write( P4Global.plain.fetch_job( @id.to_s ) ) <span class="cmt"># Now submit</span> change = P4Global.tagged.fetch_change change[ <span class="str">"Description"</span> ] = desc P4Global.tagged.submit_spec( change ) @exists = <span class="kw">true</span> <span class="kw">end</span> </pre> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000048"></a> <b>delete</b>( desc ) </td></tr> </table> <div class="description"> <p> Delete a record. Deletes the files and the job. </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 636</span> <span class="kw">def</span> delete( desc ) <span class="kw">if</span> ( ! exists? ) raise( RuntimeError, <span class="str">"Can't delete. Record doesn't exist."</span>, caller ) <span class="kw">end</span> @files.values.each { |f| f.delete } change = P4Global.tagged.fetch_change change[ <span class="str">"Description"</span> ] = desc P4Global.tagged.submit_spec( change ) P4Global.tagged.run_job( <span class="str">"-d"</span>, @spec[ <span class="str">"job"</span> ] ) @files = Hash.new @spec[ <span class="str">"files"</span> ] = Array.new @exists = <span class="kw">false</span> <span class="kw">end</span> </pre> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000049"></a> <b>obliterate</b>() </td></tr> </table> <div class="description"> <p> Obliterate a record. Does exactly what it says on the tin. </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 655</span> <span class="kw">def</span> obliterate <span class="kw">if</span> ( ! exists? ) raise( RuntimeError, <span class="str">"Can't oblit. Record doesn't exist."</span>, caller ) <span class="kw">end</span> args = @files.values.collect { |f| f.depot_path } P4Global.tagged.exception_level = 1 P4Global.tagged.run_sync( args.collect { |a| a += "#none" } ) P4Global.tagged.exception_level = 2 P4Global.tagged.run_obliterate( <span class="str">"-y"</span>, args ) P4Global.tagged.run_job( <span class="str">"-d"</span>, @spec[ <span class="str">"job"</span> ] ) @spec[ <span class="str">"files"</span> ] = Array.new @files = Hash.new @exists = <span class="kw">false</span> <span class="kw">end</span> </pre> <table summary="method" width="100%" cellspacing="0" cellpadding="5" border="0"> <tr><td class="methodtitle"> <a name="M000050"></a> <b>method_missing</b>( meth, *args ) </td></tr> </table> <div class="description"> <p> Allow direct access to the fields in the jobspec by making them virtual method names of the form _<field>() </p> </div> <pre class="source"> <span class="cmt"># File P4table.rb, line 675</span> <span class="kw">def</span> method_missing( meth, *args ) meth = meth.to_s raise <span class="kw">if</span> ( meth[0..0] != <span class="str">"_"</span> ) meth = meth[ 1..-1 ] <span class="kw">if</span> ( meth =~ <span class="re">/^(.*)=$/</span> ) meth = $1 @spec[ meth ] = args.shift <span class="kw">elsif</span> ( args.length == 0 && @spec.has_key?( meth ) ) @spec[ meth ] <span class="kw">else</span> <span class="str">""</span> <span class="kw">end</span> <span class="kw">end</span> </pre> </body>
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 4677 | Tony Smith |
Move P4table.rb to the P4Ruby library and rename it to p4table.rb instead. There's an update coming so this is just the move. |
||
#1 | 3093 | Tony Smith |
Move docs for P4Table in preparation for upcoming docs for specsaver.rb |
||
//guest/tony_smith/perforce/utils/doc/classes/P4Record.html | |||||
#1 | 2396 | Tony Smith |
Add RDoc documentation for P4Table.rb. Very pretty. |