Friday, July 27, 2012

Arbitrary precision decimal floating-point type for Ruby

Hello Rubies,
           Accurately convert numbers  to decimal or exact precision point. Use the Flt gem do discover the below scenario. 

Flt::DecNum is a standards-compliant arbitrary precision decimal floating-point type for Ruby. It is based on the Python Decimal class. 

Usage:

sudo gem install flt

require 'flt'
include Flt
 
x = 0.00000020586
y = Flt::DecNum(x.to_s)
Flt::DecNum.context.precision = 2
puts y/Flt::DecNum(1)

result should be 2.1E-7

For more information just follow the http://flt.rubyforge.org/.

Thursday, July 26, 2012

Import csv file using ruby processor

Hello Guys,
        Let's discuss how to import .csv file into database using ruby processor.

Read the .csv file:

require 'fastercsv'

rows = CSV.read("#{RAILS_ROOT}/test.csv")
header = rows[0]
rows[1..-1].each do |row|
    data = {}
    header.each_with_index do |key, index|
      data[key.downcase] = row[index].strip
    end
    ModelObject.create!(data)
end

test.csv looks like

No,Name,Percentage
1,Priyanka,81
2,Rahul,82
3,Ruby,75

Wednesday, July 25, 2012

Cannot deserialize instance of date from VALUE_STRING on salesforce

Hello Guys,
        I am working on salesforce from past few days. Just discover the bug related to date field setting using databasedotcom gem. When we are setting date value using ruby 1.9.2 it works as per expectation but with ruby 1.8.7 it gives error like 'Cannot deserialize instance of date from VALUE_STRING value'. To resolve this issue need to install latest gem version databasedotcom-1.2.7.

See this thread on github to get more detail

Monday, July 23, 2012

Generate Rss feed with rails application

  Hey guys,
      Lets today we create the rss feed with our existing rails application. Consider we have controller called post and have index method to show all the available posts. Now meanwhile we need to create feed for available posts as well.

controller:

def index
  @posts = Post.find(:all, :order => "created_at desc")
  respond_to do |format|
     format.html { render :template => 'posts/index.rhtml' }
     format.xml { render :template => 'posts/index.rxml', :layout => false
        headers["Content-Type"] = "application/rss+xml"
     }
  end
end

view:

index.rxml

xml.instruct!
xml.rss "version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/" do
  xml.channel do
    xml.title 'Available Posts'
    xml.description h("Here is the posts ... blah blah")
   
    @posts.each do |post|
      xml.item do
        xml.title post.title
        xml.link url_for(:only_path => false,
          :controller => 'posts',
          :action => 'show',
          :fishery => post.id)
        xml.description post.description
        xml.pubDate CGI.rfc1123_date(post.created_at)
      end
    end
  end
end

index.rhtml

# Add auto discovery tag to access rss feed

<%= auto_discovery_link_tag :rss, {:controller => "posts", :action => "index"}%>

See your rss feed for posts is ready!! Whenever auto discovery tag found it enables the rss icon on browser. Keep in mind Either you have to use predefined xml node or need to create XSL template. You can also emend the stylesheet into XSL template.

Thursday, July 19, 2012

Get the Date difference in days

Hello guys,
        Wanna to get the date difference in terms of days?

eg.
date_x = Time.now.to_date
date_y = Date.parse('2012-05-20')

passed_days = (date_x.to_date - date_y.to_date).to_i

passed_days should give the date difference in days.

Thursday, July 5, 2012

Convert string to serialize hash

Hello Rubies,
        Do you phase the problem ? - sometime  when we are retrieving the serialized hash field from table we might get string as response. So in that case you can use one of the below solutions.

Eg. 

Model - User.rb
class User < ActiveRecord::Base
   serialize :content, Hash
end

convert string to serialize hash format by 

# get the one of the record from users table
user = User.first

either using
    user.content.to_yaml
or
    YAML.parse(user.content)
or
    YAML.load(user.content)