Module: D
- Defined in:
- lib/rub/d.rb
Overview
Definitions Namespace.
Constant Summary collapse
- @@map =
{}
Class Method Summary collapse
-
.[](k) ⇒ Object
Retrieve a defined value.
-
.append(k, v = nil) ⇒ String
Append a configuration option onto a value.
-
.define(k, v = nil) ⇒ String
(also: []=)
Define a configuration option.
- .fromFile(fn) ⇒ Object deprecated Deprecated.
-
.map ⇒ Object
Return the configuration map.
-
.pp ⇒ Object
Pretty Print the configuration options.
-
.prepend(k, v = nil) ⇒ String
Prepend a configuration option onto a value.
-
.resolve_path(k, default = nil) ⇒ Object
Resolve a path.
Class Method Details
.[](k) ⇒ Object
Retrieve a defined value.
190 191 192 |
# File 'lib/rub/d.rb', line 190 def self.[] (k) @@map[k.to_sym] end |
.append(k, v = nil) ⇒ String
Append a configuration option onto a value.
If v
is non-nil k
is the key and v
is the value. If v
is nil k must be a string and it is parsed to find the key and value.
If there is a ‘=’ in the string everything before the first ‘=’ is used as the key and everything after the value. If the key ends is ‘+’ it is dropped. If there is no ‘=’ k
is used as the key and true
as the value.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/rub/d.rb', line 120 def self.append(k, v=nil) if v == nil k, f, v = k.partition '=' if k.end_with?('+') and not f.empty? k = k[0..-2] end end k = k.to_sym if f == '' v = true end @@map[k].is_a?(Array) or @@map[k] = [] @@map[k].push(v) end |
.define(k, v = nil) ⇒ String Also known as: []=
Define a configuration option.
If v
is non-nil k
is the key and v
is the value. If v
is nil k must be a string and it is parsed to find the key and value.
-
If there is an ‘=’ in
k
the first one is used.-
If the ‘=’ is proceeded by a ‘+’ everything before the “+=” is used as the key and everything after is used as the value. These are then passed are passed onto #push.
-
Otherwise everything before the ‘=’ is used as the key and everything after as the value.
-
If there is no ‘=’ all of
k
is used as the key and the value istrue
.
-
The key will be converted into a symbol and the k/v pair will be added to the configuration options.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rub/d.rb', line 69 def self.define(k, v=nil) if v == nil k, f, v = k.partition '=' if k.end_with?('+') and not f.empty? return append(k[0..-2], v) elsif k.end_with?('^') and not f.empty? return prepend(k[0..-2], v) end end k = k.to_sym if f == "" v = true end @@map[k] = v end |
.fromFile(fn) ⇒ Object
Read definitions from a file.
These are read one-per-line and passed to #define (as one argument).
216 217 218 |
# File 'lib/rub/d.rb', line 216 def self.fromFile(fn) File.open(fn) {|f| f.each_line {|l| define(l.chomp) } } end |
.map ⇒ Object
Return the configuration map.
This is intended for debugging only and may be removed/made private any time.
See: #pp
200 201 202 |
# File 'lib/rub/d.rb', line 200 def self.map return @@map end |
.pp ⇒ Object
Pretty Print the configuration options.
Useful for debugging.
207 208 209 |
# File 'lib/rub/d.rb', line 207 def self.pp pp map end |
.prepend(k, v = nil) ⇒ String
Prepend a configuration option onto a value.
If v
is non-nil k
is the key and v
is the value. If v
is nil k must be a string and it is parsed to find the key and value.
If there is a ‘=’ in the string everything before the first ‘=’ is used as the key and everything after the value. If the key ends is ‘^’ it is dropped. If there is no ‘=’ k
is used as the key and true
as the value.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/rub/d.rb', line 167 def self.prepend(k, v=nil) if v == nil k, f, v = k.partition '=' if k.end_with?('^') and not f.empty? k = k[0..-2] end end k = k.to_sym if f == '' v = true end @@map[k].is_a?(Array) or @@map[k] = [] @@map[k].unshift(v) end |
.resolve_path(k, default = nil) ⇒ Object
Resolve a path.
This makes a passed in path proper. This function must be used in order to make paths passed in on the command line proper. This makes all paths relative to the directory where the command was executed. If the definition was not provided it is set to default, no path resolution is done on the default value.
230 231 232 233 234 235 236 237 238 |
# File 'lib/rub/d.rb', line 230 def self.resolve_path(k, default=nil) k = k.to_sym @@map[k] = if @@map[k] != nil Pathname.new(@@map[k]).(R::Env.cmd_dir) else default end end |