Sha256: 03baca2c0e1f2743c09397f67600de2c011e57faba91803d77e6278ffcde088f

Contents?: true

Size: 1.68 KB

Versions: 14

Compression:

Stored size: 1.68 KB

Contents

module Ddr
  module Models
    class StructDiv

      attr_accessor :id, :label, :order, :orderlabel, :type, :fptrs, :divs

      def initialize(structmap_or_div_node)
        @id = structmap_or_div_node['ID']
        @label = structmap_or_div_node['LABEL']
        @order = structmap_or_div_node['ORDER']
        @orderlabel = structmap_or_div_node['ORDERLABEL']
        @type = structmap_or_div_node['TYPE']
        @fptrs = fptr_pids(structmap_or_div_node) if structmap_or_div_node.node_name == "div"
        @divs = subdivs(structmap_or_div_node).sort
      end

      def <=>(other)
        self.order.to_i <=> other.order.to_i
      end

      def pids
        collect_pids(self)
      end

      def docs
        query = ActiveFedora::SolrService.construct_query_for_pids(pids)
        results = ActiveFedora::SolrService.query(query, rows: 999999)
        results.each_with_object({}) do |r, memo|
          memo[r["id"]] = ::SolrDocument.new(r)
        end
      end

      def objects
        pids.each_with_object({}) do |pid, memo|
          memo[pid] = ActiveFedora::Base.find(pid)
        end
      end

      def as_json(options={})
        super.compact
      end

      private

      def fptr_pids(div_node)
        div_node.xpath('xmlns:fptr').map { |fptr_node| fptr_node["CONTENTIDS"] }
      end

      def subdivs(structmap_or_div_node)
        structmap_or_div_node.xpath('xmlns:div').map { |div_node| Ddr::Models::StructDiv.new(div_node) }
      end

      def collect_pids(structdiv)
        pids = structdiv.divs.each_with_object([]) do |div, memo|
          memo << collect_pids(div)
        end
        pids << structdiv.fptrs if structdiv.fptrs
        pids.flatten
      end

    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
ddr-models-3.0.0.beta.16 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.15 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.14 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.13 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.12 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.11 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.10 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.9 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.8 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.7 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.6 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.4 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.3 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.2 lib/ddr/models/struct_div.rb