class Ua::Application

Constants

SINGLETON_APP
StackFrame
TOPLEVEL

Public Class Methods

export_commands(*names) click to toggle source
# File lib/ua.rb, line 294
def self.export_commands(*names)
  that = self
  names.each{|name|  
    (Commands).send(:define_method, name) do |*a, &b|
       that.top_app.send(name, *a, &b)
    end
  }
end
new() click to toggle source
# File lib/ua.rb, line 10
def initialize
   @store   = {}
   @context = {}
end
pop() click to toggle source
# File lib/ua.rb, line 254
def self.pop
  @app_stack.pop
end
push(app = ArgVoid, context = ArgVoid, controller = ArgVoid) click to toggle source
# File lib/ua.rb, line 242
def self.push(app = ArgVoid, context = ArgVoid, controller = ArgVoid)
  app     = app     == ArgVoid ? @app_stack.last.app : app
  context = context == ArgVoid ? @app_stack.last.context : context
  controller = controller == ArgVoid ? @app_stack.last.controller : controller
  @app_stack.push StackFrame.new(app, context, controller)
end
push_app(app) click to toggle source
# File lib/ua.rb, line 249
def self.push_app(app)
   self.push(app)
end
singleton() click to toggle source
# File lib/ua.rb, line 282
def self.singleton
   SINGLETON_APP
end
top() click to toggle source
# File lib/ua.rb, line 257
def self.top
  @app_stack.last
end
top_app() click to toggle source
# File lib/ua.rb, line 260
def self.top_app
  top.app
end

Public Instance Methods

_parent(a) click to toggle source

setup an output variable

# File lib/ua.rb, line 105
def _parent(a)
  u = @store
  col = ""
  arr = a.split(".") 
  arr[0..-2].each{|x|
    col << "." << x 
    if u[x] == nil
      u[x] = {}
    elsif !u[x].respond_to?(:[])
      raise "#{col} is not valid"
    end
    u = u[x] 
  }
  [u, arr[-1]]
end
add(a = tmpid, *ar, &block) click to toggle source
# File lib/ua.rb, line 231
def add(a = tmpid, *ar, &block)
  set a, make_uaclass(a, *ar, &block)
  get a
end
app(name = TOPLEVEL) click to toggle source
# File lib/ua.rb, line 137
def app(name = TOPLEVEL)
   context(get(name), :app)
end
append(a, *b) click to toggle source
# File lib/ua.rb, line 290
def append(a, *b)
  context(get(a), :append_add, *b)
end
bootup() click to toggle source
# File lib/ua.rb, line 275
def bootup
  default_context
  add("com.ua.root").prototype.send :include, Ua::Application::Apply
end
context(a, b = Ua::Application.top.context, *c, &bl) click to toggle source
# File lib/ua.rb, line 142
def context(a, b = Ua::Application.top.context, *c, &bl)
  if block_given?
     make_context([a, b], bl)
  else
     output_by_context(a, b, *c)
  end
end
create(id, opt = {}, &b) click to toggle source
# File lib/ua.rb, line 225
def create(id, opt = {}, &b)
  x = get(id).prototype.new
  opt.each{|k, v| x.send "#{k}=", v }
  x         
end
default_context() click to toggle source
# File lib/ua.rb, line 15
 def default_context
    that = self
    context Array, :app do |arr|
       arr.map{|a| context(a, :app)}.join
    end
 
   context Apply, :app do |arr|
      get(arr.stream_).map{|a|that.app a}.join
   end
   
   context String, :app do |str|
      str
   end
   
   context UAClass, :app do |element|
     element.render
   end
   
   context Array, :stream_add do |arr, *a|
     arr.concat a
     ""
   end
   context String, :get do |obj, *a|
      context(get(obj), *a)
   end
   
   context UAClass, :stream_add do |obj, *a|
     obj.stream_ ||= stream_generate
     get(obj.stream_).concat a
     obj.extend Apply
     ""
   end
   
   context UAClass, :append_add do |obj, *a|
     obj.stream_ ||= stream_generate
     get(obj.stream_).concat a
     ""
   end
end
delete(obj) click to toggle source
# File lib/ua.rb, line 236
def delete(obj)
  set obj.id_, nil
end
each() { |x| ... } click to toggle source
# File lib/ua.rb, line 181
def each
  get(stream_).each{|x|
    yield x
  }
end
get(a) click to toggle source
# File lib/ua.rb, line 127
def get(a)
  path, val = _parent(a)
  path[val] 
end
get_local(name) click to toggle source
# File lib/ua.rb, line 318
def get_local(name)
  local = "local.#{name}"
  return get local if has?(local)  
  add local
  get local
end
go!(name = TOPLEVEL) click to toggle source
# File lib/ua.rb, line 133
def go!(name = TOPLEVEL)
   puts app(name)
end
has?(key) click to toggle source
# File lib/ua.rb, line 303
def has?(key)
  get(key)
end
make_context(a, b) click to toggle source
# File lib/ua.rb, line 55
def make_context(a, b)
   a[0] = get(a[0]).class if String === a[0] && has?(a[0])
   a[0] = a[0].class unless Module === a[0]
   @context[a] = b
end
make_uaclass(a, *ar, &block) click to toggle source
Calls superclass method
# File lib/ua.rb, line 152
def make_uaclass(a, *ar, &block)
  autoinit = false
  block ||= begin
     autoinit = true
     lambda{
        "<%= context(get(stream_), :app) %>"
     }
  end
  that = self
  code = block.call
  codename = "proc.#{a}"
  set codename, code
  klass = Class.new(OpenStruct) do
    include UAClass
    include Enumerable
    define_method(:initialize) do |*args|
    begin
        Ua::Application.push_app that
        super(*args)
        self.stream_ = that.stream_generate
        self.id_     = that.tmpid("object")
        that.set self.id_, self
    ensure
        Ua::Application.pop
    end
    end
    def mapjoin
      map{|x| yield x}.join
    end
    def each
      get(stream_).each{|x|
        yield x
      }
    end
    define_method(:classid) do
      a
    end
    define_method(:prototype) do
      klass
    end
    define_method(:erb) do |text__|
    begin
      Ua::Application.push_app that  
      ERB.new(text__).result(binding)
    ensure
      Ua::Application.pop
    end
    end
    define_method(:render) do
    begin  
      Ua::Application.push_app that          
      erb get codename
    ensure
      Ua::Application.pop
    end
    end
    define_singleton_method(:to_s) do
      a
    end
  end 
  x = klass.new
  klass.const_set :Singleton_, x
  x
end
mapjoin() { |x| ... } click to toggle source
# File lib/ua.rb, line 178
def mapjoin
  map{|x| yield x}.join
end
output_by_context(a, b, *c) click to toggle source
# File lib/ua.rb, line 68
      def output_by_context(a, b, *c)
         r = a.singleton_class rescue a.class
         r.ancestors.each{|i|
            h = i.instance_method(:uacontext) rescue nil
            catch(:continue){
              return h.bind(a).call(b, *c) if h
            }
         }
         r.ancestors.each{|i|
            h = @context[[i, b]]            
            catch(:continue){
              return a.instance_exec(a, *c, &h) if h
            }
         }
         puts "Can't find a handler #{a.class} #{b}"
         raise "Can't find a handler #{a.class} #{b}"
=begin
      ensure

        if $@
          unless $@.index{|x| x=="UAException"}
            $@.unshift("UAEnd")
            $@.unshift("UAException")
          end
          l, r = $@.index("UAException"), $@.index("UAEnd")
          u = $@.slice!(l..r)
          u[-1, 0] = "context #{a.class} #{b}"
          u += $@
          $@.replace u
        end
=end
      end
pop_local(name) click to toggle source
# File lib/ua.rb, line 338
def pop_local(name)
  local = "local.#{name}"
  r = get local
  set local, stack_of(local).pop
  r
end
push_local(name, newval = add) click to toggle source
# File lib/ua.rb, line 331
def push_local(name, newval = add)
  local = "local.#{name}"
  stack_of(local).push get local
  set local, newval
  newval
end
set(a, b) click to toggle source
# File lib/ua.rb, line 121
def set(a, b)
   path, val = _parent(a)
   path[val] = b
   b
end
set_local(name, val) click to toggle source
# File lib/ua.rb, line 325
def set_local(name, val)
  local = "local.#{name}"
  set local, val
  val
end
stack_of(name) click to toggle source
# File lib/ua.rb, line 307
def stack_of(name)
   stack = "stack.#{name}"
   if has?(stack)
     get(stack)
   else
     set stack, (a = [])
     a
   end
end
stream(a, *b) click to toggle source
# File lib/ua.rb, line 286
def stream(a, *b)
  context(get(a), :stream_add, *b)
end
stream_generate() click to toggle source
# File lib/ua.rb, line 219
def stream_generate
  u = tmpid
  set u, []
  u
end
tmpid(prefix = "temp") click to toggle source
# File lib/ua.rb, line 61
def tmpid(prefix = "temp")
   r = get(count = "#{prefix}.id") || 0
   r += 1
   set count, r
   "#{prefix}.#{r}"
end