On Sunday, April 29, 2012 3:25:48 PM UTC+1, Peter wrote:
> Hi KW,
> On Apr 29, 4:30 am, Krzysztof Wilczynski
> <krzysztof.wilczyn...@linux.com> wrote:
> > Hi,
> > > I was hoping that I could just simply do a:
> > > validate_in($::{ensure}, $::{valid_ensure_values})
> > How would that be different than using the "in" in your if statement? :)
> > Or, what is wrong with using if? Unless I am missing something? :)
> > KW
> Nothing is wrong with using if statements per-say.
> To put my problem slightly differently, in the module I am working on
> I have have five checks that would benefit by having the helper
> function (it would be cleaner, easier to maintain and easier to read).
> Also I hope that by doing the check purely in ruby it would be
> slightly faster to process rather then return a couple of times.
> To give you an example within my module in the param's file I have:
> $valid_package_ensure_values = [ "present", "installed",
> "latest", "absent", "purged", "held" ]
> $valid_service_ensure_values = [ "running", "true",
> "stopped", "false" ]
> $valid_service_enable_values = [ "true", "false", "manual" ]
> $valid_module_config_values = [ "file", "template" ]
> $valid_module_tpml_loc_values = [ "module", "site" ]
> If I had the validate_in helper function and used the other validate
> functions in the stdlib the validation code would look like
> (extracted):
> ------------------------------------------
> class module::server::base ( $package_ensure = undef,
> $service_ensure = undef,
> $service_enable = undef,
> $module_config = undef,
> $template_loc = undef,
> $use_storeconfig = undef,
> $proxy_port = undef,
> $cache_dir = undef,
> $log_dir = undef,
> $cache_net_adr = undef
> ) {
> include module::params
> validate_in(${package_ensure}, ${valid_package_ensure_values})
> validate_in(${service_ensure}, ${valid_service_ensure_values})
> validate_in(${service_enable}, ${valid_service_enable_values})
> validate_in(${module_config}, ${valid_module_config_values})
> validate_in(${template_loc}, ${valid_module_tpml_loc_values})
> is_integer($proxy_port)
> validate_absolute_path($cache_dir, $log_dir)
> validate_bool($use_storeconfig)
> # start using the values in code below
> ------------------------------------------
> The above code is very easy to read and you could see at a glance
> exactly what is happening. Also if the error message follows the
> format described in my previous message debugging would be very
> simple. I could see myself using the same pattern in other modules I
> develop.
> Bottom line instead of the current 35 lines of validation code I
> have, I could replace it with the above 8 lines which would do the
> same amount of work (4:1 ratio)!
> Funnily enough after I sent the original email I stumbled on
> puppetlabs-stdlib/lib/puppet/parser/functions/member.rb which is
> almost exactly what I am looking for, I notice that you wrote it ;)
> Thoughts?