Ask Your Question
0

How to declare a selectable hash class parameter?

asked 2015-04-03 16:36:05 -0500

updated 2015-04-04 09:45:53 -0500

Salut,

I need a selectable hash parameter in my class definition:

class colors (
  $flavor      = 'myfavorites',
  $nice_colors = $flavor ? {
    'myfavorites' => {
      'red'   => false,
      'green' => true,
      'blue'  => false,
    },
    default => {
      'red'   => true,
      'green' => true,
      'blue'  => true,
    },
  },
) inherits colors::params {
  Class['colors::install'] -> Class['colors::config']
  contain 'colors::install'
  contain 'colors::config'
}

Since hash literals can't be specified in a '?'-selector I need to go around this problem.

Later I need to iterate over the hash in a template.

<%- @nice_colors.each do |color, isnice| -%>
<%- if not isnice %>no<% end %><%= color %> <%# -%>
<%- end -%>

Does anyone have an idea (and when yes, what's your idea?)?

Thx a lot.

edit retag flag offensive close merge delete

Comments

I'd like to keep my class independent of any other module (even stdlib, if possible). Gain “extra points” by giving a hint, how to place a space only between two words and not behind every word like my template above does.

Kai Burghardt gravatar imageKai Burghardt ( 2015-04-03 16:45:53 -0500 )edit

Yeah, and don't tell me „Go use the future parser.“, „The future parser's gonna solve all your problems.“ Indeed I found a way to solve my problem but enabling the future parser is only a sub-optimal way. Especially for a production environment.

Kai Burghardt gravatar imageKai Burghardt ( 2015-04-03 20:27:12 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2015-04-03 20:48:57 -0500

updated 2015-04-04 20:28:42 -0500

class colors (
  $flavor      = 'myfavorites',
  $nice_colors = undef,
) inherits colors::params {
  Class['colors::install'] -> Class['colors::config']
  contain 'colors::install'
  contain 'colors::config'
}
class colors::config {
  file {'/tmp/colors':
    ensure  => file,
    content => template('colors/colors.erb')
  }

  if ! $::colors::nice_colors {
    case $::colors::flavor {
      'myfavorites': {
        $nice_colors = {
          'red'   => false,
          'green' => true,
          'blue'  => false,
        }
      }
      default: {
        $nice_colors = {
          'red'   => true,
          'green' => true,
          'blue'  => true,
        }
      }
    }
  } else {
    $nice_colors = $::colors::nice_colors
  }
}
<%- scope.lookupvar('nice_colors').each do |color, isnice| -%>
<%- if not isnice %>no<% end %><%= color %> <%# -%>
<%- end -%>

looks kinda dirty to me

edit flag offensive delete link more

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: 2015-04-03 16:26:45 -0500

Seen: 578 times

Last updated: Apr 04 '15