Sha256: 35df7c4b4313ce8c2e5ca4c5a63514ce05b1f14168a1f86199932352b8996aed

Contents?: true

Size: 1.32 KB

Versions: 6

Compression:

Stored size: 1.32 KB

Contents

module GovukSchemas
  class Example
    # Find all examples for a schema
    #
    # @param schema_name [String] like "detailed_guide", "policy" or "publication"
    # @return [Array] array of example content items
    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

    # Find an example by name
    #
    # @param schema_name [String] like "detailed_guide", "policy" or "publication"
    # @param example_name [String] the name of the example JSON file
    # @return [Hash] the example content item
    def self.find(schema_name, example_name:)
      json = File.read("#{examples_path(schema_name)}/#{example_name}.json")
      JSON.parse(json)
    end

    # Examples are changing location in schemas, this allows this utility
    # to work with both locations
    #
    # @param schema_name [String] like "detailed_guide", "policy" or "publication"
    # @return [String] the path to use for examples
    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
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
govuk_schemas-5.0.4 lib/govuk_schemas/example.rb
govuk_schemas-5.0.3 lib/govuk_schemas/example.rb
govuk_schemas-5.0.0 lib/govuk_schemas/example.rb
govuk_schemas-4.7.0 lib/govuk_schemas/example.rb
govuk_schemas-4.6.0 lib/govuk_schemas/example.rb
govuk_schemas-4.5.0 lib/govuk_schemas/example.rb