class RDF::Query::HashPatternNormalizer
An RDF
query pattern normalizer.
Attributes
The options for this hash pattern normalizer.
@return [Hash{Symbol => Object}]
Public Class Methods
Source
# File lib/rdf/query/hash_pattern_normalizer.rb, line 178 def initialize(**options) @options = options.dup end
@param [Hash{Symbol => Object}] options (Hash.new)
any additional normalization options.
@option options [String] :anonymous_subject_format (“__%s__”)
the string format for anonymous subjects.
Source
# File lib/rdf/query/hash_pattern_normalizer.rb, line 89 def normalize!(*args) hash_pattern = args.shift options = args.shift || {} anonymous_subject_format = options.fetch(:anonymous_subject_format, '__%s__') raise ArgumentError, "invalid hash pattern: #{hash_pattern.inspect}" unless hash_pattern.is_a?(Hash) counter = RDF::Query::HashPatternNormalizer::Counter.new hash_pattern.inject({}) { |acc, pair| subject, predicate_to_object = pair ensure_absence_of_duplicate_subjects!(acc, subject) normalized_predicate_to_object = normalize_hash!(predicate_to_object, acc, counter, anonymous_subject_format) ensure_absence_of_duplicate_subjects!(acc, subject) acc[subject] = normalized_predicate_to_object acc } end
Returns the normalization of the specified ‘hash_pattern`.
@overload normalize!(hash_pattern, **options)
@param [Hash{Symbol => Object}] hash_pattern (Hash.new) the query pattern as a hash. @param [Hash{Symbol => Object}] **options any additional normalization options. @option options [String] :anonymous_subject_format ("__%s__") the string format for anonymous subjects. @return [Hash{Symbol => Object}] the resulting query pattern as a normalized hash.
Private Class Methods
Source
# File lib/rdf/query/hash_pattern_normalizer.rb, line 113 def ensure_absence_of_duplicate_subjects!(acc, subject) raise "duplicate subject #{subject.inspect} in normalized hash pattern: #{acc.inspect}" if acc.key?(subject) return end
@private
Source
# File lib/rdf/query/hash_pattern_normalizer.rb, line 121 def normalize_array!(array, *args) raise ArgumentError, "invalid array pattern: #{array.inspect}" unless array.is_a?(Array) array.collect { |object| normalize_object!(object, *args) } end
@private
Source
# File lib/rdf/query/hash_pattern_normalizer.rb, line 131 def normalize_hash!(hash, *args) raise ArgumentError, "invalid hash pattern: #{hash.inspect}" unless hash.is_a?(Hash) hash.inject({}) { |acc, pair| acc[pair.first] = normalize_object!(pair.last, *args) acc } end
@private
Source
# File lib/rdf/query/hash_pattern_normalizer.rb, line 142 def normalize_object!(object, *args) case object when Array then normalize_array!(object, *args) when Hash then replace_hash_with_anonymous_subject!(object, *args) else object end end
@private
Source
# File lib/rdf/query/hash_pattern_normalizer.rb, line 152 def replace_hash_with_anonymous_subject!(hash, acc, counter, anonymous_subject_format) raise ArgumentError, "invalid hash pattern: #{hash.inspect}" unless hash.is_a?(Hash) subject = (anonymous_subject_format % counter.increment!).to_sym ensure_absence_of_duplicate_subjects!(acc, subject) normalized_hash = normalize_hash!(hash, acc, counter, anonymous_subject_format) ensure_absence_of_duplicate_subjects!(acc, subject) acc[subject] = normalized_hash subject end
@private
Public Instance Methods
Source
# File lib/rdf/query/hash_pattern_normalizer.rb, line 189 def normalize!(hash_pattern) self.class.normalize!(hash_pattern, @options) end
Equivalent to calling ‘self.class.normalize!(hash_pattern, self.options)`.
@param [Hash{Symbol => Object}] hash_pattern
the query pattern as a hash.
@return [Hash{Symbol => Object}]
the resulting query pattern as a normalized hash.