Class: Jimmy::Index
Overview
Represents an in-memory collection of schemas
Instance Method Summary collapse
-
#add(uri, schema) ⇒ self
(also: #[]=)
Self, for chaining.
- #each {|schema_with_uri| ... } ⇒ Object
-
#initialize ⇒ Index
constructor
rubocop:disable Style/DocumentationMethod.
- #push(*schemas_with_uris) ⇒ self (also: #<<)
- #resolve(uri) ⇒ Jimmy::SchemaWithURI? (also: #[])
- #uri?(uri) ⇒ true, false (also: #key?)
- #uris ⇒ Array<Json::URI> (also: #keys)
Constructor Details
#initialize ⇒ Index
rubocop:disable Style/DocumentationMethod
11 12 13 |
# File 'lib/jimmy/index.rb', line 11 def initialize # rubocop:disable Style/DocumentationMethod @by_uri = {} end |
Instance Method Details
#add(uri, schema) ⇒ self Also known as: []=
Returns self, for chaining.
32 33 34 35 36 37 38 |
# File 'lib/jimmy/index.rb', line 32 def add(uri, schema) uri = Json::URI.new(uri) raise Error::BadArgument, 'Expected a schema' unless schema.is_a? Schema raise Error::BadArgument, 'Cannot index relative URIs' if uri.relative? push SchemaWithURI.new(uri, schema) end |
#each {|schema_with_uri| ... } ⇒ Object
74 75 76 |
# File 'lib/jimmy/index.rb', line 74 def each(&block) @by_uri.each_value &block end |
#push(*schemas_with_uris) ⇒ self Also known as: <<
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/jimmy/index.rb', line 44 def push(*schemas_with_uris) schemas_with_uris.each do |schema_with_uri| unless schema_with_uri.is_a? SchemaWithURI raise Error::BadArgument, 'Expected a SchemaWithURI' end @by_uri[schema_with_uri.uri] = schema_with_uri end self end |
#resolve(uri) ⇒ Jimmy::SchemaWithURI? Also known as: []
17 18 19 20 21 22 23 24 25 |
# File 'lib/jimmy/index.rb', line 17 def resolve(uri) uri = Json::URI.new(uri) raise Error::BadArgument, 'Cannot resolve relative URIs' if uri.relative? return @by_uri[uri] if @by_uri.key? uri return if uri.pointer.empty? resolve(uri / -1)&.resolve uri end |
#uri?(uri) ⇒ true, false Also known as: key?
66 67 68 |
# File 'lib/jimmy/index.rb', line 66 def uri?(uri) !resolve(uri).nil? end |
#uris ⇒ Array<Json::URI> Also known as: keys
58 59 60 |
# File 'lib/jimmy/index.rb', line 58 def uris @by_uri.keys end |