Class: NanoSystem

Inherits:
Object show all
Defined in:
lib/nanosys.rb

Overview

The NanoSystem module provides meta-information on all nano-methods.

Constant Summary collapse

REQUIRE_DIR =
'nano'
DATA_DIR =
File.join( Config::CONFIG['datadir'], 'site_ruby/nano' )

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNanoSystem

Returns a new instance of NanoSystem.



55
56
57
58
59
# File 'lib/nanosys.rb', line 55

def initialize
  Object.send( :require, 'yaml' )
  Object.send( :require, 'rbconfig' )
  load
end

Instance Attribute Details

#baseObject (readonly)

instance_level



53
54
55
# File 'lib/nanosys.rb', line 53

def base
  @base
end

#fullObject (readonly)

instance_level



53
54
55
# File 'lib/nanosys.rb', line 53

def full
  @full
end

#noautoObject (readonly)

instance_level



53
54
55
# File 'lib/nanosys.rb', line 53

def noauto
  @noauto
end

#unsafeObject (readonly)

instance_level



53
54
55
# File 'lib/nanosys.rb', line 53

def unsafe
  @unsafe
end

Class Method Details

.__new__Object



39
# File 'lib/nanosys.rb', line 39

alias __new__ new

.method_missing(meth, *args, &blk) ⇒ Object



45
46
47
48
# File 'lib/nanosys.rb', line 45

def method_missing( meth, *args, &blk )
  @instance ||= __new__
  @instance.send( meth, *args, &blk )
end

.newObject

Raises:

  • (NoMethodError)


40
41
42
# File 'lib/nanosys.rb', line 40

def new
  raise NoMethodError, 'cannot instantiate auto-singleton class'
end

Instance Method Details

#[](classname) ⇒ Object

Returns a nano method lookup hash for given classname.



129
130
131
# File 'lib/nanosys.rb', line 129

def []( classname )
  index[classname.to_s.downcase]
end

#classesObject Also known as: modules

Returns list of class/module names with available nano methods.



135
136
137
# File 'lib/nanosys.rb', line 135

def classes
  index.keys.sort
end

#each(&blk) ⇒ Object

iterate through classes { |classname, methodhash| … }



124
125
126
# File 'lib/nanosys.rb', line 124

def each( &blk )
  self.index.each &blk
end

#files(classname = nil) ⇒ Object

# Returns a list of all nano method names for a given class/module name.

# If no class/module name is give returns a list of all nano methods
# in the format of '<classname>#<methname>' for instance methods, and
# '<classname>.<methname>' for class/module methods.
def method_list( classname=nil )
  lst = nil
  if classname
    lst = store[classname.to_s].keys if store.include? classname.to_s
  else
    lst = []
    store.each { |classname, methmap|
      methmap.each { |methname, path|
        md = /^(self|#{classname})[.]/.match methname
        lst << ( md ? "#{classname}.#{md.post_match}" : "#{classname}##{methname}" )
      }
    }
  end
  lst.sort
end


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/nanosys.rb', line 160

def files( classname=nil )
  lst = []
  if classname
    index[classname.to_s.downcase].each { |methname|
      lst << "#{classname}/#{methname}"
    }
  else
    index.each { |classname, methlist|
      methlist.each { |methname|
        lst << "#{classname}/#{methname}"
      }
    }
  end
  lst.uniq.sort
end

#indexObject

top level storage



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

def index
  @index
end

#loadObject

Load nano data



62
63
64
65
# File 'lib/nanosys.rb', line 62

def load
  load_index
  load_info
end

#require(klass, *meths) ⇒ Object

Require method nano(s) for given class/module.



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/nanosys.rb', line 178

def require( klass, *meths )
  classname = klass.name.downcase
  raise "unknown class #{klass}" unless self.index.has_key?( classname )
  success = true
  meths.each do |m|
    raise "unknown nano-method #{m}" unless self[ classname ].include?( m.to_s )
    f = "#{NanoSystem::REQUIRE_DIR}/#{classname}/#{m}"
    r = Object.send( :require, f )
    success = success && r
  end
  success
end