Class: Jimmy::FileMap

Inherits:
Object
  • Object
show all
Defined in:
lib/jimmy/file_map.rb

Overview

Maps a directory of files to schemas with URIs. Can be used as a URI resolver with SchemerFactory.

Given ~/schemas/user.rb as a schema file:

file_map = FileMap.new('~/schemas', 'http://example.com/schemas/', suffix: '.json')
file_map.resolve('user.json') # => SchemaWithURI

Calling SchemaWithURI#as_json on the above will include the full ID http://example.com/schemas/user.json# in the $id key.

Including the suffix in the call to #resolve is optional.

If you initialize a FileMap with live: true, files will be loaded lazily and repeatedly, every time #resolve or #index is called. This is intended as convenience for development environments.

Constant Summary collapse

DEFAULT_LOADERS =
{
  'rb'   => Loaders::Ruby,
  'json' => Loaders::JSON
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(base_dir, base_uri = nil, live: false, loaders: DEFAULT_LOADERS, suffix: '') ⇒ FileMap

Returns a new instance of FileMap.

Parameters:

  • base_dir (Pathname, String)

    The directory that should map to the given URI.

  • base_uri (Json::URI, URI, String) (defaults to: nil)

    The URI that should resolve to the given directory. If omitted, a file:// URI will be used for the absolute path of the given directory.

  • live (true, false) (defaults to: false)

    When true, schemas are not stored in memory, and are instead live-reloaded on demand. Typically only useful for development environments.

  • loaders (Hash{String => #call}) (defaults to: DEFAULT_LOADERS)

    Loaders for one or more file types. By default, .json files are parsed as-is, and .rb files are evaluated in the context of a Loaders::Ruby instance, which exposes the methods in Macros.

  • suffix (String) (defaults to: '')

    Optional suffix that will be appended to each schema ID. This can be set to “.json” if, for example, you want your schemas to have .json suffixes when you serve them over HTTP.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jimmy/file_map.rb', line 47

def initialize(
  base_dir,
  base_uri = nil,
  live:    false,
  loaders: DEFAULT_LOADERS,
  suffix:  ''
)
  @dir = Pathname(base_dir).realpath
  unless @dir.directory? && @dir.readable?
    raise Error::BadArgument, 'Expected a readable directory'
  end

  base_uri ||= uri_for_dir
  @uri = Json::URI.new(base_uri.to_s, container: true)

  @live    = live
  @loaders = loaders
  @suffix  = suffix

  index unless live
end

Instance Method Details

#indexJimmy::Index

Get an index of all schemas in the file map’s directory.

Returns:



87
88
89
90
91
92
93
94
# File 'lib/jimmy/file_map.rb', line 87

def index
  return @index if @index

  index = build_index
  @index = index unless live?

  index
end

#live?true, false

Returns true if live-reloading is enabled.

Returns:

  • (true, false)


98
99
100
# File 'lib/jimmy/file_map.rb', line 98

def live?
  @live
end

#resolve(uri) ⇒ Jimmy::SchemaWithURI? Also known as: []

Given a URI, either absolute or relative to the file map’s base URI, returns a SchemaWithURI if a matching schema is found.

Parameters:

Returns:



73
74
75
76
77
78
79
80
81
# File 'lib/jimmy/file_map.rb', line 73

def resolve(uri)
  uri          = make_child_uri(uri)
  absolute_uri = @uri + uri

  return index.resolve(absolute_uri) unless live?

  schema = load_file(path_for_uri uri)&.get_fragment(uri.fragment)
  schema && SchemaWithURI.new(absolute_uri, schema)
end