Class: GovukSchemas::Example

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_schemas/example.rb

Class Method Summary collapse

Class Method Details

.examples_path(schema_name) ⇒ String

Examples are changing location in schemas, this allows this utility to work with both locations

Parameters:

  • schema_name (String)

    like "detailed_guide", "policy" or "publication"

Returns:

  • (String)

    the path to use for examples



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

Parameters:

  • schema_name (String)

    like "detailed_guide", "policy" or "publication"

  • example_name (String)

    the name of the example JSON file

Returns:

  • (Hash)

    the example content item



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

Parameters:

  • schema_name (String)

    like "detailed_guide", "policy" or "publication"

Returns:

  • (Array)

    array of example content items



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