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

url[R]

Public Class Methods

new(url) { |self| ... } click to toggle source
# 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

location_extractor_for(predicate) click to toggle source

@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
map(slug, options = {}, &block) click to toggle source

@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
namespace_prefix(prefix = nil) click to toggle source

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(slug) click to toggle source

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
within(location = nil) click to toggle source

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

handlers_for(slug) click to toggle source

@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