Class: Babushka::Parameter
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #/(other) ⇒ Object
- #==(other) ⇒ Object
- #[](other) ⇒ Object
-
#ask(value) ⇒ Object
If #ask is set, then when this parameter lazily prompts for a value, it will pass #ask to Prompt as the message.
-
#choose(*value) ⇒ Object
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.
- #current_value ⇒ Object
-
#default(value) ⇒ Object
If #default is set, then when this parameter lazily prompts for a value, it will pass #default to Prompt as the default.
-
#default!(value) ⇒ Object
If #default! is set, the parameter’s value will default to it lazily when it’s requested, without prompting.
- #description ⇒ Object
-
#initialize(name, value = nil) ⇒ Parameter
constructor
A new instance of Parameter.
- #inspect ⇒ Object
- #p ⇒ Object
- #set? ⇒ Boolean
- #to_a ⇒ Object
- #to_s ⇒ Object
- #to_str ⇒ Object
Constructor Details
#initialize(name, value = nil) ⇒ Parameter
Returns 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
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/babushka/parameter.rb', line 3 def name @name end |
Class Method Details
Instance Method Details
#/(other) ⇒ Object
89 90 91 |
# File 'lib/babushka/parameter.rb', line 89 def / other value / other end |
#==(other) ⇒ Object
85 86 87 |
# File 'lib/babushka/parameter.rb', line 85 def == other value == other end |
#[](other) ⇒ Object
93 94 95 |
# File 'lib/babushka/parameter.rb', line 93 def [] other value[other] end |
#ask(value) ⇒ Object
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 |
#choose(*value) ⇒ Object
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 |
#current_value ⇒ Object
117 118 119 |
# File 'lib/babushka/parameter.rb', line 117 def current_value @value end |
#default(value) ⇒ Object
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 |
#default!(value) ⇒ Object
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 |
#description ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/babushka/parameter.rb', line 125 def description if @value "#{name}: #{inspect_truncated(@value)}" elsif @default_bang "#{name}: [default!: #{inspect_truncated(@default_bang)}]" elsif @default "#{name}: [default: #{inspect_truncated(@default)}]" else "#{name}: [unset]" end end |
#inspect ⇒ Object
121 122 123 |
# File 'lib/babushka/parameter.rb', line 121 def inspect "#<Babushka::Parameter:#{object_id} #{description}>" end |
#p ⇒ Object
97 98 99 |
# File 'lib/babushka/parameter.rb', line 97 def p value.p end |
#set? ⇒ Boolean
81 82 83 |
# File 'lib/babushka/parameter.rb', line 81 def set? !!@value end |
#to_a ⇒ Object
105 106 107 |
# File 'lib/babushka/parameter.rb', line 105 def to_a value.to_a end |
#to_s ⇒ Object
101 102 103 |
# File 'lib/babushka/parameter.rb', line 101 def to_s value.to_s end |
#to_str ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/babushka/parameter.rb', line 109 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 |