Programming problems

From WhyNotWiki

Jump to: navigation, search

Programming problems  edit   (Category  edit)


[edit] [Open problems (category)] Disable a certain option during a single method call

save without ___ feature

  • ActiveRecord
    • save without validations

execute command ignoring dry-run feature

  • In svn-command, I ended up setting $ignore_dry_run_option=true before some (read-only) svn command calls that I wanted to be executed even if the user had specified that they wanted to do a dry run (no command calls).
    • (Why? Because the dry-run command is used to see what commands would have been executed normally, without actually running them, but for some of the commands, it is necessary to make another svn call in the background just to build the main command string. So without making a real command call, we couldn't tell the user what main command would have been executed. And it might even die otherwise because it gets back nil instead of a string like it's expecting...)

[edit] How

refactor the method into two: one version with the feature and one without

For example, a save and a save_without_validations, where the save version does validations before calling save_without_validations ...

I imagine that isn't always the best design (adds unnecessary duplication, for one thing), if in fact it's always even possible.

pass extra arguments to method??

  • For example, model.save!(:validations => false).
  • Bad. Clutters up the method signature with options that you actually want to be per-object or per-class -- options that are typically not per-method-call. Plus, we don't want to duplicate the formal argument specification or the logic to handle those arguments to a bunch of methods!

globals ??

$feature_a = false
method_call()
$feature_a = true

Yuck!

blocks??

without_feature(:feature_name) { method_call }

Better...

Still, how would the block version be implemented?

Personal tools