lib/watir/generator/base/spec_extractor.rb in watir-6.13.0 vs lib/watir/generator/base/spec_extractor.rb in watir-6.14.0

- old
+ new

@@ -1,138 +1,156 @@ module Watir module Generator - class Base::SpecExtractor + module Base + class SpecExtractor + IDL_SELECTOR = "//pre[contains(@class, 'idl')]".freeze - IDL_SELECTOR = "//pre[contains(@class, 'idl')]".freeze + class InterfaceNotFound < StandardError; end - class InterfaceNotFound < StandardError; end + def initialize(uri) + @uri = uri + end - def initialize(uri) - @uri = uri - end + def process + download_and_parse + extract_idl_parts + extract_interface_map + drop_issued_interfaces + build_result + rescue StandardError + p errors + raise + end - def process - download_and_parse - extract_idl_parts - extract_interface_map - drop_issued_interfaces - build_result - rescue - p errors - raise - end + def errors + @errors ||= [] + end - def errors - @errors ||= [] - end + # + # returns a topoligically sorted array of WebIDL::Ast::Interface objects + # - # - # returns a topoligically sorted array of WebIDL::Ast::Interface objects - # + def sorted_interfaces + process if @interfaces.nil? - def sorted_interfaces - process if @interfaces.nil? + idl_sorter.sort.map { |name| + @interfaces.find { |i| i.name == name } || puts("ignoring interface: #{name}") + }.compact + end - sorter.sort.map { |name| - @interfaces.find { |i| i.name == name } or puts "ignoring interface: #{name}" - }.compact - end + def print_hierarchy + process if @interfaces.nil? + idl_sorter.print + end - def print_hierarchy - process if @interfaces.nil? - sorter.print - end + def fetch_interface(interface) + @interfaces_by_name[interface] || raise(InterfaceNotFound, "#{interface} not found in IDL") + end - def fetch_interface(interface) - @interfaces_by_name[interface] or raise InterfaceNotFound, "#{interface} not found in IDL" - end + private - private + def download_and_parse + URI.open(@uri) { |io| @doc = Nokogiri.HTML(io) } + end - def download_and_parse - open(@uri) { |io| @doc = Nokogiri.HTML(io) } - end + def extract_idl_parts + parsed = @doc.search(IDL_SELECTOR).map { |e| parse_idl(e.inner_text) }.compact - def extract_idl_parts - parsed = @doc.search(IDL_SELECTOR).map { |e| parse_idl(e.inner_text) }.compact + implements = [] + includes = [] + @interfaces = [] - implements = [] - @interfaces = [] - - parsed.flatten.each do |element| - case element - when WebIDL::Ast::Interface - @interfaces << element - when WebIDL::Ast::ImplementsStatement - implements << element + parsed.flatten.each do |element| + case element + when WebIDL::Ast::Interface + @interfaces << element + when WebIDL::Ast::ImplementsStatement + implements << element + when WebIDL::Ast::IncludesStatement + includes << element + end end + + @interfaces_by_name = @interfaces.group_by(&:name) + apply_implements(implements) + apply_includes(includes) + merge_interfaces end - @interfaces_by_name = @interfaces.group_by(&:name) - apply_implements(implements) - merge_interfaces - end + def extract_interface_map + raise NotImplementedError + end - def extract_interface_map - raise NotImplementedError - end + def drop_issued_interfaces + @interface_map.delete_if do |_, interface| + issued_interfaces.include?(interface) + end + end - def drop_issued_interfaces - @interface_map.delete_if do |_, interface| - issued_interfaces.include?(interface) + def build_result + raise NotImplementedError end - end - def build_result - raise NotImplementedError - end + def parse_idl(str) + result = idl_parser.parse(str) - def parse_idl(str) - result = idl_parser.parse(str) - - if result - result.build - else - errors << idl_parser.failure_reason - nil + if result + result.build + else + errors << idl_parser.failure_reason + nil + end end - end - def apply_implements(implements) - implements.each do |is| - implementor_name = is.implementor.gsub(/^::/, '') - implementee_name = is.implementee.gsub(/^::/, '') + def apply_implements(implements) + implements.each do |is| + implementor_name = is.implementor.gsub(/^::/, '') + implementee_name = is.implementee.gsub(/^::/, '') - begin - intf = fetch_interface(implementor_name).first - intf.implements << fetch_interface(implementee_name).first - rescue InterfaceNotFound => ex - puts ex.message + begin + intf = fetch_interface(implementor_name).first + intf.implements << fetch_interface(implementee_name).first + rescue InterfaceNotFound => ex + puts ex.message + end end end - end - def merge_interfaces - non_duplicates = @interfaces.uniq(&:name) - duplicates = @interfaces - non_duplicates + def apply_includes(includes) + includes.each do |is| + includer_name = is.includer.gsub(/^::/, '') + includee_name = is.includee.gsub(/^::/, '') - duplicates.each do |intf| - final = non_duplicates.find { |i| i.name == intf.name } - final.inherits += intf.inherits - final.members += intf.members - final.extended_attributes += intf.extended_attributes + begin + intf = fetch_interface(includer_name).first + intf.includes << fetch_interface(includee_name).first + rescue InterfaceNotFound => ex + puts ex.message + end + end end - @interfaces = non_duplicates - end + def merge_interfaces + non_duplicates = @interfaces.uniq(&:name) + duplicates = @interfaces - non_duplicates - def idl_parser - @idl_parser ||= WebIDL::Parser::IDLParser.new - end + duplicates.each do |intf| + final = non_duplicates.find { |i| i.name == intf.name } + final.inherits += intf.inherits + final.members += intf.members + final.extended_attributes += intf.extended_attributes + end - def sorter - @idl_sorter ||= Base::IDLSorter.new(@interfaces) - end + @interfaces = non_duplicates + end - end # SpecExtractor + def idl_parser + @idl_parser ||= WebIDL::Parser::IDLParser.new + end + + def idl_sorter + @idl_sorter ||= Base::IDLSorter.new(@interfaces) + end + end # SpecExtractor + end # Base end # Generator end # Watir