Sha256: db5f8b2b5117d7709ff4561d3639e346cc2410c09dc7671b6ce56ceeaacd2563

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 KB

Contents

module Solr
  class SchemaGenerator

    attr_reader :types

    # Takes an array of index type configs.
    #
    def initialize configuration
      @types = configuration.types
    end

    #
    #
    def generate
      generate_schema_for bound_field_names
    end

    # Returns a binding with the values needed for the schema xml.
    #
    def bound_field_names
      field_names = combine_field_names
      binding
    end

    # TODO
    #
    def combine_field_names
      field_names = []
      types.each do |type|
        field_names += type.solr_fields.map(&:name)
      end
      field_names.uniq!
      field_names
    end

    #
    #
    def generate_schema_for binding
      template_text = read_template
      result = evaluate_erb template_text, binding
      write result
    end

    #
    #
    def evaluate_erb text, binding
      require 'erb'
      template = ERB.new text
      template.result binding
    end

    #
    #
    def read_template
      template_path = File.join SEARCH_ROOT, 'solr', 'conf', 'schema.xml.erb'
      schema = ''
      File.open(template_path, 'r') do |f|
        schema = f.read
      end
      schema
    end

    #
    #
    def write result
      schema_path = File.join SEARCH_ROOT, 'solr', 'conf', 'schema.xml'
      File.open(schema_path, 'w') do |f|
        f << result
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
picky-0.0.5 lib/picky/solr/schema_generator.rb
picky-0.0.4 lib/picky/solr/schema_generator.rb
picky-0.0.3 lib/picky/solr/schema_generator.rb
picky-0.0.2 lib/picky/solr/schema_generator.rb