Source Browser

class ExampleEntry
attr_reader :slug, :title, :description, :tags, :source_file,
:entry_class, :entry_method, :position, :scenarios
def initialize(slug:, data:, base_path:)
@slug = slug
@title = data.fetch("title")
@description = data.fetch("description", "")
@tags = Array(data.fetch("tags", []))
@source_file = File.join(base_path, data.fetch("source_file"))
@entry_class = data.fetch("entry_class")
@entry_method = data.fetch("entry_method")
@position = data.fetch("position", 999)
@scenarios = build_scenarios(data.fetch("scenarios", []))
end
def source_code
File.read(@source_file)
end
def has_scenarios?
@scenarios.any?
end
private
def build_scenarios(raw)
raw.each_with_index.map do |s, i|
Scenario.new(
index: i,
name: s.fetch("name"),
description: s.fetch("description", ""),
inputs: build_inputs(s.fetch("inputs", []))
)
end
end
def build_inputs(raw)
raw.map do |inp|
Input.new(
name: inp.fetch("name"),
label: inp.fetch("label", inp.fetch("name").humanize),
options: inp.fetch("options")
)
end
end
Scenario = Struct.new(:index, :name, :description, :inputs, keyword_init: true)
Input = Struct.new(:name, :label, :options, keyword_init: true)
end

← Back