class Canis::CanisParser

Uses multiton pattern from blog.rubybestpractices.com/posts/gregory/059-issue-25-creational-design-patterns.html to create and returns cached instances of a text parser. Users will call the [] method rather than the new method. If users wish to declare their own custom parser, then the map method is to be used.

@example

CanisParser[:tmux]

To define your own parser:

CanisParser.map( :custom => [ 'canis/core/include/customparser', 'Canis::CustomParser' ]

and later at some point,

CanisParser[:custom]

Public Class Methods

[](name) click to toggle source

Used by user to retrieve a parser instance, creating one if not present CanisParser

# File lib/canis/core/include/canisparser.rb, line 55
def [](name)
  $log.debug "  [] got #{name} "
  raise "nil received by [] " unless name
  instances[name] ||= new(content_types[name])
  #instances[name] ||= create(content_types[name])
end
content_types() click to toggle source

hash storing a filename and classname per content_type

# File lib/canis/core/include/canisparser.rb, line 32
def content_types 
  #@content_types ||= {}
  unless @content_types
    @content_types = {}
    #map(:tmux => [ 'canis/core/util/defaultcolorparser', 'DefaultColorParser'])
    #map(:ansi => [ 'canis/core/util/ansiparser', 'AnsiParser'] )
    @content_types[:tmux] = [ 'canis/core/util/defaultcolorparser', 'DefaultColorParser']
    @content_types[:ansi] = [ 'canis/core/util/ansiparser', 'AnsiParser']
  end
  return @content_types
end
create(args) click to toggle source
# File lib/canis/core/include/canisparser.rb, line 61
def create args
  filename = args.first
  klassname = args[1]
  $log.debug "  canisparser create got #{args} "
  require filename
  clazz = Object.const_get(klassname).new
  $log.debug "  created #{clazz.class} "
  #  clazz = 'Foo::Bar'.split('::').inject(Object) {|o,c| o.const_get c}
  return clazz
end
instances() click to toggle source

hash storing a parser instance per content_type

# File lib/canis/core/include/canisparser.rb, line 44
def instances 
  @instances ||= {}
end
map(params) click to toggle source

Used by user to define a new parser map( :tmux => ['filename', 'klassname'] )

# File lib/canis/core/include/canisparser.rb, line 49
def map(params)
  content_types.update params
end
new(*args) click to toggle source
WARNING - this creates a CanisParser class which we really can't use.

So we need to delegate to the color parse we created. create and return a parser instance Canisparser.new filename, klassname Usually, not called by user, since this instance is not cached. Use map

and then +[]+ instead for creating and cacheing.
# File lib/canis/core/include/canisparser.rb, line 78
def initialize *args
  args = args.flatten
  filename = args.first
  klassname = args[1]
  $log.debug "  canisparser init got #{args} "
  raise "Canisparser init got nil" unless filename
  require filename
  clazz = Object.const_get(klassname).new
  #  clazz = 'Foo::Bar'.split('::').inject(Object) {|o,c| o.const_get c}
  #return clazz
  @clazz = clazz
end

Public Instance Methods

method_missing(meth, *args, &block) click to toggle source

delegate all call to color parser

# File lib/canis/core/include/canisparser.rb, line 95
def method_missing meth, *args, &block
  #$log.debug "  canisparser got method_missing for #{meth}, sending to #{@clazz.class} "
  @clazz.send( meth, *args, &block)
end
parse_format(s, *args, &block) click to toggle source

delegate call to color parser

# File lib/canis/core/include/canisparser.rb, line 91
def parse_format s, *args, &block
  @clazz.parse_format(s, *args, &block)
end