Friday, 25 July 2014

Difference between clone and dup

clone: Produces a shallow(as deep as possible) copy of obj.the instance variables of obj are copied, but not the objects they reference. Copies the frozen(cant be modified more) state of obj.

clone will copy the object alone with its internal state. suppose obj extends any module then clone of the obj also have that module extended.

example:
class Hello 
end

 module SayHello
 
  def sayhello
     'hello'
   end
 end


 s = Hello.new
 s.extend(SayHello)
 s1 = s.clone
 s1.sayhello                //     returns  "hello"


dup: Produces a shallow copy of obj.the instance variables of obj are copied, but not the objects they reference.

example:

use the above example if we use dup to create a copy of obj then dup will create a shallow copy of the obj excluding the internal state of the obj.

 s = Hello.new
 s.extend(SayHello)
 s1 = s.dup
 s1.sayhello             
 //     returns  undefined method `sayhello' for #<Hello:0x0000000197b688>















Friday, 18 July 2014

Working with CSV in jquery(Papa parse)

Papa Parse is a powerful CSV (delimited text) parser that gracefully handles large files.you can easily use this and can parse large csv files efficiently.you can get papa parse here.

here is short example of using  Papa Parse:

1.Converts csv to json:
var result = Papa.parse(csv_file)


2.Parse local CSV files:

 $('input[type=file]').parse({
config: {
             complete: function(results) {
                  console.log("Parse results:", results.data);
                 }
             }
           });
 3.Parse remote CSV files:
Papa.parse("http://example.com/file_name.csv", {
        download: true,
        complete: function(results) {
                         console.log("Remote file parsed!", results);
                       }
       });
4.Get data keyed by field name:
var results = Papa.parse(csv, {
                          header: true
                          });        
 


Friday, 11 July 2014

Working with Action Mailer Callbacks in Rails

If you want to perform some operation after the mail delivery you can use the Action Mailer callbacks.

Action Mailer allows for you to specify a before_action, after_action and around_action
  • Filters can be specified with a block or a symbol to a method in the mailer class similar to controllers.
  • You could use a before_action to populate the mail object with defaults, delivery_method_options or insert default headers and attachments.
  • You could use an after_action to do similar setup as a before_action but using instance variables set in your mailer action.
 you can use callbacks same as like in the controllers.here is an example of it.

class UserMailer < ActionMailer::Base
  after_action :set_delivery_options

  def feedback_message(user)
    @user = user
    mail
  end
 
  private
 
    def set_delivery_options
      # You have access to the mail instance,
      # @user instance variables here
      # your code here
    end
end

Friday, 4 July 2014

Working with Nokogiri::XML::Node with Rails

I am using gem 'nokogiri' for Nokogiri::XML::Node manipulations.here are the some examples of performing the basics operations on the Nokogiri::XML::Node.
you can perform these operations with Nokogiri::XML::Node.

say for the example you have the Nokogiri::XML::Node named node,now here are some basic operation with this node.

1.Get parent: you can fetch the parent of the node by,
node.parent // will return the parent of the node.

2. Get xpath :
node.path // will return of the xpath of the node in document.
 

3.Get node at some path in document:
_node = doc.xpath('some_path')
where doc is a nokogiri document.it will return a nodeset if you want to get the exact node then do this

_node = doc.xpath('some_path')[0] or _node = doc.xpath('some_path').first

4.Get children:
node.children // will return the nodeset of childs of node your can select them according to your need.

5.Add children:
node.add_child('child_html') // add child in as the last child.

6.Add siblings:
node.add_next_sibling('some_html') // add a sibling node after the node in parent.
node.add_previous_sibling('some_html') // add a sibling node before the node in parent node.

7.Replace a node:
node.replace('replace_node's_html')// replace a node by given html.

8. Styling a node:
node['class'] = 'some_css_class' // add a css class to node.
css_class = node['class'] // get the css_class of the node.

9. Show html:
node.to_html // will return html of the node.