Class: Libis::Metadata::Parser::SubfieldCriteriaParser

Inherits:
BasicParser
  • Object
show all
Defined in:
lib/libis/metadata/parser/subfield_criteria_parser.rb

Overview

New style parsers and converters for metadata. New, not finished and untested.

Instance Method Summary collapse

Methods inherited from BasicParser

#any_quoted, #complement, #grouped, #grouped_anonymous, #transformer

Instance Method Details

#criteria_to_s(criteria) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/libis/metadata/parser/subfield_criteria_parser.rb', line 27

def criteria_to_s(criteria)
  case criteria
  when Array
    # leave as is
  when Hash
    criteria = [criteria]
  else
    return criteria
  end
  criteria.map {|selection| selection_to_s(selection)}.join(' ')
end

#match_criteria(criteria, data) ⇒ Object



48
49
50
51
52
53
# File 'lib/libis/metadata/parser/subfield_criteria_parser.rb', line 48

def match_criteria(criteria, data)
  tree = self.new.parse(criteria)
  return true if tree.is_a? String
  tree = [tree] unless tree.is_a? Array
  tree.map {|selection| match_selection(selection, data)}.any?
end

#match_selection(selection, data) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/libis/metadata/parser/subfield_criteria_parser.rb', line 55

def match_selection(selection, data)
  must_match = selection[:must].to_s.split('')
  return false unless must_match == (must_match & data)
  one_of = selection[:one_of].to_s.split('')
  return false unless one_of.empty? || (one_of & data).any?
  only_one_of = selection[:only_one_of].to_s.split('')
  return false unless only_one_of.empty? || (only_one_of & data).size != 1
  return false if match_selection(selection[:not], data) if selection[:not]
  true
end

#selection_to_s(selection) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/libis/metadata/parser/subfield_criteria_parser.rb', line 39

def selection_to_s(selection)
  return selection unless selection.is_a? Hash
  result = "#{selection[:must]}"
  result += "(#{selection[:one_of]})" if selection[:one_of]
  result += "{#{selection[:only_one_of]}}" if selection[:only_one_of]
  result += "-#{selection_to_s(selection[:not])}" if selection[:not]
  result
end