Class: O
- Inherits:
-
Hash
- Object
- Hash
- O
- 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
-
#_data ⇒ Object
readonly
Returns the value of attribute _data.
Class Method Summary collapse
-
.from_hash(hash) ⇒ Object
convert <#Hash> to <#O>.
-
.load(name) ⇒ O
load a configuration file, support PATH, and ‘~/.gutenrc’.
-
.relative_load(name) ⇒ O
relative load a configuration file.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #_replace(data) ⇒ Object
-
#initialize(default = nil, &blk) ⇒ O
constructor
A new instance of O.
- #inspect ⇒ Object (also: #to_s)
- #method_missing(method, *args, &blk) ⇒ Object
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &blk) ⇒ Object
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
#_data ⇒ Object (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>
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
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.(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
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 |
#inspect ⇒ Object 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 |