Class: GovukSchemas::Schema
- Inherits:
-
Object
- Object
- GovukSchemas::Schema
- Defined in:
- lib/govuk_schemas/schema.rb
Class Method Summary collapse
-
.all(schema_type: "*") ⇒ Array<Hash>
Return all schemas in a hash, keyed by schema name.
-
.find(schema) ⇒ Hash
Find a schema by name.
-
.random_schema(schema_type:) ⇒ Hash
Return a random schema of a certain type.
-
.schema_names ⇒ Array
Return all schema names.
Class Method Details
.all(schema_type: "*") ⇒ Array<Hash>
Return all schemas in a hash, keyed by schema name
22 23 24 25 26 27 |
# File 'lib/govuk_schemas/schema.rb', line 22 def self.all(schema_type: "*") schema_type = "publisher_v2" if schema_type == "publisher" Dir.glob("#{GovukSchemas.content_schema_dir}/dist/formats/*/#{schema_type}/*.json").each_with_object({}) do |file_path, hash| hash[file_path] = JSON.parse(File.read(file_path)) end end |
.find(schema) ⇒ Hash
Find a schema by name
13 14 15 16 |
# File 'lib/govuk_schemas/schema.rb', line 13 def self.find(schema) file_path = "#{GovukSchemas.content_schema_dir}/dist/formats/#{location_for_schema_name(schema)}" JSON.parse(File.read(file_path)) end |
.random_schema(schema_type:) ⇒ Hash
Return a random schema of a certain type
33 34 35 |
# File 'lib/govuk_schemas/schema.rb', line 33 def self.random_schema(schema_type:) all(schema_type:).values.sample end |
.schema_names ⇒ Array
Return all schema names
40 41 42 43 44 |
# File 'lib/govuk_schemas/schema.rb', line 40 def self.schema_names Dir.glob("#{GovukSchemas.content_schema_dir}/dist/formats/*").map do |directory| File.basename(directory) end end |