how can I write my own validation helper method that can be reused in several different models - ruby-on-rails-2

I want to write one common validation helper method that can be used in different models
e.g.
def validate_my_own_column(arg1, arg2)
{
if arg1 > arg2
...
else
...
end
}
I want to call validate_my_own_column(arg1, arg2) method in various models.
how can I achieve this?
Thanks

You tagged this question ruby-on-rails-2. Unfortunately what you want is somewhat complicated to do in Rails 2. If you are in fact using Rails 3, it's simple: take a look at http://api.rubyonrails.org/classes/ActiveModel/Validator.html.

Related

Dart equivalent of Ruby's instance_eval

Ruby has a really neat method called instance_eval that allows evaluating a block of code in the context of a specific instance. This is great because one can use the same block of code in different contexts and get different results based on how the context defines certain methods. Is there something equivalent in Dart? In other words can I take a method from a class and attach and execute it on another instance in another class or even define a method by letting the user pass in the code corresponding to the method. I know some limited form of this is possible to emulate with subclassing.
It seems that there no possibility of doing that in Dart.
But you could use several approaches, depending on your needs:
Creating a function with context as an argument of this function:
someFunction (context, arg1, arg2) { ... }
Using mixins: https://www.dartlang.org/articles/mixins/
Using generics: https://www.dartlang.org/articles/optional-types/#generics

Store and Call Ruby Method without using .method/.call

I'm adding unit tests to a large batch of code and am looking for a way to insert fake methods in for testing purposes. The problem is that, as far as I know in Ruby, to pass a method in one must use ClassName.method(:method_name), and then refactor the method I'm testing to use boo.call() instead of just boo(). Is there an easier way to do this than refactoring everything to use .method and .call?
Why not just pass lambdas? I mean, lambda is just an anonymous method/function as you know it from other languages, so it should work fine. Eg:
fake_method = lambda { |n| "do something with n" }
def other_method(fm)
#...
fm.call
#...
end
other_method(fake_method)
You still need to call .call though

What is the preferred way (better style) to define a method in one line in Ruby?

What is the better style:
def method; some code end
or
def method() some code end
and why?
The preferred way is to not define a method in one line, as #Romain said.
def method
some code
end
Some people use {} instead of begin; end when writing blocks on only one line.
Maybe this suits your needs:
class A
define_method(:method_name) { |arg1, arg2| do_something }
end
Defining a method on one line should only be done in documentation, otherwise split it onto three lines:
def method
some code
end
If you find that you have a lot of one-liner methods (i.e. getters & setters) there could be many metaprogramming techniques (such as using define_method) to reduce duplication. For instance, you can use :attr_accessor to define many getters & setters:
class Person
# defines name, name=, age, age=, ...
attr_accessor :name, :age, :blood_type
end
Here is a ruby forum that talks about this. I also highly recommend reading Metaprogramming Ruby. It has many goodies like 3 or 4 ways to solve your problem as well as many other techniques to reduce duplication.

Is there a Rails 3 replacement for subclasses_of?

I am trying to get all derived classes for a specific type. In Rails 2+, you can say:
Class.subclasses_of SomeClass
This is because activesupport/lib/active_support/core_ext/object/extending.rb is gone in Rails 3.
Is there a replacement in Rails 3? Are there alternatives.
If you want to get all the subclasses of SomeClass, then you do:
SomeClass.descendants
Likewise, if you want to find out the parent classes, use:
SomeClass.ancestors
This will pass you back an Array for your consumption. Thus allowing you to:
SomeClass.descendants.each { |klass| }
There's always the option to search the ObjectSpace - something like:
classes = []
ObjectSpace.each_object(Class) do |klass|
SomeClass > klass
end
Which is a little wonky but useful. With more context, maybe there's a better approach?
Perhaps I misunderstand your question. I never used the subclasses_of method in Rails 2. In Rails 3 I just use Foo.descendants which provides an array of classes (and their attributes) that inherit from Foo. Very handy.

How to watch a ruby class for method calls? (e.g. dependency injection, hooks, callbacks)

I have a Populater class for populating a database with random data. I would like to measure the populator's run method with the rtui progress bar. But instead of calling the progress bar object inside the Populater class, I would like to have a third class to increment the progress bar when certain methods in the Populater class are called. The code I have in mind is like this:
#populator = Populator.new
#pb = ProgressBar.new
Watcher.watch(#populator, :before, :add_record) do
#pb.subject = "Adding a record..."
#pb.inc
end
Watcher would call the watch block before executing #populator.add_record.
What's the best way to implement this?
You might want to have a look at RCapture, a Ruby library that allows placing hooks on methods using a simple and consistent interface.
Christoph
I'd recommend you look at the observer pattern. I'd avoid fancy dynamic tricks before they are really necessary, the observer pattern is simple and pretty well understood.
Also checkout the Observable class in ruby's stdlib.
Wouldn't it just work to do this:
def watch(populator, when, dowhat)
yield
populator.add_record
end
Hope this helps...

Resources