Sha256: 9c57edd65612d892fa5ce0162ae8f19caf11c175d9745e8f1f35448ff9ac45d0

Contents?: true

Size: 1.73 KB

Versions: 7

Compression:

Stored size: 1.73 KB

Contents

require 'xommelier'
require 'nokogiri'
require 'xommelier/xml/namespace'
require 'active_support/concern'

module Xommelier
  module Xml
    extend ActiveSupport::Concern

    DEFAULT_NS = 'http://www.w3.org/XML/1998/namespace'

    module ClassMethods
      def ns
        Xommelier::Xml::Namespace.registry
      end

      # Defines namespace used in formats
      def xmlns(uri = nil, options = {}, &block)
        if uri
          options[:module] ||= self
          instance_variable_set(:@_xmlns, Xommelier::Xml::Namespace.new(uri, options, &block))
        end
        instance_variable_get(:@_xmlns)
      end

      def schema(schema = nil)
        if schema
          # If schema or schema path provided, set schema
          schema = Nokogiri::XML::Schema(open(schema).read) unless schema.is_a?(Nokogiri::XML::Node)
          instance_variable_set(:@_schema, schema)
        elsif !instance_variable_defined?(:@_schema)
          # Unless schema exists, try to autoload schema
          available_schema = available_schemas.find { |path| path =~ /#{xmlns.as}\.xsd/ }
          self.schema(available_schema) if available_schema
        else
          instance_variable_set(:@schema, nil)
        end
        instance_variable_get(:@_schema)
      end

      protected

      def available_schemas
        @_available_schemas ||= $:.map do |path|
          Dir[File.join(path, 'xommelier/schemas', '*.xsd')]
        end.flatten.uniq
      end
    end

    included do
      instance_variable_set :@_xmlns, nil
    end

    extend ClassMethods
    xmlns DEFAULT_NS, as: :xml

    module CommonAttributes
      def self.included(base)
        base.attribute :lang, ns: Xml.xmlns
        base.attribute :base, type: Uri, ns: Xml.xmlns
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
xommelier-0.1.15 lib/xommelier/xml.rb
xommelier-0.1.14 lib/xommelier/xml.rb
xommelier-0.1.13 lib/xommelier/xml.rb
xommelier-0.1.12 lib/xommelier/xml.rb
xommelier-0.1.11 lib/xommelier/xml.rb
xommelier-0.1.10 lib/xommelier/xml.rb
xommelier-0.1.9 lib/xommelier/xml.rb