lib/moxml/adapter.rb in moxml-0.1.0 vs lib/moxml/adapter.rb in moxml-0.1.1

- old
+ new

@@ -1,175 +1,31 @@ -# lib/moxml/adapter.rb -module Moxml - class Adapter - # Document operations - def parse(input, options = {}) - raise NotImplementedError - end +# frozen_string_literal: true - def serialize(node, options = {}) - raise NotImplementedError - end +require_relative "adapter/base" - # Node type detection - def node_type(node) - raise NotImplementedError - end +module Moxml + module Adapter + AVALIABLE_ADAPTERS = %i[nokogiri oga].freeze # ox to be added later - # Node operations - def node_name(node) - raise NotImplementedError - end + class << self + def load(name) + require_adapter(name) + const_get(name.to_s.capitalize) + rescue LoadError => e + raise LoadError, "Could not load #{name} adapter. Please ensure the #{name} gem is available: #{e.message}" + end - def parent(node) - raise NotImplementedError - end + private - def children(node) - raise NotImplementedError - end - - def next_sibling(node) - raise NotImplementedError - end - - def previous_sibling(node) - raise NotImplementedError - end - - def document(node) - raise NotImplementedError - end - - def remove(node) - raise NotImplementedError - end - - def replace(node, new_node) - raise NotImplementedError - end - - # Element operations - def create_element(document, name) - raise NotImplementedError - end - - def attributes(element) - raise NotImplementedError - end - - def get_attribute(element, name) - raise NotImplementedError - end - - def set_attribute(element, name, value) - raise NotImplementedError - end - - def remove_attribute(element, name) - raise NotImplementedError - end - - def add_child(element, child) - raise NotImplementedError - end - - # Namespace operations - def namespaces(element) - raise NotImplementedError - end - - def add_namespace(element, prefix, uri) - raise NotImplementedError - end - - def namespace_prefix(namespace) - raise NotImplementedError - end - - def namespace_uri(namespace) - raise NotImplementedError - end - - # Attribute operations - def attribute_value(attribute) - raise NotImplementedError - end - - def set_attribute_value(attribute, value) - raise NotImplementedError - end - - def attribute_namespace(attribute) - raise NotImplementedError - end - - # Text operations - def create_text(document, content) - raise NotImplementedError - end - - def text_content(text) - raise NotImplementedError - end - - def set_text_content(text, content) - raise NotImplementedError - end - - # CDATA operations - def create_cdata(document, content) - raise NotImplementedError - end - - # Comment operations - def create_comment(document, content) - raise NotImplementedError - end - - def comment_content(comment) - raise NotImplementedError - end - - def set_comment_content(comment, content) - raise NotImplementedError - end - - # Processing instruction operations - def create_processing_instruction(document, target, content) - raise NotImplementedError - end - - def processing_instruction_target(pi) - raise NotImplementedError - end - - def processing_instruction_content(pi) - raise NotImplementedError - end - - def set_processing_instruction_target(pi, target) - raise NotImplementedError - end - - def set_processing_instruction_content(pi, content) - raise NotImplementedError - end - - # Document specific operations - def root(document) - raise NotImplementedError - end - - protected - - def normalize_options(options) - { - encoding: options[:encoding] || "UTF-8", - indent: options[:indent] || 2, - xml_declaration: options.fetch(:xml_declaration, true), - pretty: options.fetch(:pretty, true), - namespace_declarations: options.fetch(:namespace_declarations, true), - } + def require_adapter(name) + require "#{__dir__}/adapter/#{name}" + rescue LoadError + begin + require name.to_s + require "#{__dir__}/adapter/#{name}" + rescue LoadError => e + raise LoadError, "Failed to load #{name} adapter: #{e.message}" + end + end end end end