Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert Usage of PROC objects...

Replytotopic

Usage of PROC objects

Posted in Forums : Ask a Rails expert

 
Profile

Authority 12
Posting Rating 89
Sign in to rate this post

In some of the Ruby tutorials PROC are explained as callable objects in ruby and they are not executed unless called explicitly.

Like

pr = Proc.new { puts “Hello World” }

This statement is not executed at this point

But if we do

pr.call

it results in

=> Hello World

I would like to know how this works in our model validations

suppose we have

validates_length_of :user_name, :maximum => 40,:if => Proc.new { |user| !user.user_name.blank? }

so to do this validation we have specified a condition using a Proc block but how is this Proc block called ? is the call to Proc block made internally by rails?

 
Working_with_rails

Authority 12
Posting Rating 91
Sign in to rate this post

\lib\ruby\gems\1.8\gems\activerecord-1.15.3\lib\active_record\validations.rb(from line 260):


      def evaluate_condition(condition, record)
        case condition
          when Symbol: record.send(condition)
          when String: eval(condition, binding)
          else
            if condition_block?(condition)
              condition.call(record) #!!!!!!!!!!!!!!!!!!!!
            else
              raise(
                ActiveRecordError,
                "Validations need to be either a symbol, string (to be eval'ed), proc/method, or " +
                "class implementing a static validation method" 
              )
            end
          end
      end

[...]

      def validates_each(*attrs)
        options = attrs.last.is_a?(Hash) ? attrs.pop.symbolize_keys : {}
        attrs   = attrs.flatten

        # Declare the validation.
        send(validation_method(options[:on] || :save)) do |record|
          # Don't validate when there is an :if condition and that condition is false
          unless options[:if] && !evaluate_condition(options[:if], record)  #!!!!!!!!!!!!!!!!!!!!
            attrs.each do |attr|
              value = record.send(attr)
              next if value.nil? && options[:allow_nil]
              yield record, attr, value
            end
          end
        end
      end
 
Profile

Authority 12
Posting Rating 89
Sign in to rate this post

Thanks for the reply Marcin

 
Working_with_rails

Authority 12
Posting Rating 91
Sign in to rate this post

I’m glad you found this helpful Balaji:)

Replytotopic

Other Recent Topics

Ask a Rails expert : how to execute the url from my controller

Ask a Rails expert : conditional action caching multiple mongrel clusters

Ask a Rails expert : Using like command in RoR

Ask a Rails expert : Apache ActionController:RoutingError

Ask a Rails expert : Multiple csv file upload based on data in a form

Ask a Rails expert : Exception error code

Ask a Rails expert : Tracking down an issue

Ask a Rails expert : Thread Vs Transaction

Ask a Rails expert : implementing whitelist plugin

Ask a Rails expert : Validation helper

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel