Class: O

Inherits:
Object
  • Object
show all
Includes:
HashMethodFix, Semantics
Defined in:
lib/o.rb,
lib/o/parser.rb,
lib/o/version.rb,
lib/o/semantics.rb,
lib/o/hash_method_fix.rb

Overview

<#O> is a node, it has _child, _parent and _root attribute.

Rc = O do
  a.b 1
  a.c do
    d 2
  end
end

p Rc
#=> <#O
   :a => <#O
     :b => 1
     :c => <#O
       :d => 2>>>

Rc.a #=> <#O>
Rc.a._child #=> {:b => 1, :c => <#O>}
Rc.a._parent #=> is Rc
Rc.a._root #=> is Rc

Rc._parent #=>  nil
Rc._root #=> is Rc

Defined Under Namespace

Modules: HashMethodFix, Semantics, VERSION Classes: Parser

Constant Summary collapse

Error =
Class.new Exception
LoadError =
Class.new Error
BUILTIN_METHODS =
[ :p, :raise, :sleep, :rand, :srand, :exit, :require, :at_exit, :autoload, :open]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HashMethodFix

#_merge, #_merge!

Methods included from Semantics

#no, #yes

Constructor Details

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

Returns a new instance of O.

Parameters:

  • (nil) (Object)

    default create a new hash with the defalut value



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/o.rb', line 143

def initialize(default=nil, options={}, &blk)
  @_root = options[:_root]
  @_child = Hash.new(default)

  if blk
    method = _blk2method(&blk)
    if blk.arity == 0
      method.call
    else
      method.call self
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

everything goes here.

.name? 
.name= value 
.name value 
._name

.c 
.a.b.c


232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/o.rb', line 232

def method_missing(name, *args, &blk)
  # path: root
  if name == :_
    return _root

  # relative path: __
  elsif name =~ /^__+$/
    num = name.to_s.count('_') - 1
    node = self
    num.times {
      return unless node
      node = node._parent
    }
    return node

  # .name=
  elsif name =~ /(.*)=$/
    return @_child[$1.to_sym] = args[0]

  # ._name
  elsif name =~ /^_(.*)/
    name = $1.to_sym
    args.map!{|arg| O===arg ? arg._child : arg} 
    return @_child.send(name, *args, &blk)

  # .name?
  elsif name =~ /(.*)\?$/
    return !! @_child[$1.to_sym]

  elsif Proc === @_child[name]
    return @_child[name].call *args

  # a.c  # return data if has :c
  # a.c  # create new <#O> if no :c 
  #
  elsif args.empty?

    # a.b.c 1
    # a.b do
    #   c 2
    # end
    if @_child.has_key?(name)
      o = @_child[name]
      o.instance_eval(&blk) if blk
      return o

    else
      next_o = O.new(nil, {_root: _root})
      next_o._parent = self
      self._child[name] = next_o
      next_o.instance_eval(&blk) if blk
      return next_o
    end

  # .name value
  else
    @_child[name] = args[0]
    return args[0]
  end
end

Instance Attribute Details

#_childObject

child node, a hash data



137
138
139
# File 'lib/o.rb', line 137

def _child
  @_child
end

#_parentObject

parent node, a <#O>



134
135
136
# File 'lib/o.rb', line 134

def _parent
  @_parent
end

#_rootObject

root node, a <#O>



140
141
142
# File 'lib/o.rb', line 140

def _root
  @_root
end

Class Method Details

.[](data) ⇒ O

convert Hash, O to O

Parameters:

  • data (O, Hash)

Returns:

  • (O)


59
60
61
62
63
64
65
66
67
68
# File 'lib/o.rb', line 59

def [](data)
  case data
  when O
    data
  when Hash
    o = O.new
    o._child = data
    o
  end
end

.eval(content = nil, &blk) ⇒ O

eval a file/string configuration.

Returns:

  • (O)

    configuration



49
50
51
52
53
54
# File 'lib/o.rb', line 49

def eval(content=nil, &blk)
  o = O.new nil
  content ? o.instance_eval(Parser.compile(content)) : o.instance_eval(&blk)

  o._root
end

.get(obj) ⇒ Hash

get Hash data from any object

Parameters:

  • obj (O, Hash)

Returns:

  • (Hash)


74
75
76
77
78
79
80
81
# File 'lib/o.rb', line 74

def get(obj)
  case obj
  when Hash
    obj
  when O
    obj._child
  end
end

.require(name) ⇒ O

load a configuration file, use $: and support ‘~/.gutenrc’

Examples:

Rc = O.require("~/.gutenrc")

Rc = O.require("/absolute/path/rc.rb")

Rc = O.require("guten/rc") #=> load 'APP/lib/guten/rc.rb'
# first try 'guten/rc.rb', then 'guten/rc'

Parameters:

  • name (String)

Returns:

  • (O)

Raises:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/o.rb', line 96

def require(name)
  path = nil

  # ~/.gutenrc
  if name =~ /^~/
    file = File.expand_path(name)
    path = file if File.exists?(file)

  # /absolute/path/to/rc
  elsif File.absolute_path(name) == name
    path = name if File.exists?(name)

  # relative/rc
  else
    catch :break do
      $:.each { |p|
        ['.rb', ''].each { |ext|
          file = File.join(p, name+ext)
          if File.exists? file
            path = file
            throw :break
          end
        }
      }
    end
  end

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

  O.eval File.read(path)
end

Instance Method Details

#+(other) ⇒ Object

Raises:



216
217
218
219
220
# File 'lib/o.rb', line 216

def +(other)
  raise Error, "not support type for + -- #{other.inspect}" unless O === other

  O.new _child, other._child
end

#==(other) ⇒ Object



192
193
194
# File 'lib/o.rb', line 192

def ==(other)
  _child == other._child
end

#[](key) ⇒ Object

get data



186
187
188
189
190
# File 'lib/o.rb', line 186

def [](key)
  key = key.respond_to?(:to_sym) ? key.to_sym : key

  @_child[key]
end

#[]=(key, value) ⇒ Object

set data



179
180
181
182
183
# File 'lib/o.rb', line 179

def []=(key, value)
  key = key.respond_to?(:to_sym) ? key.to_sym : key

  @_child[key] = value
end

#_dupO

duplicate

Returns:

  • (O)

    new <#O>



199
200
201
202
203
204
# File 'lib/o.rb', line 199

def _dup
  o = O.new
  o._child = _child.dup

  o
end

#_replace(obj) ⇒ O

replace with a new data

Parameters:

  • obj (Hash, O)

Returns:

  • (O)

    self



210
211
212
213
214
# File 'lib/o.rb', line 210

def _replace(obj)
  self._child = O.get(obj)

  self
end

#_temp(&blk) ⇒ Object

a temporarily change



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

def _temp(&blk)
  data = _child.dup
  blk.call
  self._child = data

  self
end

#inspect(indent = " ") ⇒ Object Also known as: to_s

pretty print

<#O 
  :b => 1
  :c => 2
  :d => <#O
    :c => 2>>


300
301
302
303
304
305
306
307
308
309
310
# File 'lib/o.rb', line 300

def inspect(indent="  ")
  o={rst: ""}
  o[:rst] << "<#O\n"
  _child.each { |k,v|
    o[:rst] << "#{indent}#{k.inspect} => "
    o[:rst] << (O === v ? "#{v.inspect(indent+"  ")}\n" : "#{v.inspect}\n")
  }
  o[:rst].rstrip! << ">"

  o[:rst]
end