lib/solrizer/field_name_mapper.rb in solrizer-0.2.0 vs lib/solrizer/field_name_mapper.rb in solrizer-0.3.0

- old
+ new

@@ -1,62 +1,48 @@ -require "yaml" - -module Solrizer - module FieldNameMapper - - # Module Methods & Attributes - @@mappings = {} +# Re-Introduced for backwards compatibility +module Solrizer::FieldNameMapper + + # Class Methods -- These methods will be available on classes that include this Module - # Generates solr field names from settings in solr_mappings - def self.solr_name(field_name, field_type) - name = field_name.to_s + self.mappings[field_type.to_s].to_s - if field_name.kind_of?(Symbol) - return name.to_sym - else - return name.to_s + module ClassMethods + def mappings + return self.default_field_mapper.mappings end + + def id_field + return self.default_field_mapper.id_field + end + + # Re-loads solr mappings for the default field mapper's class + # and re-sets the default field mapper to an FieldMapper instance with those mappings. + def load_mappings( config_path=nil) + self.default_field_mapper.class.load_mappings(config_path) + self.default_field_mapper = self.default_field_mapper.class.new + end + + def solr_name(field_name, field_type, index_type = :searchable) + self.default_field_mapper.solr_name(field_name, field_type, index_type) + end + + def default_field_mapper + @@default_field_mapper ||= Solrizer::FieldMapper::Default.new + end + + def default_field_mapper=(field_mapper) + @@default_field_mapper = field_mapper + end end - def self.mappings - @@mappings - end + # Instance Methods -- These methods will be available on instances of classes that include this module - def self.mappings=(mappings) - @@mappings = mappings - end + attr_accessor :ox_namespaces - # Instance Methods - - def solr_name(field_name, field_type) - ::Solrizer::FieldNameMapper.solr_name(field_name, field_type) + def self.included(klass) + klass.extend(ClassMethods) end - def self.logger - @logger ||= defined?(RAILS_DEFAULT_LOGGER) ? RAILS_DEFAULT_LOGGER : Logger.new(STDOUT) - end - # Loads solr mappings from yml file. - # @config_path This is the path to the directory where your mappings file is stored. @default "RAILS_ROOT/config/solr_mappings.yml" - # @mappings_file This is the filename for your solr mappings YAML file. @default solr_mappings.yml - def self.load_mappings( config_path=nil ) - - if config_path.nil? - if defined?(RAILS_ROOT) - config_path = File.join(RAILS_ROOT, "config", "solr_mappings.yml") - end - # Default to using the config file within the gem - if !File.exist?(config_path.to_s) - config_path = File.join(File.dirname(__FILE__), "..", "..", "config", "solr_mappings.yml") - end - end - - logger.info("SOLRIZER: loading field name mappings from #{File.expand_path(config_path)}") - - @@mappings = YAML::load(File.open(config_path)) - - mappings["id"] = "id" unless mappings["id"] + def solr_name(field_name, field_type, index_type = :searchable) + self.class.solr_name(field_name, field_type, index_type) end - # This ensures that some mappings will always be loaded - self.load_mappings -end #FieldNameMapper -end #Solrizer +end \ No newline at end of file