Sha256: d44f5bcd4e850410ff7ae76ccf8f82aee6c1f3820e76e6fbe27d50b5151088fa

Contents?: true

Size: 1.69 KB

Versions: 20

Compression:

Stored size: 1.69 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::SolrQueryBuilder.construct_query_for_ids(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

20 entries across 20 versions & 1 rubygems

Version Path
ddr-models-3.0.8 lib/ddr/models/struct_div.rb
ddr-models-3.0.7 lib/ddr/models/struct_div.rb
ddr-models-3.0.6 lib/ddr/models/struct_div.rb
ddr-models-3.0.5 lib/ddr/models/struct_div.rb
ddr-models-3.0.4 lib/ddr/models/struct_div.rb
ddr-models-3.0.3 lib/ddr/models/struct_div.rb
ddr-models-3.0.2 lib/ddr/models/struct_div.rb
ddr-models-3.0.1 lib/ddr/models/struct_div.rb
ddr-models-3.0.0 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.rc6 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.rc5 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.rc4 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.rc3 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.rc2 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.rc1 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.22 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.21 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.20 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.18 lib/ddr/models/struct_div.rb
ddr-models-3.0.0.beta.17 lib/ddr/models/struct_div.rb