Class: Spout::Models::Form

Inherits:
Record
  • Object
show all
Defined in:
lib/spout/models/form.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Record

find_by_id

Constructor Details

#initialize(file_name, dictionary_root) ⇒ Form

Returns a new instance of Form.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/spout/models/form.rb', line 17

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, encoding: "utf-8"))
  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

Instance Attribute Details

#code_bookObject

Returns the value of attribute code_book.



14
15
16
# File 'lib/spout/models/form.rb', line 14

def code_book
  @code_book
end

#display_nameObject

Returns the value of attribute display_name.



14
15
16
# File 'lib/spout/models/form.rb', line 14

def display_name
  @display_name
end

#errorsObject

Returns the value of attribute errors.



15
16
17
# File 'lib/spout/models/form.rb', line 15

def errors
  @errors
end

#folderObject

Returns the value of attribute folder.



14
15
16
# File 'lib/spout/models/form.rb', line 14

def folder
  @folder
end

#idObject

Returns the value of attribute id.



14
15
16
# File 'lib/spout/models/form.rb', line 14

def id
  @id
end

Instance Method Details

#deploy_paramsObject



41
42
43
44
45
# File 'lib/spout/models/form.rb', line 41

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