Module: D

Defined in:
lib/rub/d.rb

Overview

Definitions Namespace.

Constant Summary collapse

@@map =
{}

Class Method Summary collapse

Class Method Details

.[](k) ⇒ Object

Retrieve a defined value.

Parameters:

  • k (Symbol, String)

    The key.



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.

Examples:

D.append('k1', 'v1')
D[:k1] #=> ["v1"]
D.append('k1', 'v2')
D[:k1] #=> ["v1", "v2"]
D.append('k2=v3')
D.append('k2+=v4')
D.append('k2+=')
D[:k2] #=> ["v3", "v4", ""]

D.append('w1+')
D[:w1]   #=> nil
D['w1+'] #=> [true]

Parameters:

  • k (String)

    The key, or if v is nil a string to parse.

  • v (String, nil) (defaults to: nil)

    The value

Returns:

  • (String)

    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 is true.

The key will be converted into a symbol and the k/v pair will be added to the configuration options.

Examples:

D.define('k1', 'v1')
D[:k1] #=> "v1"
D.define('k2=v2')
D[:k2] #=> "v2"
D.define('k3')
D[:k3] #=> true
D.define('k4')
D[:k4] #=> true
D.define('k4=')
D[:k4] #=> ""
D.define('k5+=v5')
D.define('k5+=v6')
D[:k5] #=> ["v5", "v6"]

D.define('w1=v1=v2')
D[:w1] #=> "v1=v2"
D.define('w2=v2+=v3')
D[:w2] #=> "v2+=v3"

Parameters:

  • k (String)

    The key, or if v is nil a string to parse.

  • v (String, nil) (defaults to: nil)

    The value

Returns:

  • (String)

    The value.



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

Deprecated.

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

.mapObject

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

.ppObject

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.

Examples:

D.prepend('k1', 'v1')
D[:k1] #=> ["v1"]
D.prepend('k1', 'v2')
D[:k1] #=> ["v2", "v1"]
D.prepend('k2=v3')
D.prepend('k2^=v4')
D.prepend('k2+=')
D[:k2] #=> ["", "v4", "v3",]

D.prepend('w1^')
D[:w1]   #=> nil
D['w1^'] #=> [true]

Parameters:

  • k (String)

    The key, or if v is nil a string to parse.

  • v (String, nil) (defaults to: nil)

    The value

Returns:

  • (String)

    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.

Parameters:

  • k (Symbol, String)

    The key of the option.

  • default (Object) (defaults to: nil)

    The value to use if k is not set.



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]).expand_path(R::Env.cmd_dir)
	else
		default
	end
end