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>{
+ "device_id": 63354,
+ "channel_no": 6,
+ "interval_ms": 15000,
+ "readings": [
+ {
+ "value": 14232,
+ "time": 1339880802
+ },
+ {
+ "value": 14232,
+ "time": 1339880817
+ }
+ ],
+ "calibration": {
+ "staff": {
+ "name": {
+ "first": "Carl",
+ "middle_initial": "I",
+ "second": "Brator"
+ }
+ },
+ "last_calibration": "2010-06-16T22:06:42+01:00"
+ }
+}
</code></pre>
-<h1>LINK TO FULL RDOCS</h1>
+<p>Blobject let'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 = "#{data.calibration.staff.name.first}#{data.calibration.staff.name.middle_initial}#{data.calibration.staff.name.second}".capitalize
+ => "Calibrator"
-<p>A Blobject is a thin wrapper around a hash</p>
+last_reading = data.readings.last.value if data.readings?
+ => 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'>"</span><span class='tstring_content'>Johnny</span><span class='tstring_end'>"</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
+ => {}
-<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'>"</span><span class='tstring_content'>Johnny</span><span class='tstring_end'>"</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'>"</span><span class='tstring_content'>Begood</span><span class='tstring_end'>"</span></span>
+data.name.first = "Johnny"
+data.name.surname = "Begood"
+ => {:name=>{:first=>"Johnny", :surname=>"Begood"}}
-<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'>"</span><span class='tstring_content'>happy place</span><span class='tstring_end'>"</span></span>
+data.my.object.with.deep.nested.members = "happy place"
+ => {:name=>{:first=>"Johnny", :surname=>"Begood"}, :my=>{:object=>{:with=>{:deep=>{:nested=>{:members=>"happy place"}}}}}}
</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: "#22322" }</p>
<p>data.details.code
=> 41239
+ data.details.code = 11322
+ data.details.code
+ => 11322
data.details.ref
=> "#22322"</p>
<p>You can test to see if a member is defined:</p>
<pre class="code ruby"><code>data.something_here?
=> false
+data.name?
+ => true
</code></pre>
+<p>You can use it like a hash</p>
+
+<pre class="code ruby"><code>data[:name]
+ => {:first=>"Johnny", :surname=>"Begood"}
+
+data[:name][:first] = "Jimmy"; data[:name]
+ => {:first=>"Jimmy", :surname=>"Begood"}
+
+data.empty?
+ => false
+
+data.name == {:first=>"Jimmy", :surname=>"Begood"}
+ => 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'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 <(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('<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')</li>
+<li>Inspect it
+payload.calibration.staff.name
+=> :middle_initial=>"I", :second=>"Brator"</li>
+<li>Test for optional data members
+payload.channel<em>no?
+ => true
+payload.something</em>that<em>is</em>not_there?
+ => false</li>
+<li>Draw your own payload
+mine = Blobject.new my: 123
+mine.to_json
+ => "\"my\":{\"data\":123}"</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'>"</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'>"</span></span>
<span class='kw'>end</span>
<span class='kw'>end</span>
</code></pre>
-<h2>Serializing & 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