Class: Babushka::Parameter

Inherits:
Object show all
Defined in:
lib/babushka/parameter.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Parameter) initialize(name, value = nil)

A new instance of Parameter



9
10
11
12
# File 'lib/babushka/parameter.rb', line 9

def initialize(name, value = nil)
  @name = name
  @value = value
end

Instance Attribute Details

- (Object) name (readonly)

Returns the value of attribute name



3
4
5
# File 'lib/babushka/parameter.rb', line 3

def name
  @name
end

Class Method Details

+ (Object) for(name, value = nil)



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

def self.for name, value = nil
  value.is_a?(Parameter) ? value : Parameter.new(name, value)
end

Instance Method Details

- (Object) /(other)



89
90
91
# File 'lib/babushka/parameter.rb', line 89

def / other
  value / other
end

- (Object) ==(other)



85
86
87
# File 'lib/babushka/parameter.rb', line 85

def == other
  value == other
end

- (Object) [](other)



93
94
95
# File 'lib/babushka/parameter.rb', line 93

def [] other
  value[other]
end

- (Object) ask(value)

If #ask is set, then when this parameter lazily prompts for a value, it will pass #ask to Prompt as the message. That is, Prompt will use this string, ending it with a question mark, as the question it shows to the user to give them information about the value it's requesting.

For example, the deps that install babushka itself use a :path parameter. if #ask wasn't specified, it would prompt the user with just 'path?', which isn't very clear. Instead, #ask is set to 'Where would you like babushka installed', which, along with the default value, makes it clear to the user what they're being asked for.



59
60
61
# File 'lib/babushka/parameter.rb', line 59

def ask value
  tap { @ask = value }
end

- (Object) choose(*value)

If #choose is set, then when this parameter lazily prompts for a value, it will only accept a value that is in #choose's list.

You can pass either an array or a hash to #choose. If you pass an array of values, Prompt will only accept a value that's included in the array. If you pass a hash of => description, Prompt will only accept a value that's included in the hash's keys, printing a list of the corresponding descriptions before asking for the value.



71
72
73
74
75
76
77
78
79
# File 'lib/babushka/parameter.rb', line 71

def choose *value
  tap {
    @choose = if [[Hash], [Array]].include?(value.map(&:class))
      value.first
    else
      value
    end
  }
end

- (Object) default(value)

If #default is set, then when this parameter lazily prompts for a value, it will pass #default to Prompt as the default. That is, Prompt will return #default's value if the user just hits 'enter', if '--defaults' was passed on the commandline, or if babushka is running on a non-terminal STDIN.

This is useful for setting parameters that should be customised, or maybe just confirmed by the user, each time the dep is run.

For example, the deps that install babushka itself use a :path parameter that has #default set to '/usr/local/babushka' (unless babushka is already in the path, in which case it's a non-prompting default). This means that babushka will ask for a path when it's installed, providing '/usr/local/babushka' as the "just-hit-enter" default.



45
46
47
# File 'lib/babushka/parameter.rb', line 45

def default value
  tap { @default = value }
end

- (Object) default!(value)

If #default! is set, the parameter's value will default to it lazily when it's requested, without prompting. If the parameter is set, though (i.e. if an explicit value was supplied, say on the command line), it will override the #default! value.

This is useful for setting parameters that have a "standard" value of some kind that won't usually need to be customised.

For example, the deps that install babushka itself use a :branch parameter that has #default! set to 'master'. This means that the installation process will install the 'master' branch without asking the user to choose a branch, but a custom branch can still be specified by passing an explicit value, for example by passing 'branch=next' on the command line.



27
28
29
# File 'lib/babushka/parameter.rb', line 27

def default! value
  tap { @default_bang = value }
end

- (Object) inspect



113
114
115
# File 'lib/babushka/parameter.rb', line 113

def inspect
  "#<Babushka::Parameter:#{object_id} #{name}: #{@value || '[unset]'}>"
end

- (Object) p



97
98
99
# File 'lib/babushka/parameter.rb', line 97

def p
  value.p
end

- (Boolean) set?

Returns:

  • (Boolean)


81
82
83
# File 'lib/babushka/parameter.rb', line 81

def set?
  !!@value
end

- (Object) to_s



101
102
103
# File 'lib/babushka/parameter.rb', line 101

def to_s
  value.to_s
end

- (Object) to_str



105
106
107
108
109
110
111
# File 'lib/babushka/parameter.rb', line 105

def to_str
  if !value.respond_to?(:to_str)
    raise TypeError, "Can't coerce #{value}:#{value.class.name} into a String"
  else
    value.to_str
  end
end