Sha256: 1adb8c5bbe6adcceca471d43f3be23ca153fe44a5d7e14b9b7a996f224e837ff

Contents?: true

Size: 1.33 KB

Versions: 13

Compression:

Stored size: 1.33 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

13 entries across 13 versions & 1 rubygems

Version Path
govuk_schemas-4.4.1 lib/govuk_schemas/example.rb
govuk_schemas-4.4.0 lib/govuk_schemas/example.rb
govuk_schemas-4.3.0 lib/govuk_schemas/example.rb
govuk_schemas-4.2.0 lib/govuk_schemas/example.rb
govuk_schemas-4.1.1 lib/govuk_schemas/example.rb
govuk_schemas-4.1.0 lib/govuk_schemas/example.rb
govuk_schemas-4.0.0 lib/govuk_schemas/example.rb
govuk_schemas-3.3.0 lib/govuk_schemas/example.rb
govuk_schemas-3.2.0 lib/govuk_schemas/example.rb
govuk_schemas-3.1.0 lib/govuk_schemas/example.rb
govuk_schemas-3.0.1 lib/govuk_schemas/example.rb
govuk_schemas-3.0.0 lib/govuk_schemas/example.rb
govuk_schemas-2.3.0 lib/govuk_schemas/example.rb