doc/index.html in blobject-0.3.7 vs doc/index.html in blobject-0.4.0

- old
+ new

@@ -59,60 +59,143 @@ <div class="clear"></div> </div> <iframe id="search_frame"></iframe> - <div id="content"><div id='filecontents'><p><img src="https://github.com/sjltaylor/blobject/raw/master/blobject.png" alt=""> -<img src="https://github.com/sjltaylor/blobject/raw/master/blob_defn.png" alt=""></p> + <div id="content"><div id='filecontents'><p><img src="https://github.com/sjltaylor/blobject/raw/master/assets/blobject.png" alt=""> </p> -<pre class="code ruby"><code># Wrapper around a hash to provide arbitrarily nested object style access to attributes -class Blobject +<p>Rdocs: <a href="http://sjltaylor.github.com/blobject">http://sjltaylor.github.com/blobject</a></p> + +<h2>About</h2> + +<p>Consider the following json sample...</p> + +<pre class="code ruby"><code>{ + &quot;device_id&quot;: 63354, + &quot;channel_no&quot;: 6, + &quot;interval_ms&quot;: 15000, + &quot;readings&quot;: [ + { + &quot;value&quot;: 14232, + &quot;time&quot;: 1339880802 + }, + { + &quot;value&quot;: 14232, + &quot;time&quot;: 1339880817 + } + ], + &quot;calibration&quot;: { + &quot;staff&quot;: { + &quot;name&quot;: { + &quot;first&quot;: &quot;Carl&quot;, + &quot;middle_initial&quot;: &quot;I&quot;, + &quot;second&quot;: &quot;Brator&quot; + } + }, + &quot;last_calibration&quot;: &quot;2010-06-16T22:06:42+01:00&quot; + } +} </code></pre> -<h1>LINK TO FULL RDOCS</h1> +<p>Blobject let&#39;s you do this (complete code listing, no predefined data structures):</p> -<h1>LINK TO NARRATIVE</h1> +<pre class="code ruby"><code>data = Blobject.from_json HTTParty.get('https://raw.github.com/sjltaylor/blobject/master/spec/sample_data/sample3.json') -<h2>About</h2> +full_name = &quot;#{data.calibration.staff.name.first}#{data.calibration.staff.name.middle_initial}#{data.calibration.staff.name.second}&quot;.capitalize + =&gt; &quot;Calibrator&quot; -<p>A Blobject is a thin wrapper around a hash</p> +last_reading = data.readings.last.value if data.readings? + =&gt; 14232 +</code></pre> -<p>They are <em>freeform</em> which means you can do this...</p> +<p>Blobject is convenient to creating json payloads too. +Blobjects are <em>freeform</em> which means you can do this...</p> <pre class="code ruby"><code><span class='id identifier rubyid_data'>data</span> <span class='op'>=</span> <span class='const'>Blobject</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_name'>name</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Johnny</span><span class='tstring_end'>&quot;</span></span> <span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_number'>number</span> <span class='op'>=</span> <span class='int'>316</span> </code></pre> -<p>like an OpenStruct, the members are not predefined attributes</p> +<p>like an <code>OpenStruct</code>, the members are not predefined attributes</p> -<p>unlike OpenStruct, Blobjects can be arbitrarily <em>complex</em> which means you can do this...</p> +<p>unlike <code>OpenStruct</code>, <code>Blobject</code>s can be arbitrarily <em>complex</em> which means you can do this...</p> -<pre class="code ruby"><code><span class='id identifier rubyid_data'>data</span> <span class='op'>=</span> <span class='const'>Blobject</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> +<pre class="code ruby"><code>data = Blobject.new + =&gt; {} -<span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_name'>name</span><span class='period'>.</span><span class='id identifier rubyid_first'>first</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Johnny</span><span class='tstring_end'>&quot;</span></span> -<span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_name'>name</span><span class='period'>.</span><span class='id identifier rubyid_surname'>surname</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Begood</span><span class='tstring_end'>&quot;</span></span> +data.name.first = &quot;Johnny&quot; +data.name.surname = &quot;Begood&quot; + =&gt; {:name=&gt;{:first=&gt;&quot;Johnny&quot;, :surname=&gt;&quot;Begood&quot;}} -<span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_my'>my</span><span class='period'>.</span><span class='id identifier rubyid_object'>object</span><span class='period'>.</span><span class='id identifier rubyid_with'>with</span><span class='period'>.</span><span class='id identifier rubyid_deep'>deep</span><span class='period'>.</span><span class='id identifier rubyid_nested'>nested</span><span class='period'>.</span><span class='id identifier rubyid_members'>members</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>happy place</span><span class='tstring_end'>&quot;</span></span> +data.my.object.with.deep.nested.members = &quot;happy place&quot; + =&gt; {:name=&gt;{:first=&gt;&quot;Johnny&quot;, :surname=&gt;&quot;Begood&quot;}, :my=&gt;{:object=&gt;{:with=&gt;{:deep=&gt;{:nested=&gt;{:members=&gt;&quot;happy place&quot;}}}}}} </code></pre> -<p>or assign hashes which become nested blobjects</p> +<p>You can assign hashes which become nested blobjects</p> <p>data.details = { code: 41239, ref: &quot;#22322&quot; }</p> <p>data.details.code =&gt; 41239 + data.details.code = 11322 + data.details.code + =&gt; 11322 data.details.ref =&gt; &quot;#22322&quot;</p> <p>You can test to see if a member is defined:</p> <pre class="code ruby"><code>data.something_here? =&gt; false +data.name? + =&gt; true </code></pre> +<p>You can use it like a hash</p> + +<pre class="code ruby"><code>data[:name] + =&gt; {:first=&gt;&quot;Johnny&quot;, :surname=&gt;&quot;Begood&quot;} + +data[:name][:first] = &quot;Jimmy&quot;; data[:name] + =&gt; {:first=&gt;&quot;Jimmy&quot;, :surname=&gt;&quot;Begood&quot;} + +data.empty? + =&gt; false + +data.name == {:first=&gt;&quot;Jimmy&quot;, :surname=&gt;&quot;Begood&quot;} + =&gt; true +</code></pre> + +<p>You can get access to the internal hash with <code>Blobject#hash</code> or a de-blobjectified copy with <code>Blobject#to_hash</code></p> + +<p>You can call <code>Blobject#freeze</code> to prevent the data being modified. This still allows chained calls: <code>blobject.data.nested.is_something_here?</code> but assignments will raise <code>RuntimeError: can&#39;t modify frozen Hash</code></p> + +<p>You can work with JSON data using <code>Blobject.from_json</code> and <code>Blobject#to_json</code>, in Rails, you can use <code>Blobject#as_json</code> as you would with a hash.</p> + +<p>You can work with YAML data using <code>Blobject.from_yaml</code> and <code>Blobject#to_yaml</code>.</p> + +<h2>Try it out...</h2> + +<ol> +<li>Install the gem (ruby 1.9.2+ required): <code>sh &lt;(curl https://raw.github.com/sjltaylor/blobject/master/try_blobject.sh)</code></li> +<li>Load data from an api... +payload = Blobject.from<em>json HTTParty.get(&#39;<a href="https://raw.github.com/sjltaylor/blobject/master/spec/sample">https://raw.github.com/sjltaylor/blobject/master/spec/sample</a></em>data/sample3.json&#39;)</li> +<li>Inspect it +payload.calibration.staff.name +=&gt; :middle_initial=&gt;&quot;I&quot;, :second=&gt;&quot;Brator&quot;</li> +<li>Test for optional data members +payload.channel<em>no? + =&gt; true +payload.something</em>that<em>is</em>not_there? + =&gt; false</li> +<li>Draw your own payload +mine = Blobject.new my: 123 +mine.to_json + =&gt; &quot;\&quot;my\&quot;:{\&quot;data\&quot;:123}&quot;</li> +</ol> + <h2>Used for Configuration</h2> <p>Consider a configuration object which contains credentials for a third-party api.</p> <pre class="code ruby"><code>third_party_api: @@ -148,11 +231,11 @@ <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_protocol'>protocol</span><span class='rbrace'>}</span><span class='tstring_content'>://</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_hostname'>hostname</span><span class='rbrace'>}</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_path'>path</span><span class='rbrace'>}</span><span class='tstring_end'>&quot;</span></span> <span class='kw'>end</span> <span class='kw'>end</span> </code></pre> -<h2>Serializing &amp; Deserializing</h2> +<h2>Serialization</h2> <p>Blobjects can be used to easily build complex payloads.</p> <pre class="code ruby"><code>person = Blobject.new @@ -255,13 +338,15 @@ <p>THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p> <p>Copyright (c) 2012 Sam Taylor. See LICENSE.txt for further details.</p> + +<p><img src="https://github.com/sjltaylor/blobject/raw/master/assets/blob_defn.png" alt=""></p> </div></div> <div id="footer"> - Generated on Thu Jun 7 00:25:31 2012 by + Generated on Sun Jun 17 00:20:42 2012 by <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a> 0.8.1 (ruby-1.9.3). </div> </body> \ No newline at end of file