Class: Babushka::Vars

Inherits:
Object show all
Includes:
LogHelpers
Defined in:
lib/babushka/vars.rb

Defined Under Namespace

Modules: Helpers

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LogHelpers

debug, deprecated!, log, log_block, log_error, log_ok, log_stderr, log_warn, removed!

Constructor Details

#initializeVars

Returns a new instance of Vars.



55
56
57
58
# File 'lib/babushka/vars.rb', line 55

def initialize
  @vars = Hashish.hash
  @saved_vars = Hashish.hash
end

Instance Attribute Details

#saved_varsObject (readonly)

Returns the value of attribute saved_vars.



5
6
7
# File 'lib/babushka/vars.rb', line 5

def saved_vars
  @saved_vars
end

#varsObject (readonly)

Returns the value of attribute vars.



5
6
7
# File 'lib/babushka/vars.rb', line 5

def vars
  @vars
end

Instance Method Details

#default_for(key) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/babushka/vars.rb', line 100

def default_for key
  if vars[key.to_s][:default].respond_to? :call
    # If the default is a proc, re-evaluate it every time.
    instance_eval { vars[key.to_s][:default].call }

  elsif saved_vars[key.to_s].has_key? :value
    # Otherwise, if there's a saved value, use that.
    saved_vars[key.to_s][:value]

  # Symbol defaults are references to other vars.
  elsif vars[key.to_s][:default].is_a? Symbol
    # Look up the current value of the referenced var.
    referenced_val = var vars[key.to_s][:default], :ask => false
    # Use the corresponding saved value if there is one, otherwise use the reference.
    (saved_vars[key.to_s][:values] ||= {})[referenced_val] || referenced_val

  else
    # Otherwise, use the default.
    vars[key.to_s][:default]
  end
end

#define_var(name, opts = {}) ⇒ Object



83
84
85
86
87
# File 'lib/babushka/vars.rb', line 83

def define_var name, opts = {}
  vars[name.to_s].update opts.slice(:default, :type, :message, :choices, :choice_descriptions)
  vars[name.to_s][:choices] ||= vars[name.to_s][:choice_descriptions].keys unless vars[name.to_s][:choice_descriptions].nil?
  vars[name.to_s]
end

#for_saveObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/babushka/vars.rb', line 89

def for_save
  vars.dup.inject(saved_vars.dup) {|vars_to_save,(var,_)|
    vars_to_save[var].update vars[var]
    save_referenced_default_for(var, vars_to_save) if vars[var][:default].is_a?(Symbol)
    vars_to_save
  }.reject_r {|var,data|
    ![String, Symbol, Hash, Numeric, TrueClass, FalseClass].include?(data.class) ||
    var.to_s['password']
  }
end

#merge(key, value) ⇒ Object



64
65
66
# File 'lib/babushka/vars.rb', line 64

def merge key, value
  set key, ((vars[key.to_s] || {})[:value] || {}).merge(value)
end

#set(key, value) ⇒ Object



60
61
62
# File 'lib/babushka/vars.rb', line 60

def set key, value
  vars[key.to_s][:value] = value
end

#var(name, opts = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/babushka/vars.rb', line 68

def var name, opts = {}
  define_var name, opts
  if vars[name.to_s].has_key? :value
    if vars[name.to_s][:value].respond_to? :call
      vars[name.to_s][:value].call
    else
      vars[name.to_s][:value]
    end
  elsif opts[:ask] != false
    ask_for_var name.to_s, opts
  else
    default_for name
  end
end