Sha256: 29dbaedba911c905c931e6bb407a7a8403b489a0ae0dbacfc47e440d18813f56

Contents?: true

Size: 1.54 KB

Versions: 7

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

# {
#   "id": "intake_questionnaire",
#   "display_name": "Intake Questionnaire at Baseline Visit",
#   "code_book": "Baseline-Visit-Intake-Questionnaire.pdf"
# }

require 'spout/models/record'

module Spout
  module Models
    class Form < Spout::Models::Record
      attr_accessor :id, :folder, :display_name, :code_book
      attr_accessor :errors

      def initialize(file_name, dictionary_root)
        @errors = []
        @id     = file_name.to_s.gsub(/^(.*)\/|\.json$/, '').downcase
        @folder = file_name.to_s.gsub(/^#{dictionary_root}\/forms\/|#{@id}\.json$/, '')

        json = begin
          JSON.parse(File.read(file_name))
        rescue => e
          form_name = file_name.to_s.gsub(/^(.*)\/|\.json$/, '').downcase
          @errors << "Error Parsing #{form_name}.json: #{e.message}"
          nil
        end

        if json.is_a? Hash
          %w(display_name code_book).each do |method|
            instance_variable_set("@#{method}", json[method])
          end

          @errors << "'id': #{json['id'].inspect} does not match filename #{@id.inspect}" if @id != json['id']
        elsif json
          @errors << "Form must be a valid hash in the following format: {\n\"id\": \"FORM_ID\",\n  \"display_name\": \"FORM DISPLAY NAME\",\n  \"code_book\": \"FORMPDF.pdf\"\n}"
        end
      end

      def deploy_params
        { name: id, folder: folder.to_s.gsub(%r{/$}, ''),
          display_name: display_name, code_book: code_book,
          spout_version: Spout::VERSION::STRING }
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
spout-0.12.1 lib/spout/models/form.rb
spout-0.12.0 lib/spout/models/form.rb
spout-0.12.0.rc2 lib/spout/models/form.rb
spout-0.12.0.rc lib/spout/models/form.rb
spout-0.12.0.beta2 lib/spout/models/form.rb
spout-0.12.0.beta1 lib/spout/models/form.rb
spout-0.11.1 lib/spout/models/form.rb