Class: GovukSchemas::Example
- Inherits:
-
Object
- Object
- GovukSchemas::Example
- Defined in:
- lib/govuk_schemas/example.rb
Class Method Summary collapse
-
.examples_path(schema_name) ⇒ String
Examples are changing location in schemas, this allows this utility to work with both locations.
-
.find(schema_name, example_name:) ⇒ Hash
Find an example by name.
-
.find_all(schema_name) ⇒ Array
Find all examples for a schema.
Class Method Details
.examples_path(schema_name) ⇒ String
Examples are changing location in schemas, this allows this utility to work with both locations
29 30 31 32 33 34 35 36 |
# File 'lib/govuk_schemas/example.rb', line 29 def self.examples_path(schema_name) examples_dir = "#{GovukSchemas.content_schema_dir}/examples" if Dir.exist?(examples_dir) "#{examples_dir}/#{schema_name}/frontend" else "#{GovukSchemas.content_schema_dir}/formats/#{schema_name}/frontend/examples" end end |
.find(schema_name, example_name:) ⇒ Hash
Find an example by name
19 20 21 22 |
# File 'lib/govuk_schemas/example.rb', line 19 def self.find(schema_name, example_name:) json = File.read("#{examples_path(schema_name)}/#{example_name}.json") JSON.parse(json) end |
.find_all(schema_name) ⇒ Array
Find all examples for a schema
7 8 9 10 11 12 |
# File 'lib/govuk_schemas/example.rb', line 7 def self.find_all(schema_name) Dir.glob("#{examples_path(schema_name)}/*.json").map do |filename| json = File.read(filename) JSON.parse(json) end end |