<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
Class: OpenModel
— Documentation by YARD 0.8.7.6
</title>
<link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
<script type="text/javascript" charset="utf-8">
hasFrames = window.top.frames.main ? true : false;
relpath = '';
framesUrl = "frames.html#!OpenModel.html";
</script>
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
</head>
<body>
<div id="header">
<div id="menu">
<a href="_index.html">Index (O)</a> »
<span class="title">OpenModel</span>
<div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
</div>
<div id="search">
<a class="full_list_link" id="class_list_link"
href="class_list.html">
Class List
</a>
<a class="full_list_link" id="method_list_link"
href="method_list.html">
Method List
</a>
<a class="full_list_link" id="file_list_link"
href="file_list.html">
File List
</a>
</div>
<div class="clear"></div>
</div>
<iframe id="search_frame"></iframe>
<div id="content"><h1>Class: OpenModel
</h1>
<dl class="box">
<dt class="r1">Inherits:</dt>
<dd class="r1">
<span class="inheritName">OpenStruct</span>
<ul class="fullTree">
<li>Object</li>
<li class="next">OpenStruct</li>
<li class="next">OpenModel</li>
</ul>
<a href="#" class="inheritanceTree">show all</a>
</dd>
<dt class="r2 last">Defined in:</dt>
<dd class="r2 last">lib/helix_web_services_client/open_model.rb</dd>
</dl>
<div class="clear"></div>
<h2>Overview</h2><div class="docstring">
<div class="discussion">
<p>A lot of our data coming out of the server has a couple of issues:</p>
<ul><li>
<p>The ID of an object might be referenced by different cases, or different
field names</p>
</li><li>
<p>Dates are sometimes as returned as timestamp strings (e.g., "1437153920")
and sometimes as rendered strings (e.g., "2015/07/17 10:36:38")</p>
</li></ul>
<p>This is intended to be mixed in with an OpenStruct class to let your code
handle conversion as needed.</p>
<p>You have a couple of rules:</p>
<ol><li>
<p>For [name], if a capitalized version exists we return that</p>
</li><li>
<p>For [name], if a camel case version of the name exists we return that</p>
</li><li>
<p>You can use an "[method]_as_time" to try to detect the format and return
it as a ruby Time instance.</p>
<ul><li>
<p>You must provide an 'offset' parameter here based on the serverTime
value from p4 info.</p>
</li></ul>
</li><li>
<p>You can use an "[<a href="name">name]or</a>" to fallback to a different
symbols.</p>
<p>rev_or_revision desc_or_description</p>
</li></ol>
<p>For example, say you have the Hash</p>
<pre class="code ruby"><code class="ruby"><span class='lbrace'>{</span>
<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>branch</span><span class='label_end'>':</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>my_branch</span><span class='tstring_end'>'</span></span><span class='comma'>,</span>
<span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Update</span><span class='label_end'>':</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>1437153920</span><span class='tstring_end'>"</span></span>
<span class='rbrace'>}</span>
</code></pre>
<p>Then your object should both respond to:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_branch'>branch</span><span class='period'>.</span><span class='id identifier rubyid_branch'>branch</span>
<span class='id identifier rubyid_branch'>branch</span><span class='period'>.</span><span class='id identifier rubyid_update_as_time'>update_as_time</span><span class='lparen'>(</span><span class='id identifier rubyid_offset'>offset</span><span class='rparen'>)</span>
</code></pre>
<p>This only affects “read” methods. Your code should probably only use the
Singular format for “write” methods, and rarely should it ever need to
create dates.</p>
</div>
</div>
<div class="tags">
</div>
<h2>
Class Method Summary
<small>(<a href="#" class="summary_toggle">collapse</a>)</small>
</h2>
<ul class="summary">
<li class="public ">
<span class="summary_signature">
<a href="#camelize-class_method" title="camelize (class method)">+ (Object) <strong>camelize</strong>(sym) </a>
</span>
<span class="summary_desc"><div class='inline'></div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#capitalize-class_method" title="capitalize (class method)">+ (Object) <strong>capitalize</strong>(sym) </a>
</span>
<span class="summary_desc"><div class='inline'></div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#date_str%3F-class_method" title="date_str? (class method)">+ (Boolean) <strong>date_str?</strong>(str) </a>
</span>
<span class="summary_desc"><div class='inline'></div></span>
</li>
</ul>
<h2>
Instance Method Summary
<small>(<a href="#" class="summary_toggle">collapse</a>)</small>
</h2>
<ul class="summary">
<li class="public ">
<span class="summary_signature">
<a href="#method_missing-instance_method" title="#method_missing (instance method)">- (Object) <strong>method_missing</strong>(sym, *args) </a>
</span>
<span class="summary_desc"><div class='inline'></div></span>
</li>
</ul>
<div id="method_missing_details" class="method_details_list">
<h2>Dynamic Method Handling</h2>
<p class="notice this">
This class handles dynamic methods through the <tt>method_missing</tt> method
</p>
<div class="method_details first">
<h3 class="signature first" id="method_missing-instance_method">
- (<tt>Object</tt>) <strong>method_missing</strong>(sym, *args)
</h3><table class="source_code">
<tr>
<td>
<pre class="lines">
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112</pre>
</td>
<td>
<pre class="code"><span class="info file"># File 'lib/helix_web_services_client/open_model.rb', line 63</span>
<span class='kw'>def</span> <span class='id identifier rubyid_method_missing'>method_missing</span><span class='lparen'>(</span><span class='id identifier rubyid_sym'>sym</span><span class='comma'>,</span> <span class='op'>*</span><span class='id identifier rubyid_args'>args</span><span class='rparen'>)</span>
<span class='id identifier rubyid_match_time'>match_time</span> <span class='op'>=</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>^(.*)_as_time$</span><span class='regexp_end'>/</span></span><span class='period'>.</span><span class='id identifier rubyid_match'>match</span><span class='lparen'>(</span><span class='id identifier rubyid_sym'>sym</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span><span class='rparen'>)</span>
<span class='kw'>if</span> <span class='id identifier rubyid_match_time'>match_time</span>
<span class='id identifier rubyid_val'>val</span> <span class='op'>=</span> <span class='id identifier rubyid_send'>send</span><span class='lparen'>(</span><span class='id identifier rubyid_match_time'>match_time</span><span class='lbracket'>[</span><span class='int'>1</span><span class='rbracket'>]</span><span class='rparen'>)</span>
<span class='kw'>if</span> <span class='id identifier rubyid_val'>val</span> <span class='op'>=~</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>\A[-+]?\d+\z</span><span class='regexp_end'>/</span></span>
<span class='kw'>return</span> <span class='const'>Time</span><span class='period'>.</span><span class='id identifier rubyid_at'>at</span><span class='lparen'>(</span><span class='id identifier rubyid_val'>val</span><span class='period'>.</span><span class='id identifier rubyid_to_i'>to_i</span><span class='rparen'>)</span>
<span class='kw'>elsif</span> <span class='const'>OpenModel</span><span class='period'>.</span><span class='id identifier rubyid_date_str?'>date_str?</span><span class='lparen'>(</span><span class='id identifier rubyid_val'>val</span><span class='rparen'>)</span> <span class='op'>&&</span> <span class='id identifier rubyid_args'>args</span><span class='period'>.</span><span class='id identifier rubyid_length'>length</span> <span class='op'>></span> <span class='int'>0</span>
<span class='id identifier rubyid_offset'>offset</span> <span class='op'>=</span> <span class='id identifier rubyid_args'>args</span><span class='period'>.</span><span class='id identifier rubyid_first'>first</span>
<span class='kw'>return</span> <span class='const'>DateTime</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>"</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_val'>val</span><span class='embexpr_end'>}</span><span class='tstring_content'> </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_offset'>offset</span><span class='embexpr_end'>}</span><span class='tstring_end'>"</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_time'>to_time</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_mname'>mname</span> <span class='op'>=</span> <span class='id identifier rubyid_sym'>sym</span><span class='period'>.</span><span class='id identifier rubyid_id2name'>id2name</span>
<span class='id identifier rubyid_is_assign'>is_assign</span> <span class='op'>=</span> <span class='id identifier rubyid_mname'>mname</span><span class='period'>.</span><span class='id identifier rubyid_chomp!'>chomp!</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>=</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
<span class='kw'>if</span> <span class='id identifier rubyid_mname'>mname</span><span class='period'>.</span><span class='id identifier rubyid_split'>split</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>_or_</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_length'>length</span> <span class='op'>></span> <span class='int'>1</span>
<span class='id identifier rubyid_syms'>syms</span> <span class='op'>=</span> <span class='id identifier rubyid_mname'>mname</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span><span class='period'>.</span><span class='id identifier rubyid_split'>split</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>_or_</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
<span class='id identifier rubyid_s'>s</span> <span class='op'>=</span> <span class='id identifier rubyid_syms'>syms</span><span class='period'>.</span><span class='id identifier rubyid_find'>find</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_s'>s</span><span class='op'>|</span> <span class='ivar'>@table</span><span class='period'>.</span><span class='id identifier rubyid_key?'>key?</span><span class='lparen'>(</span><span class='const'>OpenModel</span><span class='period'>.</span><span class='id identifier rubyid_capitalize'>capitalize</span><span class='lparen'>(</span><span class='id identifier rubyid_s'>s</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span><span class='rparen'>)</span> <span class='rbrace'>}</span>
<span class='kw'>if</span> <span class='id identifier rubyid_s'>s</span>
<span class='id identifier rubyid_mname'>mname</span> <span class='op'>=</span> <span class='id identifier rubyid_s'>s</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span>
<span class='kw'>else</span>
<span class='id identifier rubyid_s'>s</span> <span class='op'>=</span> <span class='id identifier rubyid_syms'>syms</span><span class='period'>.</span><span class='id identifier rubyid_find'>find</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_s'>s</span><span class='op'>|</span> <span class='ivar'>@table</span><span class='period'>.</span><span class='id identifier rubyid_key?'>key?</span><span class='lparen'>(</span><span class='const'>OpenModel</span><span class='period'>.</span><span class='id identifier rubyid_camelize'>camelize</span><span class='lparen'>(</span><span class='id identifier rubyid_s'>s</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span><span class='rparen'>)</span> <span class='rbrace'>}</span>
<span class='id identifier rubyid_mname'>mname</span> <span class='op'>=</span> <span class='id identifier rubyid_s'>s</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span> <span class='kw'>if</span> <span class='id identifier rubyid_s'>s</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='kw'>if</span> <span class='ivar'>@table</span><span class='period'>.</span><span class='id identifier rubyid_key?'>key?</span><span class='lparen'>(</span><span class='const'>OpenModel</span><span class='period'>.</span><span class='id identifier rubyid_capitalize'>capitalize</span><span class='lparen'>(</span><span class='id identifier rubyid_mname'>mname</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span><span class='rparen'>)</span>
<span class='id identifier rubyid_mname'>mname</span> <span class='op'>=</span> <span class='const'>OpenModel</span><span class='period'>.</span><span class='id identifier rubyid_capitalize'>capitalize</span><span class='lparen'>(</span><span class='id identifier rubyid_mname'>mname</span><span class='rparen'>)</span>
<span class='kw'>end</span>
<span class='kw'>if</span> <span class='ivar'>@table</span><span class='period'>.</span><span class='id identifier rubyid_key?'>key?</span><span class='lparen'>(</span><span class='const'>OpenModel</span><span class='period'>.</span><span class='id identifier rubyid_camelize'>camelize</span><span class='lparen'>(</span><span class='id identifier rubyid_mname'>mname</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span><span class='rparen'>)</span>
<span class='id identifier rubyid_mname'>mname</span> <span class='op'>=</span> <span class='const'>OpenModel</span><span class='period'>.</span><span class='id identifier rubyid_camelize'>camelize</span><span class='lparen'>(</span><span class='id identifier rubyid_mname'>mname</span><span class='rparen'>)</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_len'>len</span> <span class='op'>=</span> <span class='id identifier rubyid_args'>args</span><span class='period'>.</span><span class='id identifier rubyid_length'>length</span>
<span class='kw'>if</span> <span class='id identifier rubyid_is_assign'>is_assign</span>
<span class='kw'>if</span> <span class='id identifier rubyid_len'>len</span> <span class='op'>!=</span> <span class='int'>1</span>
<span class='id identifier rubyid_raise'>raise</span> <span class='const'>ArgumentError</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>wrong number of arguments (</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_len'>len</span><span class='embexpr_end'>}</span><span class='tstring_content'> for 1)</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='id identifier rubyid_caller'>caller</span><span class='lparen'>(</span><span class='int'>1</span><span class='rparen'>)</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_modifiable'>modifiable</span><span class='lbracket'>[</span><span class='id identifier rubyid_new_ostruct_member'>new_ostruct_member</span><span class='lparen'>(</span><span class='id identifier rubyid_mname'>mname</span><span class='rparen'>)</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_args'>args</span><span class='lbracket'>[</span><span class='int'>0</span><span class='rbracket'>]</span>
<span class='kw'>elsif</span> <span class='id identifier rubyid_len'>len</span> <span class='op'>==</span> <span class='int'>0</span>
<span class='ivar'>@table</span><span class='lbracket'>[</span><span class='id identifier rubyid_mname'>mname</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span><span class='rbracket'>]</span>
<span class='kw'>else</span>
<span class='id identifier rubyid_err'>err</span> <span class='op'>=</span> <span class='const'>NoMethodError</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>undefined method `</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_mname'>mname</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span><span class='embexpr_end'>}</span><span class='tstring_content'>' for </span><span class='embexpr_beg'>#{</span><span class='kw'>self</span><span class='embexpr_end'>}</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='id identifier rubyid_mname'>mname</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span><span class='comma'>,</span> <span class='id identifier rubyid_args'>args</span>
<span class='id identifier rubyid_err'>err</span><span class='period'>.</span><span class='id identifier rubyid_set_backtrace'>set_backtrace</span> <span class='id identifier rubyid_caller'>caller</span><span class='lparen'>(</span><span class='int'>1</span><span class='rparen'>)</span>
<span class='id identifier rubyid_raise'>raise</span> <span class='id identifier rubyid_err'>err</span>
<span class='kw'>end</span>
<span class='kw'>end</span></pre>
</td>
</tr>
</table>
</div>
</div>
<div id="class_method_details" class="method_details_list">
<h2>Class Method Details</h2>
<div class="method_details first">
<h3 class="signature first" id="camelize-class_method">
+ (<tt>Object</tt>) <strong>camelize</strong>(sym)
</h3><table class="source_code">
<tr>
<td>
<pre class="lines">
54
55
56</pre>
</td>
<td>
<pre class="code"><span class="info file"># File 'lib/helix_web_services_client/open_model.rb', line 54</span>
<span class='kw'>def</span> <span class='id identifier rubyid_camelize'>camelize</span><span class='lparen'>(</span><span class='id identifier rubyid_sym'>sym</span><span class='rparen'>)</span>
<span class='id identifier rubyid_sym'>sym</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span><span class='period'>.</span><span class='id identifier rubyid_split'>split</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>_</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_inject'>inject</span><span class='lparen'>(</span><span class='lbracket'>[</span><span class='rbracket'>]</span><span class='rparen'>)</span><span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_buffer'>buffer</span><span class='comma'>,</span><span class='id identifier rubyid_e'>e</span><span class='op'>|</span> <span class='id identifier rubyid_buffer'>buffer</span><span class='period'>.</span><span class='id identifier rubyid_push'>push</span><span class='lparen'>(</span><span class='id identifier rubyid_buffer'>buffer</span><span class='period'>.</span><span class='id identifier rubyid_empty?'>empty?</span> <span class='op'>?</span> <span class='id identifier rubyid_e'>e</span> <span class='op'>:</span> <span class='id identifier rubyid_e'>e</span><span class='period'>.</span><span class='id identifier rubyid_capitalize'>capitalize</span><span class='rparen'>)</span> <span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_join'>join</span>
<span class='kw'>end</span></pre>
</td>
</tr>
</table>
</div>
<div class="method_details ">
<h3 class="signature " id="capitalize-class_method">
+ (<tt>Object</tt>) <strong>capitalize</strong>(sym)
</h3><table class="source_code">
<tr>
<td>
<pre class="lines">
50
51
52</pre>
</td>
<td>
<pre class="code"><span class="info file"># File 'lib/helix_web_services_client/open_model.rb', line 50</span>
<span class='kw'>def</span> <span class='id identifier rubyid_capitalize'>capitalize</span><span class='lparen'>(</span><span class='id identifier rubyid_sym'>sym</span><span class='rparen'>)</span>
<span class='id identifier rubyid_sym'>sym</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span><span class='period'>.</span><span class='id identifier rubyid_split'>split</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>_</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span><span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_e'>e</span><span class='op'>|</span> <span class='id identifier rubyid_e'>e</span><span class='period'>.</span><span class='id identifier rubyid_capitalize'>capitalize</span> <span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_join'>join</span>
<span class='kw'>end</span></pre>
</td>
</tr>
</table>
</div>
<div class="method_details ">
<h3 class="signature " id="date_str?-class_method">
+ (<tt>Boolean</tt>) <strong>date_str?</strong>(str)
</h3><div class="docstring">
<div class="discussion">
</div>
</div>
<div class="tags">
<p class="tag_title">Returns:</p>
<ul class="return">
<li>
<span class='type'>(<tt>Boolean</tt>)</span>
</li>
</ul>
</div><table class="source_code">
<tr>
<td>
<pre class="lines">
58
59
60</pre>
</td>
<td>
<pre class="code"><span class="info file"># File 'lib/helix_web_services_client/open_model.rb', line 58</span>
<span class='kw'>def</span> <span class='id identifier rubyid_date_str?'>date_str?</span><span class='lparen'>(</span><span class='id identifier rubyid_str'>str</span><span class='rparen'>)</span>
<span class='const'>Date</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</span><span class='lparen'>(</span><span class='id identifier rubyid_str'>str</span><span class='rparen'>)</span> <span class='kw'>rescue</span> <span class='kw'>false</span>
<span class='kw'>end</span></pre>
</td>
</tr>
</table>
</div>
</div>
</div>
<div id="footer">
Generated on Tue Sep 15 01:20:25 2015 by
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
0.8.7.6 (ruby-2.2.3).
</div>
</body>
</html>
# |
Change |
User |
Description |
Committed |
|
#1
|
15741 |
ptomiak |
Branch HWS for my use. |
|
|