Module: Fabulator::TagLib::ClassMethods

Defined in:
lib/fabulator/tag_lib.rb

Instance Method Summary collapse

Instance Method Details

#action(name, klass = nil, &block) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/fabulator/tag_lib.rb', line 344

def action(name, klass = nil, &block)
  self.action_descriptions[name.to_sym] = Fabulator::TagLib.last_description if Fabulator::TagLib.last_description
  Fabulator::TagLib.last_description = nil
  if block
    define_method("action:#{name.to_s}", block)
  elsif !klass.nil?
    Fabulator::TagLib.structural_classes[self.name] ||= {}
    Fabulator::TagLib.structural_classes[self.name][name.to_sym] = klass
    action(name) { |e,c|
      r = klass.new
      r.compile_xml(e,c)
      r
    }
  end
end

#action_descriptions(hash = nil) ⇒ Object



273
274
275
# File 'lib/fabulator/tag_lib.rb', line 273

def action_descriptions(hash = nil)
  Fabulator::TagLib.action_descriptions[self.name] ||= (hash ||{})
end

#axis(nom, &block) ⇒ Object



332
333
334
# File 'lib/fabulator/tag_lib.rb', line 332

def axis(nom, &block)
  Fabulator::TagLib.axes[nom] = block
end

#consolidation(name, opts = {}, &block) ⇒ Object



398
399
400
401
402
403
# File 'lib/fabulator/tag_lib.rb', line 398

def consolidation(name, opts = {}, &block)
  self.function_descriptions[name.to_sym] = { :type => :consolidation }.merge(opts)
  self.function_descriptions[name.to_sym][:description] = Fabulator::TagLib.last_description if Fabulator::TagLib.last_description
  Fabulator::TagLib.last_description = nil
  define_method("fctn:consolidation:#{name}", &block)
end

#constraint(name, &block) ⇒ Object



433
434
435
# File 'lib/fabulator/tag_lib.rb', line 433

def constraint(name, &block)
  define_method("constraint:#{name}", &block)
end

#desc(text) ⇒ Object



340
341
342
# File 'lib/fabulator/tag_lib.rb', line 340

def desc(text)
  Fabulator::TagLib.last_description = RedCloth.new(Util.strip_leading_whitespace(text)).to_html
end

#filter(name, &block) ⇒ Object



429
430
431
# File 'lib/fabulator/tag_lib.rb', line 429

def filter(name, &block)
  define_method("filter:#{name}", &block)
end

#function(name, returns = nil, takes = nil, &block) ⇒ Object



376
377
378
379
380
381
382
# File 'lib/fabulator/tag_lib.rb', line 376

def function(name, returns = nil, takes = nil, &block)
  self.function_descriptions[name.to_sym] = { :returns => returns, :takes => takes }
  self.function_descriptions[name.to_sym][:description] = Fabulator::TagLib.last_description if Fabulator::TagLib.last_description
  #self.function_args[name] = { :return => returns, :takes => takes }
  Fabulator::TagLib.last_description = nil
  define_method("fctn:#{name}", &block)
end

#function_decl(name, expr, ns) ⇒ Object



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/fabulator/tag_lib.rb', line 413

def function_decl(name, expr, ns)
  parser = Fabulator::Expr::Parser.new
  fctn_body = parser.parse(expr, ns)

  function name do |ctx, args, ns|
    res = nil
    ctx.in_context do
      args.size.times do |i|
        ctx.set_var((i+1).to_s, args[i])
      end
      res = fctn_body.run(ctx)
    end
    res
  end
end

#function_descriptions(hash = nil) ⇒ Object



277
278
279
# File 'lib/fabulator/tag_lib.rb', line 277

def function_descriptions(hash = nil)
  Fabulator::TagLib.action_descriptions[self.name] ||= (hash ||{})
end

#get_type(nom) ⇒ Object



303
304
305
306
307
308
309
310
311
# File 'lib/fabulator/tag_lib.rb', line 303

def get_type(nom)
  ns = nil
  Fabulator::TagLib.namespaces.each_pair do |k,v|
    if v.is_a?(self)
      ns = k
    end
  end
  Fabulator::TagLib.types[ns][nom.to_sym]
end

#has_type(nom, &block) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/fabulator/tag_lib.rb', line 313

def has_type(nom, &block)
  ns = nil
  Fabulator::TagLib.namespaces.each_pair do |k,v|
    if v.is_a?(self)
      ns = k
    end
  end
  Fabulator::TagLib.types[ns] ||= {}
  Fabulator::TagLib.types[ns][nom.to_sym] ||= Fabulator::TagLib::Type.new([ns, nom.to_sym])

  if block
    Fabulator::TagLib.types[ns][nom.to_sym].instance_eval &block
  end

  mapping nom do |ctx, i|
    ctx.with_root(i).to([ ns, nom.to_s ]).root
  end
end

#inherited(subclass) ⇒ Object



267
268
269
270
271
# File 'lib/fabulator/tag_lib.rb', line 267

def inherited(subclass)
  subclass.action_descriptions.reverse_merge! self.action_descriptions
  subclass.function_descriptions.reverse_merge! self.function_descriptions
  super
end

#mapping(name, opts = {}, &block) ⇒ Object



405
406
407
408
409
410
411
# File 'lib/fabulator/tag_lib.rb', line 405

def mapping(name, opts = {}, &block)
  name = name.to_sym
  self.function_descriptions[name] = { :type => :mapping }.merge(opts)
  self.function_descriptions[name][:description] = Fabulator::TagLib.last_description if Fabulator::TagLib.last_description
  Fabulator::TagLib.last_description = nil
  define_method("fctn:#{name.to_s}", &block)
end

#namespace(ns) ⇒ Object



285
286
287
# File 'lib/fabulator/tag_lib.rb', line 285

def namespace(ns)
  Fabulator::TagLib.namespaces[ns] = self.new
end

#namespacesObject



336
337
338
# File 'lib/fabulator/tag_lib.rb', line 336

def namespaces
  Fabulator::TagLib.namespaces
end

#presentationObject



289
290
291
# File 'lib/fabulator/tag_lib.rb', line 289

def presentation
  Fabulator::TagLib.presentations[self.name] ||= Fabulator::TagLib::Presentations.new
end

#presentations(&block) ⇒ Object



437
438
439
# File 'lib/fabulator/tag_lib.rb', line 437

def presentations(&block)
  self.presentation.instance_eval &block
end

#reduction(name, opts = {}, &block) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/fabulator/tag_lib.rb', line 384

def reduction(name, opts = {}, &block)
  self.function_descriptions[name.to_sym] = { :type => :reduction }.merge(opts)
  self.function_descriptions[name.to_sym][:description] = Fabulator::TagLib.last_description if Fabulator::TagLib.last_description
  Fabulator::TagLib.last_description = nil
  define_method("fctn:#{name}", &block)
  cons = self.function_descriptions[name.to_sym][:consolidation]
  if !cons.nil?
    Fabulator::TagLib.last_description = self.function_descriptions[name.to_sym][:description]
    consolidation name do |ctx, args|
      send "fctn:#{cons}", ctx, args
    end
  end
end

#register_attribute(a, options = {}) ⇒ Object



293
294
295
296
297
298
299
300
301
# File 'lib/fabulator/tag_lib.rb', line 293

def register_attribute(a, options = {})
  ns = nil
  Fabulator::TagLib.namespaces.each_pair do |k,v|
    if v.is_a?(self)
      ns = k
    end
  end
  Fabulator::TagLib.attributes << [ ns, a, options ]
end

#register_namespace(ns) ⇒ Object



281
282
283
# File 'lib/fabulator/tag_lib.rb', line 281

def register_namespace(ns)
  Fabulator::TagLib.namespaces[ns] = self.new
end

#structural(name, klass = nil, &block) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/fabulator/tag_lib.rb', line 360

def structural(name, klass = nil, &block)
  self.structural_descriptions[name.to_sym] = Fabulator::TagLib.last_description if Fabulator::TagLib.last_description
  Fabulator::TagLib.last_description = nil
  if block
    define_method("structural:#{name.to_s}", block)
  elsif !klass.nil?
    structural(name) { |e,c|
      r = klass.new
      r.compile_xml(e,c)
      r
    }
    Fabulator::TagLib.structural_classes[self.name] ||= {}
    Fabulator::TagLib.structural_classes[self.name][name.to_sym] = klass
  end
end