Class: Cql

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ Cql

Returns a new instance of Cql.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/enju_biblio/porta_cql.rb', line 8

def initialize(line)
  @logic = nil
  @query = split_clause_text(line).collect{|txt| Clause.new(txt)}

  from_day, @query = extract_with_index(@query, /from/io)
  @from = comp_date(from_day)
  until_day, @query = extract_with_index(@query, /until/io)
  @until = comp_date(until_day)
  sort_by, @query = extract_with_index(@query, /sortBy/io)
  @sort_by = sort_by ? sort_by.terms.first : ''
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



20
21
22
# File 'lib/enju_biblio/porta_cql.rb', line 20

def from
  @from
end

#logicObject (readonly)

Returns the value of attribute logic.



20
21
22
# File 'lib/enju_biblio/porta_cql.rb', line 20

def logic
  @logic
end

#queryObject (readonly)

Returns the value of attribute query.



20
21
22
# File 'lib/enju_biblio/porta_cql.rb', line 20

def query
  @query
end

#sort_byObject (readonly)

Returns the value of attribute sort_by.



20
21
22
# File 'lib/enju_biblio/porta_cql.rb', line 20

def sort_by
  @sort_by
end

#untilObject (readonly)

Returns the value of attribute until.



20
21
22
# File 'lib/enju_biblio/porta_cql.rb', line 20

def until
  @until
end

Instance Method Details

#==(other) ⇒ Object



22
23
24
25
26
# File 'lib/enju_biblio/porta_cql.rb', line 22

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

#split_clause_text(line) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/enju_biblio/porta_cql.rb', line 32

def split_clause_text(line)
  clauses = []

  s = StringScanner.new(line)
  text = ''
  while s.rest?
    case
    when s.scan(/\s+/)
      text << s.matched
    when s.scan(/"(?:[^"\\]|\\.)*"/)
      text << s.matched
    when s.scan(/(AND|OR)/i)
      logic = s.matched.upcase
      if @logic
        raise QuerySyntaxError unless @logic == logic
      else
        @logic = logic
      end
      clauses << text.strip
      text = ''
    when s.scan(/\S*/)
      text << s.matched
    end
  end
  clauses << text.strip
  clauses.collect{|txt| txt.gsub(/(\A\(|\)\Z)/, '')}
end

#to_sunspotObject



28
29
30
# File 'lib/enju_biblio/porta_cql.rb', line 28

def to_sunspot
  (@query.collect{|c| c.to_sunspot} + date_range(@from, @until)).join(" #{@logic} ")
end