Sha256: 06593538e509a4586e3b67a0b29c229fdbdb540af428847e4578973613d1ba8b

Contents?: true

Size: 1.48 KB

Versions: 5

Compression:

Stored size: 1.48 KB

Contents

require "nom"

module ActiveFedora
  class NomDatastream < File
    include Datastreams::NokogiriDatastreams

    def self.set_terminology(options = {}, &block)
      @terminology_options = options || {}
      @terminology = block
    end

    class << self
      attr_reader :terminology_options
    end

    class << self
      attr_reader :terminology
    end

    def self.decorate_ng_xml(xml)
      xml.set_terminology terminology_options, &terminology
      xml.nom!
      xml
    end

    def serialize!
      self.content = @ng_xml.to_s if @ng_xml
    end

    def to_solr
      solr_doc = {}

      ng_xml.terminology.flatten.select { |x| x.options[:index] }.each do |term|
        term.values.each do |v|
          Array(term.options[:index]).each do |index_as|
            solr_doc[index_as] ||= []
            solr_doc[index_as] << if v.is_a? Nokogiri::XML::Node
                                    v.text
                                  else
                                    v
                                  end
          end
        end
      end

      solr_doc
    end

    def method_missing(method, *args, &block)
      if ng_xml.respond_to? method
        ng_xml.send(method, *args, &block)
      else
        super
      end
    end

    def respond_to_missing?(*args)
      ng_xml.respond_to?(*args)
    end

    def respond_to?(*args)
      super || self.class.terminology.respond_to?(*args)
    end

    protected

      def default_mime_type
        'text/xml'
      end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
active_fedora-datastreams-0.5.0 lib/active_fedora/nom_datastream.rb
active_fedora-datastreams-0.4.0 lib/active_fedora/nom_datastream.rb
active_fedora-datastreams-0.3.0 lib/active_fedora/nom_datastream.rb
active_fedora-datastreams-0.2.0 lib/active_fedora/nom_datastream.rb
active_fedora-datastreams-0.1.0 lib/active_fedora/nom_datastream.rb