Class: Fulcrum::XlsReader

Inherits:
Object
  • Object
show all
Defined in:
lib/tofulcrum/fulcrum-xls-form/xls_reader.rb

Constant Summary collapse

VALID_ROW_TYPES =
[
  'section begin',
  'section end',
  'text',
  'choice',
  'numeric',
  'repeatable begin',
  'repeatable end',
  'date',
  'address',
  'signature',
  'photos',
  'label'
]

Class Method Summary collapse

Class Method Details

.boolean_value(value, default = false) ⇒ Object



186
187
188
189
# File 'lib/tofulcrum/fulcrum-xls-form/xls_reader.rb', line 186

def self.boolean_value(value, default=false)
  return default if value.nil?
  return %(true yes 1).include?(value.to_s.downcase.strip)
end

.element_type_for_type(type) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/tofulcrum/fulcrum-xls-form/xls_reader.rb', line 117

def self.element_type_for_type(type)
  case type
  when 'section begin'    then 'Section'
  when 'text'             then 'TextField'
  when 'choice'           then 'ChoiceField'
  when 'numeric'          then 'TextField'
  when 'repeatable begin' then 'Repeatable'
  when 'date'             then 'DateTimeField'
  when 'address'          then 'AddressField'
  when 'signature'        then 'SignatureField'
  when 'photos'           then 'PhotoField'
  when 'label'            then 'Label'
  else nil
  end
end

.make_data_name(row, container) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/tofulcrum/fulcrum-xls-form/xls_reader.rb', line 107

def self.make_data_name(row, container)
  return row[:data_name] if row[:data_name]

  if container[:data_name].present?
    "#{container[:data_name]}_#{row[:label].to_s.downcase.parameterize.underscore}"
  else
    row[:label].to_s.downcase.parameterize.underscore
  end
end

.make_element(row, container) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/tofulcrum/fulcrum-xls-form/xls_reader.rb', line 84

def self.make_element(row, container)
  {}.tap do |hash|
    hash[:type] = element_type_for_type(row[:type])
    hash[:label] = row[:label]
    hash[:data_name] = make_data_name(row, container)
    hash[:description] = row[:description]
    hash[:required] = boolean_value(row[:required])
    hash[:hidden] = boolean_value(row[:hidden])
    hash[:disabled] = boolean_value(row[:disabled])
    hash[:numeric] = true if row[:type] == 'numeric'
    hash[:elements] = [] if %w(Section Repeatable).include?(hash[:type])
    hash[:key] = SecureRandom.hex(2)

    case hash[:type]
    when 'ChoiceField'
      hash[:choices] = @choices[row['choices']] if hash[:type] == 'ChoiceField'
      hash[:allow_other] = boolean_value(row[:allow_other])
    when 'AddressField'
      hash[:auto_populate] = boolean_value(row[:auto_populate], true)
    end
  end
end

.make_elements(rows) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tofulcrum/fulcrum-xls-form/xls_reader.rb', line 36

def self.make_elements(rows)
  root = { elements: [] }

  containers = [ root ]

  current_container = root

  rows.each do |row|
    row = row.with_indifferent_access

    row_type = row['type'].to_s.downcase.strip

    raise "Invalid row type #{row_type}." unless VALID_ROW_TYPES.include?(row_type)

    if !['repeatable end', 'section end'].include?(row_type)
      element = make_element(row, current_container)
      current_container[:elements] << element
    end

    case row_type
    when 'section begin'
      containers << element
      current_container = element

    when 'section end'
      raise "section end found without matching begin" if containers.count == 1

      containers = containers[0..-2]
      current_container = containers.last

    when 'repeatable begin'
      containers << element
      current_container = element

    when 'repeatable end'
      raise "repeatable end found without matching begin" if containers.count == 1

      containers = containers[0..-2]
      current_container = containers.last

    end
  end

  raise "Unmatched sections or repeatables found. The open fields are: #{containers.map {|c| c[:label]}.join(', ')}" if containers.count != 1

  root[:elements]
end

.make_hash(sheet) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/tofulcrum/fulcrum-xls-form/xls_reader.rb', line 209

def self.make_hash(sheet)
  column_names = []

  sheet.row(1).to_a.each_with_index do |column_name, index|
    column_names << {name: column_name, index: index} if column_name.present?
  end

  rows = []

  sheet.each_with_index do |item, index|
    next if index == 0

    hash = {}

    column_names.each do |column|
      hash[column[:name]] = item[column[:index]]
    end

    rows << hash
  end

  rows
end

.parse_choices(spreadsheet) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/tofulcrum/fulcrum-xls-form/xls_reader.rb', line 147

def self.parse_choices(spreadsheet)
  sheet = spreadsheet.sheet('choices')

  return unless sheet

  all_choices = make_hash(sheet)

  @choices = {}

  all_choices.each do |choice|
    @choices[choice['list']] ||= []
    @choices[choice['list']] << choice.slice('label', 'value')
  end
end

.parse_metadata(spreadsheet) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/tofulcrum/fulcrum-xls-form/xls_reader.rb', line 133

def self.(spreadsheet)
  sheet = spreadsheet.sheet('metadata')

  raise "Can't find metadata sheet." unless sheet

   = make_hash(sheet)

  @metadata = {}

  .each do |row|
    @metadata[row['key']] = row['value']
  end
end

.parse_schema(spreadsheet) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/tofulcrum/fulcrum-xls-form/xls_reader.rb', line 27

def self.parse_schema(spreadsheet)
  form = { form: { name: @metadata['name'],
                   description: @metadata['description'],
                   status_field: @status_field,
                   elements: make_elements(make_hash(spreadsheet.sheet('form'))) } }

  puts form.to_json
end

.parse_status_field(spreadsheet) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/tofulcrum/fulcrum-xls-form/xls_reader.rb', line 162

def self.parse_status_field(spreadsheet)
  sheet = spreadsheet.sheet('status')

  return unless sheet

  statuses = make_hash(sheet)

  @status_field = {}
  @status_field[:label] = @metadata['status_label']
  @status_field[:description] = @metadata['status_description']
  @status_field[:data_name] = @metadata['status_data_name']
  @status_field[:default_value] = @metadata['status_default_value']
  @status_field[:hidden] = boolean_value(@metadata['status_hidden'])
  @status_field[:read_only] = boolean_value(@metadata['status_read_only'])
  @status_field[:enabled] = boolean_value(@metadata['status_enabled'])

  statuses.each do |status|
    @status_field[:choices] ||= []
    @status_field[:choices] << { label: status['label'] || '',
                                 value: status['value'] || status['label'] || '',
                                 color: status_color(status['color']) }
  end
end

.read(file) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/tofulcrum/fulcrum-xls-form/xls_reader.rb', line 18

def self.read(file)
  spreadsheet = Roo::Spreadsheet.open(file)

  (spreadsheet)
  parse_status_field(spreadsheet)
  parse_choices(spreadsheet)
  parse_schema(spreadsheet)
end

.status_color(human_name) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/tofulcrum/fulcrum-xls-form/xls_reader.rb', line 191

def self.status_color(human_name)
  case human_name.to_s.strip.downcase
  when 'black'      then '#242424'
  when 'gray'       then '#B3B3B3'
  when 'white'      then '#FFFFFF'
  when 'brown'      then '#704B10'
  when 'pink'       then '#DA0796'
  when 'red'        then '#CB0D0C'
  when 'orange'     then '#FF8819'
  when 'yellow'     then '#FFD300'
  when 'green'      then '#87D30F'
  when 'dark green' then '#2D5D00'
  when 'dark blue'  then '#294184'
  when 'blue'       then '#1891C9'
  else '#CB0D0C'
  end
end