Hi
Your call to attr_accessor will create instance methods called test
and test=, but you tried to call test on the class (and so you end up
falling through to Kernel#test).
No I have tested this is not the case, yes I have given wrong example
but with object also it is not working, I have given all details below
I guess I got it working by hit and trial way, so not sure what
exactly is happening around.
Have you seen that validations take :if and :unless options ?
I need to use coustom validation, for them I did not see any :if,
:unless option
But as I got it working I have lot of question in all stages
that I want to put else I will never able to know what is happening.
test 1:
>
> def cvalidation(name, &block)
> attr_accessor name
> define_method "svalidator_#{name}" do |*arg|
> if send name
> yield *arg
> end
> end
> end
=>
>
> class Person
> cvalidation :zen do
> puts "Yes"
> end
> end
=> #<Proc:0x95e73fc@(irb):8 (lambda)>
> x = Person.newq
NoMethodError: undefined method `newq' for Person:Class
So this is not working now in test 2, I now test only attr_accessor
test 2:
> def k(name)
> attr_accessor name
> end
=> nil
> class Personq
> k :test
> end
=> nil
> x = Personq.new
=> #<Personq:0x917a4e0>
> x.test
NoMethodError: private method `test' called for #<Personq:
0x917a4e0>
it again will not work.
test 3:
> def k(name)
> class_eval do
> attr_accessor name
> end
> end
=> nil
> class Pers
> k :u
> end
> x = Pers.new
=> #<Pers:0x8f9bf0c>
> x.u
=> nil
> x.u = "sfdsg"
=> "sfdsg"
> x.u
=> "sfdsg
Q1: Now it is working, but why it did not work in test 2 ?
test 4:
> def cvalidation(name, &block)
> class_eval do
> attr_accessor name
> define_method "svalidator_#{name}" do |*arg|
> if name
> yield *arg
> end
> end
> end
> end
=> nil
>
> class Work
> cvalidation :done do
> puts "Yes"
> end
> end
=> #<Proc:0xa337638@(irb):64 (lambda)>
> x = Work.new
=> #<Work:0xa32d200>
> x.svalidator_done
Yes
=> nil
> x.done
=> nil
Here define_method is working but it is not using argument `name' in
if condition, it uses some other name method, than provided argument
`name'
Q2: Why it is like that and what `name' method or variable it is
actually using in both lines ?
> define_method "svalidator_#{name}" do |*arg|
> if name
test 5:
> def cvalidation(name, &block)
> class_eval do
> attr_accessor name
> define_method "svalidator_#{name}" do |*arg|
> if send "#{name}"
> yield *arg
> end
> end
> end
> end
=> nil
>
> class QQ
> cvalidation :xx do
> puts "Yes"
> end
> end
=> #<Proc:0xa0aaf28@(irb):105 (lambda)>
> x = QQ.new
=> #<QQ:0xa0a09ec>
> x.svalidator_xx
=> nil
> x.xx
=> nil
> x.xx = "sfddsaf"
=> "sfddsaf"
> x.svalidator_xx
Yes
=> nil
> x.xx = nil
=> nil
> x.svalidator_xx
=> nil
> class NN
> cvalidation :ww do |w|
> puts w, " Hi"
> end
> end
=> #<Proc:0x9fbe830@(irb):105 (lambda)>
> x = NN.new
=> #<NN:0x9fabb40>
> x.ww
=> nil
> x.svalidator_ww
=> nil
> x.ww = "Sharad"
=> "Sharad"
> x.svalidator_ww
Hi
=> nil
> x.svalidator_ww "Sharad"
Sharad
Hi
=> nil
> puts "Sharad", "Hi"
Sharad
Hi
=> nil
>
Here I guess as I see all is working, but
I really like to know about `name' used in
> if send "#{name}"
in test 5, uses the `name' arguemnt provided.
But
> if name
in test 4 it do not able to use same `name' argument
Why ?