Class: O

Inherits:
Hash
  • Object
show all
Defined in:
lib/o.rb

Overview

internal: store data in a Hash, the key of the Hash is always converted to symbol.

Defined Under Namespace

Classes: O_Eval

Constant Summary collapse

PATH =

PATH for O.load

[]
Error =
Exception.new
LoadError =
Exception.new(Error)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default = nil, &blk) ⇒ O

Returns a new instance of O.



110
111
112
113
114
115
116
117
# File 'lib/o.rb', line 110

def initialize default=nil, &blk
	@_data = Hash.new(default)
	if blk
		o_eval = O_Eval.new
		o_eval.instance_eval &blk
		@_data.merge!(o_eval._data)
	end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object

_method goes to @_data.send(_method, ..) method? #=> !! @_data method #=> @_data method=value #=> @_data=value



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/o.rb', line 147

def method_missing method, *args, &blk
	if method =~ /(.*)=$/
		@_data[$1.to_sym] = args[0]
	elsif method =~ /(.*)\?$/
		!! @_data[$1.to_sym]
	elsif method =~ /^_(.*)/
		@_data.send($1.to_sym, *args, &blk)
	else
		@_data[method]
	end
end

Instance Attribute Details

#_dataObject (readonly)

Returns the value of attribute _data.



108
109
110
# File 'lib/o.rb', line 108

def _data
  @_data
end

Class Method Details

.from_hash(hash) ⇒ Object

convert <#Hash> to <#O>

Parameters:

  • hash (hash)

Returns:

  • O



29
30
31
32
# File 'lib/o.rb', line 29

def from_hash hash
	o = O.new
	o._replace hash
end

.load(name) ⇒ O

load a configuration file, support PATH, and ‘~/.gutenrc’

first try name.rb, then use name

Examples:

option = O.load("~/.gutenrc")

O::Path << "/home"
option = O.load("guten")  #=> try guten.rb; then try guten
option = O.load("guten.rb")

Parameters:

  • name (String)

Returns:

  • (O)

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/o.rb', line 48

def load name
	path = nil
	if name =~ /^~/
		file = File.expand_path(name)
		path = file if File.exists?(file)
	else
		catch :break do
			PATH.each  do |p|
				['.rb', ''].each {|ext|
					file = File.join(p, name+ext)
					if File.exists? file
						path = file
						throw :break
					end
				}
			end
		end
	end

	raise LoadError, "can't find file -- #{name}" unless path

	eval_file path
end

.relative_load(name) ⇒ O

relative load a configuration file

Parameters:

  • name (String)

Returns:

  • (O)

    option

Raises:

See Also:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/o.rb', line 77

def relative_load name
	pd caller if $TEST
	a,file, line, method = caller[0].match(/^(.*):(\d+):.*`(.*)'$/).to_a
	raise LoadError, "#{type} is called in #{file}" if file=~/\(.*\)/ # eval, etc.

	file = File.readlink(file) if File.symlink?(file)

	path = nil
	[".rb", ""].each do |ext|
		f = File.absolute_path(File.join(File.dirname(file), name+ext))
		if File.exists?(f)
			path = f
			break
		end
	end

	raise LoadError, "can't find file -- #{name}" unless path

	eval_file path
end

Instance Method Details

#+(other) ⇒ Object



127
128
129
# File 'lib/o.rb', line 127

def + other
	O.new(@_data, other._data)
end

#[](key) ⇒ Object



123
124
125
# File 'lib/o.rb', line 123

def [] key
	@_data[key.to_sym]
end

#[]=(key, value) ⇒ Object



119
120
121
# File 'lib/o.rb', line 119

def []= key, value
	@_data[key.to_sym] = value
end

#_replace(data) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/o.rb', line 131

def _replace data
	case data
	when Hash
		@_data = data
	when O
		@_data = data._data
	end
	self
end

#inspectObject Also known as: to_s



159
160
161
162
163
164
165
# File 'lib/o.rb', line 159

def inspect 
	rst = "<#O "
	@_data.each do |k,v|
		rst << "#{k}:#{v.inspect} "
	end
	rst << " >" 
end