<commit_before>require 'yaml'

module FML
  class FMLForm
    attr_reader :form, :title, :fieldsets

    def initialize(form)
      parse(YAML.load(form))
    end

    private

    def parse(yaml)
      @form = getrequired(yaml, "form")
      @title = getrequired(@form, "title")

      # @fieldsets is just a list of lists of fields
      @fieldsets = getrequired(@form, "fieldsets").collect do |fieldset|
        getrequired(fieldset, "fieldset").collect do |field|
          parsefield(field["field"])
        end
      end
    end

    def parsefield(field)
      name = getrequired(field, "name")
      type = getrequired(field, "fieldType")
      label = getrequired(field, "label")
      prompt = field["prompt"]
      is_required = field["isRequired"]

      #TODO: actually handle the things below this point
      options = field["options"]
      conditional = field["conditionalOn"]
      validations = field["validations"]

      FMLField.new(name, type, label, prompt, is_required, options, conditional,
                   validations)
    end

    def getrequired(obj, attr)
      x = obj[attr]
      if x.nil?
        raise KeyError.new("Could not find required `#{attr}` attribute in #{obj}")
      end
      x
    end
  end
end<commit_msg>Add version attribute and begin code for filling in<commit_after>require 'yaml'

module FML
  class FMLForm
    attr_reader :form, :title, :version, :fieldsets, :fields

    def initialize(form)
      @fields = {}
      parse(YAML.load(form))
    end

    def fill(params)
      params.each do |field|
        if @fields.has? field
          @fields[field].value = params[field]
        end
      end
    end

    def to_json
      #TODO: turn an FMLForm into a json doc
    end

    private

    def parse(yaml)
      @form = getrequired(yaml, "form")
      @title = getrequired(@form, "title")
      @version = getrequired(@form, "version")

      # @fieldsets is just a list of lists of fields
      @fieldsets = getrequired(@form, "fieldsets").collect do |fieldset|
        getrequired(fieldset, "fieldset").collect do |field|
          parsefield(field["field"])
        end
      end
    end

    def parsefield(field)
      name = getrequired(field, "name")
      type = getrequired(field, "fieldType")
      label = getrequired(field, "label")
      prompt = field["prompt"]
      is_required = field["isRequired"]

      #TODO: actually handle the things below this point
      options = field["options"]
      conditional = field["conditionalOn"]
      validations = field["validations"]

      field = FMLField.new(name, type, label, prompt, is_required, options,
                           conditional, validations)
      @fields[name.to_s] = field
    end

    def getrequired(obj, attr)
      x = obj[attr]
      if x.nil?
        raise KeyError.new("Could not find required `#{attr}` attribute in #{obj}")
      end
      x
    end
  end
end
{'fraction_non_alphanumeric': 0.10885989010989011, 'fraction_numerical': 0.0, 'mean_word_length': 2.4844497607655502, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5351426', 'n_tokens_mistral': 870, 'n_tokens_neox': 846, 'n_words': 248}