Class: Ua::Application

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

Defined Under Namespace

Modules: Apply, ArgVoid, UAClass Classes: StackFrame

Constant Summary collapse

TOPLEVEL =
"com.ua.root"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



10
11
12
13
# File 'lib/ua.rb', line 10

def initialize
   @store   = {}
   @context = {}
end

Class Method Details

.export_commands(*names) ⇒ Object



294
295
296
297
298
299
300
301
# 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

.popObject



254
255
256
# File 'lib/ua.rb', line 254

def self.pop
  @app_stack.pop
end

.push(app = ArgVoid, context = ArgVoid, controller = ArgVoid) ⇒ Object



242
243
244
245
246
247
# 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) ⇒ Object



249
250
251
# File 'lib/ua.rb', line 249

def self.push_app(app)
   self.push(app)
end

.singletonObject



282
283
284
# File 'lib/ua.rb', line 282

def self.singleton
   SINGLETON_APP
end

.topObject



257
258
259
# File 'lib/ua.rb', line 257

def self.top
  @app_stack.last
end

.top_appObject



260
261
262
# File 'lib/ua.rb', line 260

def self.top_app
  top.app
end

Instance Method Details

#_parent(a) ⇒ Object

setup an output variable



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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) ⇒ Object



231
232
233
234
# 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) ⇒ Object



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

def app(name = TOPLEVEL)
   context(get(name), :app)
end

#append(a, *b) ⇒ Object



290
291
292
# File 'lib/ua.rb', line 290

def append(a, *b)
  context(get(a), :append_add, *b)
end

#bootupObject



275
276
277
278
# 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) ⇒ Object



142
143
144
145
146
147
148
# 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) ⇒ Object



225
226
227
228
229
# 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_contextObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object



236
237
238
# File 'lib/ua.rb', line 236

def delete(obj)
  set obj.id_, nil
end

#get(a) ⇒ Object



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

def get(a)
  path, val = _parent(a)
  path[val] 
end

#get_local(name) ⇒ Object



318
319
320
321
322
323
# 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) ⇒ Object



133
134
135
# File 'lib/ua.rb', line 133

def go!(name = TOPLEVEL)
   puts app(name)
end

#has?(key) ⇒ Boolean

Returns:

  • (Boolean)


303
304
305
# File 'lib/ua.rb', line 303

def has?(key)
  get(key)
end

#make_context(a, b) ⇒ Object



55
56
57
58
59
# 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) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# 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

#output_by_context(a, b, *c) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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 [email protected]{|x| x=="UAException"}
      [email protected]("UAEnd")
      [email protected]("UAException")
    end
    l, r = [email protected]("UAException"), [email protected]("UAEnd")
    u = [email protected]!(l..r)
    u[-1, 0] = "context #{a.class} #{b}"
    u += $@
    [email protected] u
  end
=end
end

#pop_local(name) ⇒ Object



338
339
340
341
342
343
# 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) ⇒ Object



331
332
333
334
335
336
# 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) ⇒ Object



121
122
123
124
125
# File 'lib/ua.rb', line 121

def set(a, b)
   path, val = _parent(a)
   path[val] = b
   b
end

#set_local(name, val) ⇒ Object



325
326
327
328
329
# File 'lib/ua.rb', line 325

def set_local(name, val)
  local = "local.#{name}"
  set local, val
  val
end

#stack_of(name) ⇒ Object



307
308
309
310
311
312
313
314
315
# 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) ⇒ Object



286
287
288
# File 'lib/ua.rb', line 286

def stream(a, *b)
  context(get(a), :stream_add, *b)
end

#stream_generateObject



219
220
221
222
223
# File 'lib/ua.rb', line 219

def stream_generate
  u = tmpid
  set u, []
  u
end

#tmpid(prefix = "temp") ⇒ Object



61
62
63
64
65
66
# File 'lib/ua.rb', line 61

def tmpid(prefix = "temp")
   r = get(count = "#{prefix}.id") || 0
   r += 1
   set count, r
   "#{prefix}.#{r}"
end