Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #23 from projecthydra/suffixes-for-everyone

Enhance Suffix#config so it can be usefully overridden by downstream
  • Loading branch information...
commit 0d857e03886f3253dbf811b29f7042c69788526c 2 parents a222cf3 + ac8c64b
Justin Coyne jcoyne authored
Showing with 131 additions and 33 deletions.
  1. +51 −33 lib/solrizer/suffix.rb
  2. +80 −0 spec/units/suffix_spec.rb
84 lib/solrizer/suffix.rb
View
@@ -1,20 +1,26 @@
+require 'ostruct'
+
module Solrizer
class Suffix
- def initialize(fields)
- @fields = fields
+ def initialize(*fields)
+ @fields = fields.flatten
end
def multivalued?
- @fields.include? :multivalued
+ has_field? :multivalued
end
def stored?
- @fields.include? :stored
+ has_field? :stored
end
def indexed?
- @fields.include? :indexed
+ has_field? :indexed
+ end
+
+ def has_field? f
+ f.to_sym == :type or @fields.include? f.to_sym
end
def data_type
@@ -22,40 +28,52 @@ def data_type
end
def to_s
- stored_suffix = config[:stored_suffix] if stored?
- index_suffix = config[:index_suffix] if indexed?
- multivalued_suffix = config[:multivalued_suffix] if multivalued?
+
raise Solrizer::InvalidIndexDescriptor, "Missing datatype for #{@fields}" unless data_type
- type_suffix = config[:type_suffix].call(data_type)
- raise Solrizer::InvalidIndexDescriptor, "Invalid datatype `#{data_type.inspect}'. Must be one of: :date, :time, :text, :text_en, :string, :integer" unless type_suffix
- [config[:suffix_delimiter], type_suffix, stored_suffix, index_suffix, multivalued_suffix].join
+ field_suffix = [config.suffix_delimiter]
+
+ config.fields.select { |f| has_field? f }.each do |f|
+ key = :"#{f}_suffix"
+ field_suffix << if config.send(key).is_a? Proc
+ config.send(key).call(@fields)
+ else
+ config.send(key)
+ end
+ end
+
+ field_suffix.join
end
+ def self.config
+ @config ||= OpenStruct.new :fields => [:type, :stored, :indexed, :multivalued],
+ suffix_delimiter: '_',
+ type_suffix: (lambda do |fields|
+ type = fields.first
+ case type
+ when :string, :symbol # TODO `:symbol' usage ought to be deprecated
+ 's'
+ when :text
+ 't'
+ when :text_en
+ 'te'
+ when :date, :time
+ 'dt'
+ when :integer
+ 'i'
+ when :boolean
+ 'b'
+ else
+ raise Solrizer::InvalidIndexDescriptor, "Invalid datatype `#{type.inspect}'. Must be one of: :date, :time, :text, :text_en, :string, :symbol, :integer, :boolean"
+ end
+ end),
+ stored_suffix: 's',
+ indexed_suffix: 'i',
+ multivalued_suffix: 'm'
+ end
- private
def config
- @config ||=
- {suffix_delimiter: '_',
- type_suffix: lambda do |type|
- case type
- when :string, :symbol # TODO `:symbol' usage ought to be deprecated
- 's'
- when :text
- 't'
- when :text_en
- 'te'
- when :date, :time
- 'dt'
- when :integer
- 'i'
- when :boolean
- 'b'
- end
- end,
- stored_suffix: 's',
- index_suffix: 'i',
- multivalued_suffix: 'm'}
+ @config ||= self.class.config.dup
end
end
end
80 spec/units/suffix_spec.rb
View
@@ -0,0 +1,80 @@
+require 'spec_helper'
+
+describe Solrizer::Suffix do
+
+ describe "#multivalued?" do
+ it "should be multivalued if :multivalued is among the field types" do
+ expect(Solrizer::Suffix.new(:multivalued)).to be_multivalued
+ end
+
+ it "should not be multivalued if :multivalued was not passed in a field type" do
+ expect(Solrizer::Suffix.new(:some_other_field_type)).to_not be_multivalued
+ end
+ end
+
+ describe "#stored?" do
+ it "should be stored if :stored is among the field types" do
+ expect(Solrizer::Suffix.new(:stored)).to be_stored
+ end
+
+ it "should not be stored if :stored was not passed in a field type" do
+ expect(Solrizer::Suffix.new(:some_other_field_type)).to_not be_stored
+ end
+ end
+
+ describe "#indexed?" do
+ it "should be indexed if :indexed is among the field types" do
+ expect(Solrizer::Suffix.new(:indexed)).to be_indexed
+ end
+
+ it "should not be indexed if :indexed was not passed in a field type" do
+ expect(Solrizer::Suffix.new(:some_other_field_type)).to_not be_indexed
+ end
+ end
+ describe "#has_field?" do
+ subject do
+ Solrizer::Suffix.new(:type, :a, :b, :c)
+ end
+ it "should be able to tell if a field is in the suffix or not" do
+ expect(subject).to have_field :a
+ expect(subject).to have_field :b
+ expect(subject).to have_field :c
+ expect(subject).to_not have_field :d
+ end
+ end
+
+ describe "#data_type" do
+ it "should always be the first argument to the suffix" do
+ expect(Solrizer::Suffix.new(:some_type, :a).data_type).to eq :some_type
+ end
+ end
+
+ describe "#to_s" do
+ it "should combine the fields into a suffix string" do
+ expect(Solrizer::Suffix.new(:string, :stored, :indexed).to_s).to eq '_ssi'
+ expect(Solrizer::Suffix.new(:integer, :stored, :multivalued).to_s).to eq '_ism'
+ end
+ end
+
+ describe "config" do
+ subject do
+ Solrizer::Suffix.new(:my_custom_type, :a, :b, :c)
+ end
+
+ it "should let you mess with the suffix config" do
+ subject.config.fields += [:b]
+ subject.config.suffix_delimiter = "#"
+ subject.config.type_suffix = lambda do |fields|
+ type = fields.first
+
+ if type == :my_custom_type
+ "custom_suffix_"
+ else
+ "nope"
+ end
+ end
+ subject.config.b_suffix = 'now_with_more_b'
+ expect(subject.to_s).to eq "#custom_suffix_now_with_more_b"
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.