Sha256: e18809f31c4ca88c24e2b79514bd695b846a5f57e3eef083cce7b34ea89760f0

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

module Vitreous
  module Share
    class Indexer
      def initialize( structure )
        @structure = structure
      end
      
      def generate
        structure = @structure
        
        {
          'not_found' => generate_not_found( structure ),
          'title'     => Vitreous::Share::IndexerUtils.to_title( structure['name'] ),
          'link'      => Vitreous::Share::IndexerUtils.to_link( structure['path'] ),
          'type'      => 'collection',
          'elements'  => tree( structure['elements'].select { |e| !(e['name'] =~ /^_/) }.sort { |x, y| x['name'] <=> y['name'] } )
        }.merge( 
          Vitreous::Share::IndexerUtils.meta_properties(
            structure['elements'].select { |e| e['name'] =~ /^_root\./ } 
          )
        )
      end
      
      def tree( structure )
        Vitreous::Share::IndexerUtils.grouping( structure ).values.map do |e|
          {
            'title'    => Vitreous::Share::IndexerUtils.to_title( e[0]['name'] ),
            'link'     => Vitreous::Share::IndexerUtils.to_link( e[0]['path'] ),
            'type'     => e.any? { |e2| e2['type'] == 'directory' } ? 'collection' : 'item',
            'elements' => tree( e[0]['elements'].sort { |x, y| x['name'] <=> y['name'] } )
          }.merge( Vitreous::Share::IndexerUtils.meta_properties( e ) )
        end
      end
      
      def generate_not_found( structure )
        {
          'title'       => 'Not found',
          'type'        => 'item',
          'elements'    => [],
        }.merge( 
          Vitreous::Share::IndexerUtils.meta_properties(
            structure['elements'].select { |e| e['name'] =~ /^_not_found\./ } 
          )
        )
      end
      
      def json
        JSON.pretty_generate( generate )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vitreous_share-0.0.25 lib/vitreous/share/indexer.rb