Class: Clause

Inherits:
Object
  • Object
show all
Defined in:
lib/enju_biblio/porta_cql.rb

Constant Summary collapse

INDEX =
/(dpid|dpgroupid|title|creator|publisher|ndc|description|subject|isbn|issn|jpno|from|until|anywhere|porta_type|digitalize_type|webget_type|payment_type|ndl_agent_type|ndlc|itemno)/io
SORT_BY =
/sortBy/io
RELATION =
/(=|exact|\^|any|all)/io
MATCH_ALL =
%w[title creator publisher]
MATCH_EXACT =
%w[dpid dpgroupid isbn issn jpno porta_type digitalize_type webget_type payment_type ndl_agent_type itemno]
MATCH_PART =
%w[description subject]
MATCH_AHEAD =
%w[ndc ndlc]
MATCH_DATE =
%w[from until]
MATCH_ANYWHERE =
%w[anywhere]
LOGIC_ALL =
%w[title creator publisher description subject anywhere]
LOGIC_ANY =
%w[dpid ndl_agent_type]
LOGIC_EQUAL =
%w[dpgroupid ndc isbn issn jpno from until porta_type digitalize_type webget_type payment_type ndlc itemno]
MULTIPLE =
%w[dpid title creator publisher description subject anywhere ndl_agent_type]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Clause

Returns a new instance of Clause.



117
118
119
120
121
122
123
124
125
# File 'lib/enju_biblio/porta_cql.rb', line 117

def initialize(text)
  unless text.empty?
    @index, @relation, @terms = scan(text)
    porta_adapter
    @field = @index
  else
    @index = ''
  end
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



127
128
129
# File 'lib/enju_biblio/porta_cql.rb', line 127

def index
  @index
end

#relationObject (readonly)

Returns the value of attribute relation.



127
128
129
# File 'lib/enju_biblio/porta_cql.rb', line 127

def relation
  @relation
end

#termsObject (readonly)

Returns the value of attribute terms.



127
128
129
# File 'lib/enju_biblio/porta_cql.rb', line 127

def terms
  @terms
end

Instance Method Details

#==(other) ⇒ Object



129
130
131
132
133
# File 'lib/enju_biblio/porta_cql.rb', line 129

def ==(other)
  instance_variables.all? do |val|
    instance_variable_get(val) == other.instance_variable_get(val)
  end
end

#logic_adapterObject



169
170
171
172
173
174
175
176
177
178
# File 'lib/enju_biblio/porta_cql.rb', line 169

def logic_adapter
  case
  when LOGIC_ALL.include?(@index)
    raise AdapterError unless %w[ALL ANY = EXACT ^].include?(@relation)
  when LOGIC_ANY.include?(@index)
    raise AdapterError unless %w[ANY =].include?(@relation)
  when LOGIC_EQUAL.include?(@index)
    raise AdapterError unless %w[=].include?(@relation)
  end
end

#multiple_adapterObject



180
181
182
183
184
# File 'lib/enju_biblio/porta_cql.rb', line 180

def multiple_adapter
  unless MULTIPLE.include?(@index)
    raise AdapterError if @terms.size > 1
  end
end

#porta_adapterObject



164
165
166
167
# File 'lib/enju_biblio/porta_cql.rb', line 164

def porta_adapter
  logic_adapter
  multiple_adapter
end

#scan(text) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/enju_biblio/porta_cql.rb', line 135

def scan(text)
  ss = StringScanner.new(text)
  index = ''
  relation = ''
  terms = []

  if ss.scan(INDEX) or ss.scan(SORT_BY)
    index = ss[0]
  end
  #else
  #  raise ScannerError, "index or the sortBy is requested in '#{text}'"
  #end
  ss.scan(/\s+/)
  if ss.scan(RELATION)
    relation = ss[0].upcase
  end
  #else
  #  raise ScannerError, "relation is requested in '#{text}'"
  #end
  ss.scan(/\s+/)
  if ss.scan(/.+/)
    terms = ss[0].gsub(/(\A\"|\"\Z)/, '').split
  else
    raise ScannerError, "search term(s) is requested in '#{text}'"
  end

  [index, relation, terms]
end

#to_sunspotObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/enju_biblio/porta_cql.rb', line 186

def to_sunspot
  case
  when MATCH_ALL.include?(@index)
    to_sunspot_match_all
  when MATCH_EXACT.include?(@index)
    to_sunspot_match_exact
  when MATCH_PART.include?(@index)
    to_sunspot_match_part
  when MATCH_AHEAD.include?(@index)
    to_sunspot_match_ahead
  when MATCH_ANYWHERE.include?(@index)
    to_sunspot_match_anywhere
  when @index.empty?
    @terms.join(' ')
  end
end

#to_sunspot_match_aheadObject



251
252
253
# File 'lib/enju_biblio/porta_cql.rb', line 251

def to_sunspot_match_ahead
  "%s_%s:(%s*)" % [@field, :s, @terms.first]
end

#to_sunspot_match_allObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/enju_biblio/porta_cql.rb', line 203

def to_sunspot_match_all
  term = @terms.join(' ')
  case @relation
  when /\A=\Z/
    unless /\A\^(.+)/ =~ term
      "%s_%s:(%s)" % [@field, :text, term]
    else
      ahead_tarm = $1.gsub("\s", '').downcase
      "connect_%s_%s:(%s*)" % [@field, :s, ahead_tarm]
    end
  when /\AEXACT\Z/
    "%s_%s:(%s)" % [@field, :sm, term.gsub(' ', '')]
  when /\AANY\Z/
    "%s_%s:(%s)" % [@field, :text, multiple_to_sunspot(@terms, :any)]
  when /\AALL\Z/
    "%s_%s:(%s)" % [@field, :text, multiple_to_sunspot(@terms, :all)]
  else
    raise QuerySyntaxError
  end
end

#to_sunspot_match_anywhereObject



255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/enju_biblio/porta_cql.rb', line 255

def to_sunspot_match_anywhere
  case @relation
  when /\A=\Z/
    term = @terms.join(' ')
    "(%s)" % [trim_ahead(term)]
  when /\AANY\Z/
    "(%s)" % [multiple_to_sunspot(@terms, :any)]
  when /\AALL\Z/
    "(%s)" % [multiple_to_sunspot(@terms, :all)]
  else
    raise QuerySyntaxError
  end
end

#to_sunspot_match_exactObject



224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/enju_biblio/porta_cql.rb', line 224

def to_sunspot_match_exact
  case @relation
  when /\A=\Z/
    term = @terms.join(' ')
    type = @field != 'issn' ? :sm : :s
    "%s_%s:(%s)" % [@field, type, term]
  when /\AANY\Z/
    "%s_%s:(%s)" % [@field, :sm, multiple_to_sunspot(@terms, :any)]
  else
    raise QuerySyntaxError
  end
end

#to_sunspot_match_partObject



237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/enju_biblio/porta_cql.rb', line 237

def to_sunspot_match_part
  case @relation
  when /\A=\Z/
    term = @terms.join(' ')
    "%s_%s:(%s)" % [@field, :text, trim_ahead(term)]
  when /\AANY\Z/
    "%s_%s:(%s)" % [@field, :text, multiple_to_sunspot(@terms, :any)]
  when /\AALL\Z/
    "%s_%s:(%s)" % [@field, :text, multiple_to_sunspot(@terms, :all)]
  else
    raise QuerySyntaxError
  end
end