Class: DoctorNinja::Parser
- Inherits:
-
Object
- Object
- DoctorNinja::Parser
- Defined in:
- lib/doctor_ninja/parser.rb
Defined Under Namespace
Classes: Noop
Instance Method Summary collapse
- #debug(node, b) ⇒ Object
- #debug?(node, parsers) ⇒ Boolean
-
#initialize(doc) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
- #parse_node(node, context) ⇒ Object
- #parsers ⇒ Object
- #parsers_for(node, context) ⇒ Object
Constructor Details
#initialize(doc) ⇒ Parser
Returns a new instance of Parser.
12 13 14 15 |
# File 'lib/doctor_ninja/parser.rb', line 12 def initialize(doc) @docx = doc @xmldoc = Nokogiri::XML @docx.read "word/document.xml" end |
Instance Method Details
#debug(node, b) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/doctor_ninja/parser.rb', line 50 def debug(node,b) if(ENV["DEBUG_MODE"]=="pry") require "pry" b.pry else puts "---BEGIN---\n#{node.to_xml}\n----END----" end end |
#debug?(node, parsers) ⇒ Boolean
43 44 45 46 47 48 |
# File 'lib/doctor_ninja/parser.rb', line 43 def debug?(node,parsers) ENV["DEBUG"] == "all" || (ENV["DEBUG"] == "missing" && parsers.length == 1) || ENV["DEBUG"] == node.name || (node.namespace && ENV["DEBUG"] == "#{node.namespace.prefix}:#{node.name}") end |
#parse ⇒ Object
17 18 19 |
# File 'lib/doctor_ninja/parser.rb', line 17 def parse self.parse_node(@xmldoc.root, {}) end |
#parse_node(node, context) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/doctor_ninja/parser.rb', line 21 def parse_node(node,context) parsers = parsers_for(node,context) if debug?(node,parsers) debug(node,binding) end parsers .first .parse end |
#parsers ⇒ Object
39 40 41 |
# File 'lib/doctor_ninja/parser.rb', line 39 def parsers DoctorNinja::Parsers.constants.map{|c| DoctorNinja::Parsers.const_get(c)}+[Noop] end |
#parsers_for(node, context) ⇒ Object
33 34 35 36 37 |
# File 'lib/doctor_ninja/parser.rb', line 33 def parsers_for(node,context) parsers .select{|p| p.applicable_to? node} .map{|p| p.new(node, self.public_method(:parse_node), @docx, context)} end |