Class: Libis::Tools::Metadata::SubfieldCriteriaParser

Inherits:
BasicParser show all
Defined in:
lib/libis/tools/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



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

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



50
51
52
53
54
55
# File 'lib/libis/tools/metadata/parser/subfield_criteria_parser.rb', line 50

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



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

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



41
42
43
44
45
46
47
48
# File 'lib/libis/tools/metadata/parser/subfield_criteria_parser.rb', line 41

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