class ROF::Translators::JsonldToRof::PredicateHandler::UrlHandler
@api private For a given URL map all of the predicates; Some predicates require explicit mapping, while others may use implicit mapping.
Attributes
Public Class Methods
# File lib/rof/translators/jsonld_to_rof/predicate_handler.rb, line 100 def initialize(url) @url = url @within = [] @namespace_prefix = '' @slug_handlers = {} yield(self) if block_given? end
Public Instance Methods
@param [#to_s] predicate @return [nil, LocationExtractor] if the given predicate does not match the url, return nil; Otherwise return a LocationExtractor
@see LocationExtractor
# File lib/rof/translators/jsonld_to_rof/predicate_handler.rb, line 128 def location_extractor_for(predicate) return nil unless predicate.to_s =~ %r{^#{Regexp.escape(@url)}(.*)} slug = $1 handlers = handlers_for(slug) LocationExtractor.new(predicate, handlers) end
@param [String] slug = @param [Hash] options (with symbol keys) @option options [Boolean] :force - don't apply the within nor namespace prefix @option options [Array] :to - an array that will be nested Hash keys @option options [Boolean] :multiple (default true) - if true will append values to an Array; if false will have a singular (non-Array) value @yield If a block is given, call the block (and skip all other configuration) @yieldparam [String] object @see BlockSlugHandler
for details concerning a mapping via a block @see ExplicitLocationSlugHandler
for details concerning a mapping via a to: option
# File lib/rof/translators/jsonld_to_rof/predicate_handler.rb, line 157 def map(slug, options = {}, &block) @slug_handlers ||= {} @slug_handlers[slug] ||= [] if block_given? @slug_handlers[slug] << BlockSlugHandler.new(self, slug, options, block) else @slug_handlers[slug] << ExplicitLocationSlugHandler.new(self, slug, options) end end
The final key in the location array should be prefixed with the namespace_prefix
; By default this is “” @param [String, nil] prefix - what is the namespace prefix to apply to the last location in the array. @return [String]
# File lib/rof/translators/jsonld_to_rof/predicate_handler.rb, line 112 def namespace_prefix(prefix = nil) return @namespace_prefix if prefix.nil? @namespace_prefix = prefix end
Skip the given slug @param [String] slug
# File lib/rof/translators/jsonld_to_rof/predicate_handler.rb, line 169 def skip(slug) map(slug) { |*| } end
Prepend the within array to the location array @param [Array<String>, nil] location - where in the ROF
document are we putting the value @return [Array<String>]
# File lib/rof/translators/jsonld_to_rof/predicate_handler.rb, line 120 def within(location = nil) return @within if location.nil? @within = Array.wrap(location) end
Private Instance Methods
@param [String] slug - a slug that may or may not have been registered @return [Array<#call>] an array of handlers that each respond to call @see ImplicitLocationHandler
@see ExplicitLocationSlugHandler
@see BlockSlugHandler
# File lib/rof/translators/jsonld_to_rof/predicate_handler.rb, line 142 def handlers_for(slug) Array.wrap(@slug_handlers.fetch(slug) { ImplicitLocationHandler.new(self, slug) }) end