Ask Your Question
0

How to use concatenated default parameters

asked 2014-10-20 08:08:17 -0600

neil gravatar image

I have lots of parameters depending on others. Like this:

class myproject::params {
  $path       = '/'
  $path_js    = "${path}/js"
}

class myproject (
  $path       = $::myproject::params::path,
  $path_js    = $::myproject::params::path_js,
) inherits ::myproject::params {
  #...
}

If I change $path to ~/neil/myproject, $path_js keeps '/js'.

I want to retain the possibility to define $path_js separately (eq: to development files).

I want to avoid to define $path_js relative $path.

How can i achieve this behavior?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2014-10-23 00:00:05 -0600

cbarbour gravatar image

This is the normal approach to resolving your problem:

class myproject::params {
  $path       = '/'
  $path_js    = '/js'
}

class myproject (
    $path       = $::myproject::params::path,
    $path_js    = undef,
) inherits ::myproject::params {

  if $::myproject::path_js {
    $path_js_real = $::myproject::path_js
  } else {
    $path_js_real = $::myproject::params::path_js
  }

  # Code goes here

}

You cannot use one parameter as an interpolation token in another parameter in the same class. E.g. this doesn't work:

class test (
  $foo = $bar,
  $bar = 'zapp',
  $baz = $bar,

) {
  alert($foo)
  alert($baz)
}

include test

This output is produced:

$ puppet apply ./test.pp
Alert: Scope(Class[Test]):
Alert: Scope(Class[Test]):
Notice: Compiled catalog for localhost in environment production in 0.02 seconds
Notice: Finished catalog run in 0.01 seconds
edit flag offensive delete link more

Comments

As an aside, this approach is probably better, as it more clearly communicates your intent: if $::myproject::path_js { $path_js_real = $::myproject::path_js } else { $path_js_real = "${::myclass::path}/js } You can remove the param for path_js with this approach.

cbarbour gravatar imagecbarbour ( 2014-10-23 15:26:37 -0600 )edit
0

answered 2014-10-20 13:47:15 -0600

bytwocoffee gravatar image

updated 2014-10-20 15:22:53 -0600

You could make $path_js an optional parameter and inside your myproject::params class, you can calculate the value of $path_js only if it was not included, that is, it was equal to undef.

edit flag offensive delete link more

Comments

Thanks. Do you have an example? This results in: `Error: Cannot reassign variable path_js`

neil gravatar imageneil ( 2014-10-21 00:52:00 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-10-20 08:08:17 -0600

Seen: 53 times

Last updated: Oct 23 '14