Sha256: 664bb8a9faa4c5a1f57cd6c260e529f256a08ad48c466543bc962067c7834d35

Contents?: true

Size: 1.42 KB

Versions: 9

Compression:

Stored size: 1.42 KB

Contents

# Copyright (c) 2012-2013 Stark & Wayne, LLC

require "settingslogic"
module Bosh; module Bootstrap; module Helpers; end; end; end

# Set a nested setting with "key1.key2.key3" notation
#
# Assumes +settings+ contains the settings
module Bosh::Bootstrap::Helpers::SettingsSetter

  # Let's you navigate to a nested setting, do something with it,
  # and then saves any changes to settings.
  # Usage:
  #   with_setting "inception" { |s| s["host"] = "1.2.3.4" }
  #   with_setting "a.b.c" { |s| s["value"] = "1.2.3.4" }
  def with_setting(nested_key, &block)
    target_settings_field = settings
    settings_key_portions = nested_key.split(".")
    parent_key_portions, final_key = settings_key_portions[0..-2], settings_key_portions[-1]
    parent_key_portions.each do |key_portion|
      target_settings_field[key_portion] ||= {}
      target_settings_field = target_settings_field[key_portion]
    end

    target_settings_field[final_key] ||= {}
    yield target_settings_field[final_key]
    save_settings!
  end

  def setting(nested_key, value)
    target_settings_field = settings
    settings_key_portions = nested_key.split(".")
    parent_key_portions, final_key = settings_key_portions[0..-2], settings_key_portions[-1]
    parent_key_portions.each do |key_portion|
      target_settings_field[key_portion] ||= {}
      target_settings_field = target_settings_field[key_portion]
    end
    target_settings_field[final_key] = value
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
bosh-bootstrap-0.10.2 lib/bosh-bootstrap/helpers/settings_setter.rb
bosh-bootstrap-0.10.1 lib/bosh-bootstrap/helpers/settings_setter.rb
bosh-bootstrap-0.10.0 lib/bosh-bootstrap/helpers/settings_setter.rb
bosh-bootstrap-0.9.0 lib/bosh-bootstrap/helpers/settings_setter.rb
bosh-bootstrap-0.8.2 lib/bosh-bootstrap/helpers/settings_setter.rb
bosh-bootstrap-0.8.1 lib/bosh-bootstrap/helpers/settings_setter.rb
bosh-bootstrap-0.8.0 lib/bosh-bootstrap/helpers/settings_setter.rb
bosh-bootstrap-0.7.1 lib/bosh-bootstrap/helpers/settings_setter.rb
bosh-bootstrap-0.7.0 lib/bosh-bootstrap/helpers/settings_setter.rb