Class: Canis::CanisParser
- Defined in:
- lib/canis/core/include/canisparser.rb
Overview
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.
To define your own parser:
CanisParser.map( :custom => [ 'canis/core/include/customparser', 'Canis::CustomParser' ]
and later at some point,
CanisParser[:custom]
Class Method Summary collapse
-
.[](name) ⇒ Object
Used by user to retrieve a parser instance, creating one if not present CanisParser.
-
.content_types ⇒ Object
hash storing a filename and classname per content_type.
- .create(args) ⇒ Object
-
.instances ⇒ Object
hash storing a parser instance per content_type.
-
.map(params) ⇒ Object
Used by user to define a new parser map( :tmux => [‘filename’, ‘klassname’] ).
Instance Method Summary collapse
-
#initialize(*args) ⇒ CanisParser
constructor
WARNING - this creates a CanisParser class which we really can’t use.
-
#method_missing(meth, *args, &block) ⇒ Object
delegate all call to color parser.
-
#parse_format(s, *args, &block) ⇒ Object
delegate call to color parser.
Constructor Details
#initialize(*args) ⇒ CanisParser
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.
78 79 80 81 82 83 84 85 86 87 88 89 |
# 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
delegate all call to color parser
95 96 97 98 |
# 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 |
Class Method Details
.[](name) ⇒ Object
Used by user to retrieve a parser instance, creating one if not present CanisParser
55 56 57 58 59 60 |
# 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 ⇒ Object
hash storing a filename and classname per content_type
32 33 34 35 36 37 38 39 40 41 42 |
# 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) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# 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 ⇒ Object
hash storing a parser instance per content_type
44 45 46 |
# File 'lib/canis/core/include/canisparser.rb', line 44 def instances @instances ||= {} end |
.map(params) ⇒ Object
Used by user to define a new parser map( :tmux => [‘filename’, ‘klassname’] )
49 50 51 |
# File 'lib/canis/core/include/canisparser.rb', line 49 def map(params) content_types.update params end |
Instance Method Details
#parse_format(s, *args, &block) ⇒ Object
delegate call to color parser
91 92 93 |
# File 'lib/canis/core/include/canisparser.rb', line 91 def parse_format s, *args, &block @clazz.parse_format(s, *args, &block) end |