<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>for i in infinity</title>
	<atom:link href="http://www.anup.info/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.anup.info</link>
	<description>Ruby on Rails Developer, London</description>
	<pubDate>Fri, 15 Oct 2010 15:29:26 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<item>
		<title>HashClass.js</title>
		<link>http://www.anup.info/2010/10/03/hashclassjs/</link>
		<comments>http://www.anup.info/2010/10/03/hashclassjs/#comments</comments>
		<pubDate>Sun, 03 Oct 2010 21:26:10 +0000</pubDate>
		<dc:creator>anup.narkhede</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.anup.info/?p=262</guid>
		<description><![CDATA[HashClass.js is a Hash with observable attributes, written to remove ObservableHash dependency from MooModel. Written for node.js using server side mootools, HashClass provides an ability to watch attribute changes by attaching a callback function.
To get a server side version of mootools follow instructions here http://davidwalsh.name/mootools-nodejs
Example: demo.js

require("../lib/mootools-serverside").apply(GLOBAL);
require("../lib/HashClass").apply(GLOBAL);

var Person = new HashClass();
var person = new Person({ name: [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/railsbob/HashClass.js">HashClass.js</a> is a Hash with observable attributes, written to remove ObservableHash dependency from <a href="http://github.com/railsbob/moo-model">MooModel</a>. Written for <a href="http://nodejs.org/">node.js</a> using server side mootools, HashClass provides an ability to watch attribute changes by attaching a callback function.</p>
<p>To get a server side version of mootools follow instructions here <a href="http://davidwalsh.name/mootools-nodejs">http://davidwalsh.name/mootools-nodejs</a></p>
<p>Example: demo.js</p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
require("../lib/mootools-serverside").apply(GLOBAL);
require("../lib/HashClass").apply(GLOBAL);

var Person = new HashClass();
var person = new Person({ name: 'anup', title: 'HashClass' });

// Observe change on any attribute
Person.onChange(function(instance){
  console.log('something changed');
});

person.set('title', 'HashClass.js')
=> something changed

// Observe a particular attribute
Person.onChange('name', function(instance){
  console.log('name changed');
});

person.set('name', 'Anup');
=> name changed
</textarea>
]]></content:encoded>
			<wfw:commentRss>http://www.anup.info/2010/10/03/hashclassjs/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Introducing MooModel: A Javascript Framework</title>
		<link>http://www.anup.info/2010/09/05/introducing-moomodel/</link>
		<comments>http://www.anup.info/2010/09/05/introducing-moomodel/#comments</comments>
		<pubDate>Sun, 05 Sep 2010 16:42:56 +0000</pubDate>
		<dc:creator>anup.narkhede</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[MooTools]]></category>

		<guid isPermaLink="false">http://www.anup.info/?p=184</guid>
		<description><![CDATA[Introduction
MooModel is a javascript framework built on top of mootools, to simplify the use of objects and classes in javascript. It comes with an elegant syntax and built in support for object oriented features. Additionally it provides observable attributes, ActiveModel like validations and a REST persistence adapter.
MooModel makes use of the powerful object oriented features [...]]]></description>
			<content:encoded><![CDATA[<h4>Introduction</h4>
<p><a href="http://github.com/railsbob/moo-model">MooModel</a> is a javascript framework built on top of <a href="http://mootools.net/">mootools</a>, to simplify the use of objects and classes in javascript. It comes with an elegant syntax and built in support for object oriented features. Additionally it provides observable attributes, ActiveModel like validations and a REST persistence adapter.</p>
<p>MooModel makes use of the powerful object oriented features provided by mootools. With the latest introduction of <a href="http://mootools.net/download/get/mootools-1.2.4-core-server.js">server side mootools</a>, it is possible to use same javascript on your browser and server. The current version of MooModel (0.0.1) is not CommonJS compliant and does not support server side mootools either. However, I am working to release a server edition soon. Follow this article to know more about using mootools  as <a href="http://davidwalsh.name/mootools-nodejs">server side javascript</a>.</p>
<h4>Getting Started</h4>
<p>To define a class:</p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
var Person = new Class({
  Extends: MooModel.Base
});
</textarea>
<p>To create new instances of Person class:</p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
foo = new Person({ id: 1, name: 'foo' })
bar = new Person({ id: 2, name: 'bar' })
</textarea>
<p>Attribute values can be accessed using getter and setter functions:</p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
foo.get('name')
foo.set('name', 'railsbob')
</textarea>
<p>The Base class provides a collection array to store instances and they can be added as:</p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
Person.add(foo)
Person.add(bar)
</textarea>
<p>The collection belongs to the page context when used in a browser. Records can be fetched by using the finders as:</p>
<ul class="console">
<li>Person.find(id)</li>
<li>Person.all()</li>
<li>Person.first()</li>
</ul>
<h4>Custom Methods</h4>
<p>Instance methods and class methods can be defined as shown below:</p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
var Person = new Class({
  Extends: MooModel.Base,

  // Class Methods
  ClassMethods: {
    custom_finder: function(){
      return 'class method invoked!';
    }
  },

  // Instance Methods
  speak: function(){
    return 'instance method invoked!';
  }
});

foo = new Person({ name: 'foo' });
</textarea>
<ul class="console">
<li>foo.speak() => instance method invoked!</li>
<li>Person.custom_finder() => class method invoked!</li>
</ul>
<h4>Inheritance</h4>
<p>Mootools uses Javascript&#8217;s native prototype inheritance model. MooModel uses this built in support to give you access to all instance and class methods from a subclass.</p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
var Employee = new Class({
  Extends: Person
});

bob = new Employee({ name: 'bob' });
</textarea>
<ul class="console">
<li>Employee.custom_finder() => class method invoked!</li>
<li>bob.speak() => instance method invoked!</li>
</ul>
<h4>Class Mixin</h4>
<p>Implements is a mootools class mutator, used to include properties from one or more classes without inheritance. It is useful while implementing a default set of properties in multiple classes.</p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
var Able = new Class({
  can_login: function(){
    return true;
  }
});

var Person = new Class({
  Extends: MooModel.Base,
  Implements: Able
});

var person = new Person({ id: 1 });
</textarea>
<ul class="console">
<li>person.can_login() => true</li>
</ul>
<h4>Events</h4>
<p>MooModel uses <a href="http://github.com/subtleGradient/mootools-data-binding">ObservableHash</a> internally to store attributes. This brings all coolness of attaching functions to attribute change events. Check the examples below:</p>
<p><strong>Initialize</strong></p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
var person = new Person({ id: 1, name: "bean" });
</textarea>
<p><strong>Observe set event on &#8216;name&#8217; attribute:</strong></p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
person.attributes.observeSet('name', function(value){
  console.log('value changed to ' + value);
});
</textarea>
<ul class="console">
<li>person.set(&#8217;name&#8217;, &#8216;railsbob&#8217;) => Console: value changed to railsbob</li>
</ul>
<p><strong>Observe get event on &#8216;name&#8217; attribute:</strong></p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
person.attributes.observeGet('name', function(){
  // do something when the value is fetched
});
</textarea>
<p><strong>Observe erase event on &#8216;name&#8217; attribute:</strong></p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
person.attributes.observeErase('name', function(){
  // do something when this attribute is erased from hash
});
</textarea>
<p><strong>Observe add event on the class collection:</strong></p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
Person.observeAdd(function(instance){
  console.log('added ' + instance.get('name'));
});
</textarea>
<ul class="console">
<li>Person.add(new Post({id: 1, name: &#8220;bean&#8221;})) => Console: added bean</li>
</ul>
<p><strong>Observe remove event on the class collection:</strong></p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
Person.observeRemove(function(instance){
  console.log('removed ' + instance.get('name'));
});
var post = new Person({id: 1, name: "bean"});
Person.add(person);
</textarea>
<ul class="console">
<li>Person.remove(person.get(&#8217;id&#8217;)) => Console: removed bean</li>
</ul>
<h4>Validations</h4>
<p>MooModel provides a full validation framework. The following example shows an implementation of a custom validation, defined on an attribute with a regex matcher.</p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
var Person = new Class({
  Extends: MooModel.Base,
  Validations: [{
    attribute: 'employeeNumber',
    type: 'custom',
    regex: new RegExp(/^\d{5}$/),
    message: 'should be a 5 digit number'
  }]
});
</textarea>
<ul class="console">
<li>person = new Person({id: 1, employeeNumber: &#8216;12345&#8242; })</li>
<li>person.valid() => true</li>
<li>person.set(&#8217;employeeNumber&#8217;, &#8216;123ASDF&#8217;)</li>
<li>person.valid() => false</li>
<li>person.errors() => ['employeeNunber', 'should be a 5 digit number']</li>
<li>person.errors.on(&#8217;employeeNumber&#8217;) => ['should be a 5 digit number']</li>
</ul>
<p>MooModel provides a set of standard validation rules, as listed below:</p>
<p><strong>Presence</strong></p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
Validations: [{ attribute: 'name', type: 'presence' }]
</textarea>
<p><strong>Length</strong></p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
Validations: [{ attribute: 'name', type: 'length', is: 14, allow_null: true  }]
</textarea>
<p>Options: </p>
<ul class="console">
<li>minimum: Number</li>
<li>maximum: Number</li>
<li>within: [0, n] (Array)</li>
</ul>
<p><strong>Numericality</strong></p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
Validations: [{
  attribute: 'number', 
  type: 'numericality',
  allow_null: true,
  message: "should be a number"
}]
</textarea>
<p><strong>Exclusion</strong></p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
Validations: [{
  attribute: 'skills', 
  type: 'exclusion',
  options: ['php'], 
  message: 'is not allowed'
}]
</textarea>
<p><strong>Inclusion</strong></p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
Validations: [{ 
  attribute: 'skills', 
  type: 'inclusion', 
  options: ['ror', 'mongo'],
   message: 'is not allowed'
}]
</textarea>
<p><strong>Within a given range</strong></p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
Validations: [{ 
  attribute: 'age', 
  type: 'within', 
  min: 20, 
  max: 25, 
  required: true, 
  message: 'out of range'
}]
</textarea>
<p>These <a href="http://github.com/railsbob/moo-model/tree/master/test/public/tests/validations/">tests</a> provide more information on usage of these validation rules. </p>
<h4>Dirty Attributes</h4>
<p>MooModel provides ActiveModel like dirty attributes to track attribute changes.</p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
var Person = new Class({
  Extends: MooModel.Base
});

var person = new Person({ id: 100, name: 'railsbob' })
</textarea>
<ul class="console">
<li>person.has_changed() => false</li>
<li>person.set(&#8217;name&#8217;, &#8216;anup&#8217;)</li>
<li>person.has_changed() => true</li>
</ul>
<p>Which attribute/s changed?</p>
<ul class="console">
<li>person.changed() => ["name"]</li>
</ul>
<p>And what were the changes on &#8216;name&#8217; attribute?</p>
<ul class="console">
<li>person.changes(&#8221;name&#8221;) => ["railsbob", "anup"]</li>
</ul>
<p> To Reset an attribute and restore the previous value:</p>
<ul class="console">
<li>person.reset(&#8221;name&#8221;)</li>
<li>person.get(&#8221;name&#8221;) => railsbob</li>
<li>person.has_changed() => false</li>
<li>person.changed() => []</li>
</ul>
<p>To reset all attributes:</p>
<ul class="console">
<li>person.reset()</li>
</ul>
<h4>Persistence</h4>
<p>Currently MooModel supports REST persistence for client side use. MooModel uses ActiveSupport style inflections to define the routes. For ex: resource_name: &#8216;person&#8217; corresponds to route /people/:id. A .save() call on an object fires an ajax json request to the route url. Refer <a href="http://github.com/railsbob/moo-model/blob/master/test/public/tests/restPersistence.js">rest persistence tests</a> for more information.</p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
var Person = new Class({
  Extends: MooModel.Base,
  Persistence: { uses: MooModel.RestPersistence, resource_name: 'person' }
});

var person = new Person({ id: 10, name: 'dood' })
</textarea>
<ul class="console">
<li>person.resource_path() => &#8216;/people/10&#8242;</li>
<li>person.collection_path() => &#8216;/people&#8217;</li>
</ul>
<p>The resolved route can be overridden by passing in a route option as follows:<br />
Ex:</p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
Persistence: { uses: MooModel.RestPersistence, route: '/employees' }
</textarea>
<ul class="console">
<li>person.resource_path() => &#8216;/employees/10&#8242;</li>
<li>person.collection_path() => &#8216;/employees&#8217;</li>
<li>person.save() => posts attributes to the appropriate path using ajax</li>
</ul>
<p>The project is hosted on <a href="http://github.com/railsbob/moo-model">Github</a>. For any issues and suggestions, contact me at anup[dot]narkhede[at]gmail.com.</p>
<p><strong>Next Steps</strong></p>
<li>Make moo-model compliant with server side mootools</li>
<li>CommonJS compliance</li>
<li>MongoDB persistence adapter for use on server side</li>
<p>For updates on latest features, follow me on <a href="http://twitter.com/railsbob">twitter</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anup.info/2010/09/05/introducing-moomodel/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Globalize2 and Thinking Sphinx</title>
		<link>http://www.anup.info/2010/08/17/globalize2-and-thinking-sphinx/</link>
		<comments>http://www.anup.info/2010/08/17/globalize2-and-thinking-sphinx/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 07:23:11 +0000</pubDate>
		<dc:creator>anup.narkhede</dc:creator>
		
		<category><![CDATA[Code Recipes]]></category>

		<category><![CDATA[Ruby on Rails]]></category>

		<category><![CDATA[Thinking Sphinx]]></category>

		<guid isPermaLink="false">http://www.anup.info/?p=178</guid>
		<description><![CDATA[If you are trying to integrate Thinking Sphinx with models using Globalize2 for translations, here is what worked for me. Many thanks to Zach Inglis and Pat Allan for their help.
My Article class uses Globalize2 to translate &#8216;title&#8217; attribute in two locales (:en and :fr). Globalize2 creates an association named &#8216;translations&#8217; for the table article_translations.
The [...]]]></description>
			<content:encoded><![CDATA[<p>If you are trying to integrate Thinking Sphinx with models using Globalize2 for translations, here is what worked for me. Many thanks to <a href="http://www.zachinglis.com/">Zach Inglis</a> and <a href="http://freelancing-gods.com/">Pat Allan</a> for their help.</p>
<p>My Article class uses Globalize2 to translate &#8216;title&#8217; attribute in two locales (:en and :fr). Globalize2 creates an association named &#8216;translations&#8217; for the table article_translations.</p>
<p>The solution is to set up one index per locale. As you see, &#8216;article_en&#8217; automatically creates &#8216;article_en_core&#8217; and &#8216;article_en_delta&#8217; index if you use delta indexing.</p>
<textarea name="code" class="ruby:nocontrols" cols="60" rows="10">
class Article < ActiveRecord::Base

  translates :title

  define_index('article_en') do
    indexes translations.title
    where "article_translations.locale='en'"
  end

  define_index('article_fr') do
    indexes translations.title
    where "article_translations.locale='fr'"
  end
end
</textarea>
<p>To limit your search within a particular locale, pass on the :index option.<br />
For ex: </p>
<textarea name="code" class="ruby:nocontrols" cols="60" rows="10">
Article.search 'Internationale', :index => 'article_fr'
Or
Article.search @q, :index => "article_{ I18n.locale }"
</textarea>
]]></content:encoded>
			<wfw:commentRss>http://www.anup.info/2010/08/17/globalize2-and-thinking-sphinx/feed/</wfw:commentRss>
		</item>
		<item>
		<title>MooTools: Class Methods and Inheritance</title>
		<link>http://www.anup.info/2010/07/12/mootools-class-methods-and-inheritance/</link>
		<comments>http://www.anup.info/2010/07/12/mootools-class-methods-and-inheritance/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 22:49:59 +0000</pubDate>
		<dc:creator>anup.narkhede</dc:creator>
		
		<category><![CDATA[Code Recipes]]></category>

		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[MooTools]]></category>

		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.anup.info/?p=172</guid>
		<description><![CDATA[In my last project, I enjoyed scripting using mootools and started exploring it more deeply. One of my extensive use was to define UI elements&#8217; functionality in classes and then create multiple instances for re-usability. 
Mootools makes use of Javascript&#8217;s native prototype model for inheritance. To extend a base functionality into sub class, mootools provide [...]]]></description>
			<content:encoded><![CDATA[<p>In my last project, I enjoyed scripting using mootools and started exploring it more deeply. One of my extensive use was to define UI elements&#8217; functionality in classes and then create multiple instances for re-usability. </p>
<p>Mootools makes use of Javascript&#8217;s native prototype model for inheritance. To extend a base functionality into sub class, mootools provide &#8216;Extends&#8217; method. However, if you have class methods in your base model, Extends is insufficient to get them into the sub classes. After some research, I managed to solve this by using Class Mutators and overriding the Mootools &#8216;Extends&#8217; to take care of class methods inheritance.</p>
<p>First of all, we need to define a ClassMethods mutator. Mutators belong to a class, not to their instances and they are called only when we define a new Class.</p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
Class.Mutators.ClassMethods = function(methods){
  this.__classMethods = $extend(this.__classMethods || {}, methods);
  this.extend(methods);
};
</textarea>
<p>Next, we need to redefine Mootools Extends mutator:</p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
Class.Mutators.Extends = function(parent){
  this.parent = parent;
  this.prototype = Class.instantiate(parent);

  this.implement('parent', function(){
    var name = this.caller._name, previous = this.caller._owner.parent.prototype[name];
    if (!previous) throw new Error('The method "' + name + '" has no parent.');
    return previous.apply(this, arguments);
  }.protect());

  this.extend(parent.__classMethods);
};
</textarea>
<p>Check out the example below:</p>
<textarea name="code" class="javascript:nocontrols" cols="60" rows="10">
var Foo = new Class({
  ClassMethods: {
    hello: function(){
      alert('hello from foo');
    }
  },
  
  size: function(){
    alert('foo instance method called');
  }
});

var Bar = new Class({
  Extends: Foo,
  
  ClassMethods: {
    hola: function(){
      alert('hola from bar');
    }
  }
});

var f = new Foo();
var b = new Bar();


// ClassMethods
Foo.hello() => alerts "hello from foo"
Bar.hello() => alerts "hello from foo"
Bar.hola()  => alerts "hola from bar"

// Instance methods
f.size() => 'foo instance method called'
b.size() => 'foo instance method called'

// Call ClassMethods from instances
f.constructor.hello() => "hello from foo"
b.constructor.hello() => "hello from foo"
</textarea>
<p>You can see the above code working here. <a href=" http://jsfiddle.net/rZvJE/11/"><br />
http://jsfiddle.net/rZvJE/11/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.anup.info/2010/07/12/mootools-class-methods-and-inheritance/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Daily Blockers #2</title>
		<link>http://www.anup.info/2010/03/02/daily-blockers-2/</link>
		<comments>http://www.anup.info/2010/03/02/daily-blockers-2/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 17:31:53 +0000</pubDate>
		<dc:creator>anup.narkhede</dc:creator>
		
		<category><![CDATA[Ruby on Rails]]></category>

		<category><![CDATA[blockers]]></category>

		<guid isPermaLink="false">http://www.anup.info/?p=169</guid>
		<description><![CDATA[Problem:

/Users/railsbob/multiruby/install/1.8.7-p249/lib/ruby/gems/1.8/gems/
activesupport-2.3.5/lib/active_support/multibyte/unicode_database.rb:37:
[BUG] Segmentation fault
ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-darwin9.6.0]

Solution:
Somehow my activesupport-2.3.5/lib/active_support/values/unicode_tables.dat was corrupted. Reinstalling activesupport gem fixed the issue.
]]></description>
			<content:encoded><![CDATA[<p>Problem:</p>
<textarea name="code" class="ruby:nocontrols" cols="60" rows="10">
/Users/railsbob/multiruby/install/1.8.7-p249/lib/ruby/gems/1.8/gems/
activesupport-2.3.5/lib/active_support/multibyte/unicode_database.rb:37:
[BUG] Segmentation fault
ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-darwin9.6.0]
</textarea>
<p>Solution:<br />
Somehow my activesupport-2.3.5/lib/active_support/values/unicode_tables.dat was corrupted. Reinstalling activesupport gem fixed the issue.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anup.info/2010/03/02/daily-blockers-2/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.647 seconds -->

